Skip to content

[#33] reduce download_all + _download_one complexity below grade C#36

Merged
VGonPa merged 1 commit into
developfrom
ws-radon-cleanup-media
May 24, 2026
Merged

[#33] reduce download_all + _download_one complexity below grade C#36
VGonPa merged 1 commit into
developfrom
ws-radon-cleanup-media

Conversation

@VGonPa
Copy link
Copy Markdown
Owner

@VGonPa VGonPa commented May 24, 2026

Summary

After round-1's "inline single-caller helpers" fix on PR #35, download_all hit radon C(18) and _download_one C(11). Local CI gate showed ⚠️ Radon: grade C function(s) detected (warning only).

This is a focused refactor that reduces those two functions to B without re-introducing the gold-plating that round-1 explicitly told me to inline.

What changed

  • _iter_eligible_attempts (new generator) yields (item_id, item, index, entry) work tuples. Carries the filter + empty-media skip + eligibility cascade + global limit countdown — real state-machine logic, not a renamed parameter list. download_all becomes a flat for-loop with one decision per iteration.
  • _filter_by_ids (new) handles the items-filter restriction up front.
  • _classify_status (new) folds the 4xx / 5xx / other branches in _download_one into a single lookup-then-bucket.

Result

Function Before After
download_all C(18) B(7)
_download_one C(11) B(9)
_iter_eligible_attempts (new) B(10)

No grade C functions remain in media.py.

The remaining 3 C-grade functions in the repo (generate.py:_render_index, validate.py:validate_judgment, archive.py:_archive_tweet_to_item) are pre-existing and outside the scope of #33. The Radon WARN may still trigger on them; this PR clears my contribution to the warning.

Test plan

  • uv run pytest → 431 passed (no test changes — behaviour preserved)
  • uv run ruff check src/ → clean
  • uv run ruff format --check src/ → clean
  • uv run mypy --ignore-missing-imports src/ → clean
  • uv run radon cc src/xbrain/media.py -nc -s → empty
  • bash scripts/check.sh → ALL CRITICAL CHECKS PASSED

🤖 Generated with Claude Code

After round-1's inline-the-helpers fix, download_all hit radon C(18) and
_download_one C(11) — they carried inherent orchestration complexity that
flat inlining could not shed.

Targeted refactor without re-introducing single-caller-rename helpers:

- Extract _iter_eligible_attempts as a generator yielding the (item_id,
  item, index, entry) work tuples. The generator carries the filter +
  empty-media skip + eligibility cascade + global limit countdown — real
  state-machine logic, not a renamed parameter list. download_all becomes
  a flat loop with one decision per iteration.

- Extract _filter_by_ids for the items-filter restriction (was inlined
  inside the generator; pulling it out shaves the and-conjunction from
  the inner loop).

- Extract _classify_status to fold the 4xx / 5xx / other branches in
  _download_one into a single lookup-then-bucket line.

Result:
  download_all C(18) -> B(7)
  _download_one C(11) -> B(9)
  _iter_eligible_attempts (new) B(10)
  No grade C functions remain in media.py.

All 431 tests pass; ruff lint + format + mypy clean; behaviour unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@VGonPa VGonPa merged commit 5490bbc into develop May 24, 2026
1 check passed
@VGonPa VGonPa deleted the ws-radon-cleanup-media branch May 24, 2026 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant