Skip to content

refactor: drop the C901 mccabe ratchet 44 → 41 (behavior-neutral) - #524

Merged
wladerer merged 1 commit into
mainfrom
refactor/reduce-fn-complexity
Sep 26, 2026
Merged

wladerer merged 1 commit into
mainfrom
refactor/reduce-fn-complexity

Conversation

@wladerer

Copy link
Copy Markdown
Owner

What

Behavior-neutral decomposition of the two worst mccabe offenders in the driver layer, then a tightening of the ruff C901 ratchet.

_relax_nested (api/relax.py) was the single function pinning the ratchet: pure orchestration/glue (wires ASE optimizers to the calculator, no numerical hot path). Extracted into self-contained helpers, each typed + docstringed, no shared mutable state beyond atoms:

  • _apply_selective_dynamics(atoms, fixed) — FixScaled constraint assembly
  • _make_cell_filter(atoms, inp) — FrechetCellFilter for a variable-cell relax
  • _resolve_line_search(target, inp, verbose) — line-search selection + the cell/non-bfgs fallback cascade, returning (ls_opt, ls_fallback_reason)
  • _seed_initial_hessian(opt, atoms, inp, verbose) — the Lindh model-Hessian seed
  • _build_relax_summary(...) — the ~90-line summary-dict assembly

run (api/dispatch.py): the task=="scf" summary-block accretion (error_estimate / pdos / cohp / bader / work_function, plus the dispersion attach) moved into _scf_summary_extras(res, inp, summary, disp_block). The dispersion energy still folds into res before build_summary reads it — that call stays in run; only the block attachments move.

scf (scf/loop.py): added # === setup / iteration / finalize === section-banner comments to the 852-line body for navigability. Comments only — no code in the hot SCF function is moved or extracted.

pyproject.toml: lowered [tool.ruff.lint.mccabe] max-complexity 44 → 41 (the new worst offender, scf in scf/loop.py), tightening the ratchet.

Complexity (old → new)

function before after
_relax_nested 43 21
run (dispatch) 26 20

New ceiling 41 is the exact minimum: ruff --select C901 --config max-complexity=40 fails only on scf (41); at 41 there are zero violations.

Gates

  • uv run ruff check clean at max-complexity=41
  • uv run ty check unchanged (7 pre-existing optional-dep import errors, none in touched files)
  • uv run lint-imports clean (7/7 contracts)
  • make doc-refs clean

Validation

Relax + elastic + dispatch/scf-extras tests run on asus (standard tier) via the pueue queue. No test monkeypatches the extracted internals.

Note: this edits pyproject.toml's [tool.ruff.lint.mccabe]; a sibling PR edits [[tool.ty.overrides]] (different section) — a trivial clean rebase at merge is expected.

🤖 Generated with Claude Code

…extraction

Pure orchestration/glue extraction, no numerical path touched.

api/relax.py: decompose _relax_nested (C901 43 -> 21) into self-contained
helpers, each with a typed signature and docstring and no shared mutable state
beyond `atoms`:
  - _apply_selective_dynamics(atoms, fixed): FixScaled constraint assembly
  - _make_cell_filter(atoms, inp): FrechetCellFilter for a vc-relax
  - _resolve_line_search(target, inp, verbose): line-search selection + the
    cell/non-bfgs fallback cascade, returning (ls_opt, ls_fallback_reason)
  - _seed_initial_hessian(opt, atoms, inp, verbose): the Lindh model-Hessian seed
  - _build_relax_summary(...): the ~90-line summary-dict assembly

api/dispatch.py: extract the task=="scf" summary-block accretion into
_scf_summary_extras(res, inp, summary, disp_block) (run C901 26 -> 20). The
dispersion energy still folds into `res` before build_summary reads it (the
call stays in `run`); only the block attachments move into the helper.

scf/loop.py: add "# === setup / iteration / finalize ===" section-banner
comments to the 852-line scf() body for navigability. Comments only; no code
in the hot SCF function is moved or extracted.

pyproject.toml: lower [tool.ruff.lint.mccabe] max-complexity 44 -> 41 (the new
worst offender, scf in scf/loop.py), tightening the ratchet.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@wladerer
wladerer merged commit 27ac052 into main Sep 26, 2026
15 checks passed
@wladerer
wladerer deleted the refactor/reduce-fn-complexity branch September 26, 2026 20:54
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