Skip to content

refactor: decompose run_remove to reduce cyclomatic complexity#526

Open
kasperjunge wants to merge 2 commits into
mainfrom
refactor/run-remove-complexity
Open

refactor: decompose run_remove to reduce cyclomatic complexity#526
kasperjunge wants to merge 2 commits into
mainfrom
refactor/run-remove-complexity

Conversation

@kasperjunge

Copy link
Copy Markdown
Collaborator

Summary

First iteration of the radon complexity-reduction task. Decomposes run_remove (agr/commands/remove.py) — the codebase's worst complexity outlier — from CC 36 (grade E) down to CC 2 (A), with no behavior change.

The per-ref loop body is extracted into focused, individually testable helpers:

  • _RefRemoval dataclass — one ref's outcome (result + parallel candidate/kind lists for lockfile cleanup).
  • _resolve_dep — handle parse + identifier candidates + dependency lookup.
  • _remove_from_filesystem / _remove_leaf_from_filesystem — filesystem removal, fanning out to package children.
  • _nested_packages_to_remove / _transitive_leaves_to_remove / _resolve_package_cleanup — package child resolution (split so each stays ≤ B).
  • _uninstall_transitive_entries, _remove_from_config, _build_removal, _process_ref.

run_remove is now a thin loop + finalization. All pre-existing helpers reused; pure decomposition.

Tests

Added TestResolvePackageCleanup (transitive leaf, nested package, shared-child retention, no-lockfile) and TestProcessRef (not-found, leaf removal, package transitive scheduling) to tests/unit/test_remove.py. Existing run_remove integration tests retained as the behavior-preservation net.

Verification

  • uvx radon cc agr/commands/remove.py -s: no function above grade B.
  • uv run pytest: 1310 passed, 6 skipped.
  • uv run ruff check ., ruff format --check, uv run ty check: clean.
  • Smoke: agr --help, agr remove --help OK.

A LOG.md and implementation diary document the iteration. Task definition and remaining acceptance criteria (add.py / package.py / config.py hotspots, sync.py split, xenon CI gate) are tracked under tasks/2026-06-09-radon-complexity-reduction/.

🤖 Generated with Claude Code

Kasper Junge and others added 2 commits June 9, 2026 14:21
Defines the complexity-reduction task and acceptance criteria so the
refactor iterations have a shared target to build toward.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
run_remove was the worst complexity outlier (radon CC 36, grade E). Its
per-ref loop body is extracted into focused helpers (_resolve_dep,
_remove_from_filesystem, the _resolve_package_cleanup trio, _process_ref,
and a _RefRemoval result struct), leaving run_remove a thin loop. No
behavior change; all functions now grade <= B (run_remove itself A 2).

Adds seam-level tests for package child resolution and per-ref processing,
plus a LOG.md and implementation diary for the iteration.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@kasperjunge kasperjunge requested a review from IIMunchII June 9, 2026 12:24
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