Repository navigation
scripts/validate-repository-naming.py, render-cli-reference.py, bump-leg.py and family.py: six functions' cognitive complexity dropped to Sonar's limit of 15 - #145
Conversation
…ission ladder answers one question per helper, cognitive complexity 35 -> 2 Sonar's python:S3776 scored `_describe` at 35 against the 15 the rule allows (#136): the `--explain` row loop built each family's MATCH mark, role note and claim string inline, with a four-way if/elif ladder — a referent CLAIM, a DECLARED-ONLY note, an ADMITS note (itself a nested three-way ladder over what is or is not declared), and an UNAMBIGUOUS note — plus the CHAIN/WARNING/OVERLAP trailer, all in one body. Split into one helper per decision, in the file's own leading-underscore convention, each carrying a WHY docstring saying what it decides and that it was split from its parent for #136: `_no_family_lines` (the NO FAMILY early return), `_referent_claim_note`, `_declared_only_note`, `_unambiguous_note` and `_admission_row` (the four claim answers), `_family_claim_and_role_note` (which of the four answers this family gets), `_family_row` (one family's full line) and `_referent_lines` (the CHAIN/WARNING/OVERLAP trailer). `_describe` itself now only decides which lines to assemble, not how each one reads. Behaviour is byte-identical: every line, in the same order, from the same conditions — this is code motion, not a fix. `complexipy --max-complexity- allowed 15` now passes every function in the file this touches (`_describe` 2, its highest helper `_family_claim_and_role_note` 9); `python3 -m pytest tests/test_naming_policy.py -q` still passes all 162 tests, including the `--explain` assertions this function answers. Lane: openreposhape-2 (openRepoShape-2) Co-Authored-By: Claude Fable 5.1 <[email protected]>
…g_target what one leg is worth, cognitive complexity 18 -> 6 Sonar's python:S3776 scored `_targets_from_project` at 18 against the 15 the rule allows (#136): the loop that walks a `project.yaml`'s legs built each leg's `Target`, classified it, and appended its own findings in place, then fell through to the manifest-level `topic:` check after the loop — three questions folded into one body. `_leg_target` now answers the first one alone: one leg's `Target` (or `None`, with the "a leg has no `repository:`" finding, for a leg that cannot be classified at all) and the findings that leg's own name and chain earn. `_topic_finding` answers the second: whether the manifest's `topic:` agrees with its `id:`. `_targets_from_project` is left as the loop that collects both, plus the one topic finding after it — the same shape it always had, with the two questions it was answering asked elsewhere. Behaviour is byte-identical: the `targets` list and the `findings` list come back with the same elements in the same order for every case, since each leg still contributes to both lists in the same per-leg iteration this always ran. `complexipy --max-complexity-allowed 15` now passes `_targets_from_project` (6) and `_leg_target` (7); `python3 -m pytest tests/test_naming_policy.py -q` still passes all 162 tests, including the `--project` manifest cases this function answers. Lane: openreposhape-2 (openRepoShape-2) Co-Authored-By: Claude Fable 5.1 <[email protected]>
…g, target gathering and the classify loop to helpers, cognitive complexity 39 -> 12 Sonar's python:S3776 scored `main` at 39 against the 15 the rule allows (#136): parsing `--link-source`, reading targets from three sources (the command line, `--project`, stdin), the classify-and-print loop over every target, and the final WARNING/FINDING/exit-code trailer were all one body. Split into `_link_sources_from_args` (the `--link-source` parsing loop), `_targets_from_stdin` (the stdin fallback), `_print_classification` (one plain-listing line), `_classify_targets` (the loop that classifies every target and returns `(unclassified, warnings)`) and `_emit_reports` (the WARNING/FINDING lines and the exit code). `main` is left as the sequence these already were — parse, load the policy, gather targets from every source in the same order, classify, report — with each step's own decisions asked elsewhere. The `--project` `Refusal` handling stays inline in `main`, unchanged, because it is `main` that must print and `return 2` on it, exactly as the policy-load `Refusal` beside it already does. Behaviour is byte-identical: the same argument definitions, the same target-gathering order (CLI names, then `--project`, then stdin only if still empty), the same lines and the same exit codes. The one structural change is folding two always-mutually-exclusive `if found: ... / if not found: ...` statements in the classify loop into one `if/else`, which decides nothing differently. `complexipy --max-complexity-allowed 15` now passes every function in this file (`main` 12, its busiest helper `_classify_targets` 10); `python3 -m pytest tests/test_naming_policy.py -q` still passes all 162 tests, and `python3 -m pytest tests/test_cli_reference.py -q` (13 passed) confirms no `--help` text moved. Lane: openreposhape-2 (openRepoShape-2) Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ion instead of every tool's in one body, cognitive complexity 16 -> 6 Sonar's python:S3776 scored `render` at 16 against the 15 the rule allows (#136): one function built the active tool groups (a comprehension nested inside a comprehension), the table of contents, and then, for every group and every tool in it, the heading, the installed-as note, the tool's own note, its captured `--help`, and every subcommand section it lists — four questions folded into two nested loops. `_active_tools` answers which of a group's tools this rendering keeps, once, instead of as a comprehension nested inside the one that walks `GROUPS`. `_tool_section` answers what one tool's whole section says; `_subcommand_sections`, the part of that answer that lists a dispatcher's own subcommands. `_refuse_host_paths` is Rule 1's check, split out so it reads as its own question on the finished document rather than the last few lines of assembling it. `render` itself is now the outline: which groups, which sections, in which order. Behaviour is byte-identical: `python3 scripts/render-cli-reference.py --check` still says `[ok] docs/cli.md is what the generator produces`, so the committed file needed no regeneration. `complexipy --max-complexity-allowed 15` now passes every function in the file (`render` 6); `python3 -m pytest tests/test_cli_reference.py -q` still passes all 13 tests. Lane: openreposhape-2 (openRepoShape-2) Co-Authored-By: Claude Fable 5.1 <[email protected]>
…e-or-roll-back to named helpers, cognitive complexity 28 -> 5 Sonar's python:S3776 scored `cmd_bump` at 28 against the 15 the rule allows (#136), also carrying flake8's own `# noqa: C901` for the same shape: one function validated `--to`, the root's cleanliness, the branch, the submodule and the recorded gitlink (five refusal checks), reported what it found, then rewrote the pin and every workflow file inside a try/except that rolls every byte back on failure — the whole leg-advance procedure in one body. Each refusal is now a named question, in the SAME ORDER the checks always ran: `_checked_target_commit` (`--to` is 40 hex), `_ensure_root_clean` (tracked changes), `_branch_to_bump_from` (detached, or the tracking branch), `_initialized_submodule`, `_recorded_leg_commit`, and, once `fetch_leg` and the digest are already computed, exactly where the original checked it, `_leg_pin_path`. `_print_workflow_plan` is the workflow-count report. `_apply_rewrite` is the checkout, the pin and workflow rewrite, the staging, the validators, and the rollback on a caught `Refusal` or `CommandFailed` — moved whole, because splitting the try from its except would be splitting a rollback from the write it undoes. `cmd_bump` is now the sequence: gather the facts, refuse on the first one that is wrong, report, and either stop (nothing to do, `--dry-run`) or hand the rewrite to `_apply_rewrite` and make the commit. Behaviour is byte-identical: every refusal keeps its id and its exact remediation text, every git command this runs and its order are unchanged (checkout detached, write, stage, validate; on failure restore bytes, restore the leg's placement, reset the index — never reordered), and `main`'s two `except` clauses still catch whatever this raises, whichever helper raised it. `complexipy --max-complexity-allowed 15` now passes every function in the file (`cmd_bump` 5, its largest helper `_apply_rewrite` 11); `python3 -m pytest tests/test_bump_leg.py -q` still passes all 16 tests, and `tests/test_repo_hygiene.py` still passes all 48. Lane: openreposhape-2 (openRepoShape-2) Co-Authored-By: Claude Fable 5.1 <[email protected]>
…a named helper, cognitive complexity 51 -> 2 Sonar's python:S3776 scored `cmd_init` at 51 against the 15 the rule allows (#136), the highest of the six functions in this cluster, also carrying flake8's own `# noqa: C901` for the same shape: one function validated every flag, computed who is creating the family and where its remote and its holder land, printed the plan, refused on a non-empty target or a live remote, created the remote (bare or `gh repo create`), materialized and committed the holder, pushed it, set its topic, and printed the NEXT STEPS block — the whole `init` procedure, start to finish, in one body. Split into one helper per phase, called in the SAME ORDER the phases always ran: `_checked_family_identity` (the name and id checks, still mutating `args.org`/`args.tracking_branch` as it always did), `_creator_name`, `_holder_remote`, `_shape_snapshot`, `_template_values`, `_checked_landing` (the `--into`/`--work-dir` refusal plus `_landing`), `_print_init_plan` (what `--dry-run` shows and what a real run shows first), `_prepare_landing_dir` (the target-exists refusal), `_checked_reuse` (the remote-exists refusal, restructured as a guard clause: `if empty is None: return False` in place of the same body nested inside `if empty is not None`), `_create_holder_remote`, `_create_holder_worktree` (materialize, commit, push — returning `None` on success or the int `cmd_init` should return right now, since a push failure is handled here with its own message and exit code rather than left for `main`'s generic one), `_finish_holder_remote` and `_print_next_steps`. `cmd_init` is now the outline: gather the facts, print the plan, stop early (`--dry-run`, a push failure), or finish. Behaviour is byte-identical: every refusal keeps its id and its exact remediation text, every printed line and its exact wording (`values`'s own `FAMILY_ID`/`VISIBILITY` entries are read where the removed local variables used to be, since the dict already carried the same values), and every git and `gh` command and its order are unchanged. `--trailer` on `bump` (#112) and the `GITMODULES` handling in `add`/`remove` (#125) are untouched — this commit is `cmd_init` only. `complexipy --max-complexity-allowed 15` now passes every function in the file (`cmd_init` 2, its largest helper `_checked_reuse` 7); `python3 -m pytest tests/test_family.py -q` still passes all 74 tests, and `tests/test_repo_hygiene.py` still passes all 48. Lane: openreposhape-2 (openRepoShape-2) Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
@codex review |
There was a problem hiding this comment.
Sorry @brettheap, you've used your own review budget of 250,000 diff characters for the last 7 days.
You can request another review in 3 days and 13 hours by commenting @sourcery-ai review. Upgrade to get a review now.
Reviewer's GuideRefactors six high-cognitive-complexity functions across four scripts into focused helpers, reducing all measured complexity to Sonar's limit while intentionally preserving behavior, output, ordering, error handling, and generated documentation. Sequence diagram for the refactored leg bump workflowsequenceDiagram
participant C as cmd_bump
participant V as Validation helpers
participant G as Git and leg operations
participant R as Rewrite and rollback
C->>V: _checked_target_commit
C->>V: _ensure_root_clean
C->>V: _branch_to_bump_from
C->>V: _initialized_submodule
C->>V: _recorded_leg_commit
C->>G: fetch_leg
C->>G: tree_digest
C->>V: _leg_pin_path
C->>G: plan_workflows
C->>V: _print_workflow_plan
alt dry run or nothing to do
C-->>C: return status
else rewrite requested
C->>R: _apply_rewrite
R->>G: checkout, rewrite, stage, validate
alt validation or command failure
R->>R: rollback and re-raise
else success
R-->>C: complete rewrite
end
end
Flow diagram for the extracted repository naming validation pipelineflowchart TD
A["main"] --> B["_link_sources_from_args"]
A --> C["_targets_from_project"]
A --> D["_targets_from_stdin"]
C --> E["_leg_target"]
C --> F["_topic_finding"]
A --> G["_classify_targets"]
G --> H["_describe"]
H --> I["_family_row"]
H --> J["_referent_lines"]
A --> K["_emit_reports"]
Flow diagram for the refactored family initialization phasesflowchart TD
A["cmd_init"] --> B["_checked_family_identity"]
B --> C["_creator_name"]
C --> D["_holder_remote"]
D --> E["_shape_snapshot"]
E --> F["_template_values"]
F --> G["_checked_landing"]
G --> H["_print_init_plan"]
H --> I{"--dry-run?"}
I -- yes --> J["return 0"]
I -- no --> K["_prepare_landing_dir"]
K --> L["_checked_reuse"]
L --> M["_create_holder_remote"]
M --> N["_create_holder_worktree"]
N --> O["_finish_holder_remote"]
O --> P["_print_next_steps"]
Flow diagram for the extracted CLI reference rendering stagesflowchart TD
A["render"] --> B["_active_tools"]
B --> C["_tool_section"]
C --> D["capture_help"]
C --> E["_subcommand_sections"]
E --> D
A --> F["_refuse_host_paths"]
F --> G["Rendered CLI document"]
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
🟢 Approval recommended
Only minor documentation nits remain; no blocking issues were identified.
Pull request overview
Refactors six high-complexity functions across four scripts into focused helpers while preserving behavior, output, and exit codes.
Changes:
- Decomposes naming validation and reporting logic.
- Extracts CLI rendering, leg bumping, and family initialization phases.
- Retains existing validation and generated documentation behavior.
File summaries
| File | Summary | Review comments |
|---|---|---|
scripts/validate-repository-naming.py |
Extracted classification and reporting helpers. | Nit (2 votes): fix the grammatical typo in the role_note docstring. |
scripts/render-cli-reference.py |
Extracted tool and subcommand rendering helpers. | None. |
scripts/family.py |
Extracted family initialization phases. | Nit (1 vote): clarify the materialize_family_root docstring wording. |
scripts/bump-leg.py |
Extracted validation, planning, and rollback helpers. | Nit (1 vote): clarify the “commits on” wording. |
Review details
Suppressed comments (2)
scripts/bump-leg.py:483
- The helper description uses the nonstandard “commits on” construction; phrase it as “the branch on which the bump commits” so the refusal behavior is described clearly.
"""The branch the bump commits on, refused if there is none or it is
the project's own tracking branch.
scripts/family.py:515
- This docstring is missing a relative pronoun and reads as if
materialize_family_rootwere the values: describe these as the values that the function substitutes into the template.
"""The substitution values `materialize_family_root` fills the
holder's template with.
- Files reviewed: 4/4 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
scripts/validate-repository-naming.py: `_admission_row`'s docstring had a stray "in" — "`role_note` in is the plain note" — read as broken English; fixed to "`role_note` is the plain note". scripts/bump-leg.py: `_branch_to_bump_from`'s docstring read "The branch the bump commits on"; reworded to "The branch on which the bump commits" per the review's suggestion, so the sentence does not lean on a trailing preposition right before its comma. scripts/family.py: `_template_values`'s docstring read "The substitution values `materialize_family_root` fills the holder's template with", which reads as though `materialize_family_root` were the values; reworded to "The values `materialize_family_root` substitutes into the holder's template". Docstrings only — no code, no behaviour, no test changes. Lane: openreposhape-2 (openRepoShape-2) Co-Authored-By: Claude Fable 5.1 <[email protected]>
|



Lane: openreposhape-2 (openRepoShape-2)
Claim: #136 (comment)
Closes #136.
What was wrong
SonarCloud's
python:S3776(cognitive complexity, limit 15) had six openissues against four standard-side scripts, none of them started before this
branch:
main(d682127)scripts/validate-repository-naming.py_describescripts/validate-repository-naming.py_targets_from_projectscripts/validate-repository-naming.pymainscripts/render-cli-reference.pyrenderscripts/bump-leg.pycmd_bumpscripts/family.pycmd_initEach one folded several distinct decisions into a single body:
_describe's--explainrow was a four-way claim/admission ladder run per family;_targets_from_projectfolded per-leg classification and the manifest'stopic check into one loop;
maindid argument handling, three targetsources and the classify-and-print loop in one function;
rendercomposedevery tool's whole documentation section in one body;
cmd_bumpandcmd_init(both already carrying flake8's own# noqa: C901for the sameshape) each held their tool's entire refusal ladder, git plumbing and commit
in one body.
What landed
One commit per function, each an extraction only — no message text, exit
code, output order, file written or exception type changes anywhere:
_describe--explainnote (_no_family_lines,_referent_claim_note,_declared_only_note,_unambiguous_note,_admission_note/_admission_row),_family_claim_and_role_note(which note a family gets),_family_row(one family's line),_referent_lines(the CHAIN/WARNING/OVERLAP trailer)_targets_from_project_leg_target(one leg'sTargetand findings) and_topic_finding(the manifest-level check), called from the same loop shapemain_link_sources_from_args,_targets_from_stdin,_print_classification,_classify_targets(the loop),_emit_reports(the WARNING/FINDING trailer and exit code)render_active_tools(replacing a comprehension nested inside a comprehension),_tool_section,_subcommand_sections,_refuse_host_pathscmd_bump_checked_target_commit,_ensure_root_clean,_branch_to_bump_from,_initialized_submodule,_recorded_leg_commit,_leg_pin_path),_print_workflow_plan, and_apply_rewrite(the whole checkout/rewrite/stage/validate-or-roll-back try/except, moved as one unit since splitting the try from its except would split a rollback from the write it undoes)cmd_init_checked_family_identity,_creator_name,_holder_remote,_shape_snapshot,_template_values,_checked_landing,_print_init_plan,_prepare_landing_dir,_checked_reuse(restructured to a guard clause:if empty is None: return False, same result as the same body nested one level deeper),_create_holder_remote,_create_holder_worktree(returnsNoneon success or the exit codecmd_initshould return right now — a push failure is still reported with its own message and exit code, notmain's generic one),_finish_holder_remote,_print_next_stepscomplexipy --max-complexity-allowed 15against all four files now reportsevery function passing, highest at 12 (
main) — complexipy's own numberssit at or above Sonar's throughout this cluster (e.g.
_describe38 vsSonar's 35,
cmd_bump29 vs 28), so the margin below 15 is real headroom, notan artifact of the two tools disagreeing in the lenient direction.
Digest-pinned file touched:
scripts/validate-repository-naming.py(ships into every scaffolded project; still Python 3.9-safe — no
match, noruntime
X | Y, and it already carriesfrom __future__ import annotationsso the deferred-evaluation type hints on the new helpers cost nothing at
import time). This shows as
upstream-changedin every project'supdate-shape.py checkuntil an estate re-pin, which is not this PR's to do.No follow-up bugs were noticed or left. Every change was pure code
motion — a helper extracted with the exact lines, exact conditions, and
exact order its parent already had.
How it was proven
Local, after every commit, from this worktree:
python3 -m pytest tests/test_naming_policy.py -q— 162 passed, after eachof the three
validate-repository-naming.pycommits.python3 -m pytest tests/test_cli_reference.py -q— 13 passed, both as asanity check on the
validate-repository-naming.pycommits (no--helptext lives there) and after the
render-cli-reference.pycommit itself;python3 scripts/render-cli-reference.py --checksays[ok] docs/cli.md is what the generator produces— no regeneration wasneeded, confirming the rendered document is byte-identical.
python3 -m pytest tests/test_bump_leg.py -q— 16 passed, after thebump-leg.pycommit.python3 -m pytest tests/test_family.py -q— 74 passed, after thefamily.pycommit.python3 -m pytest tests/test_repo_hygiene.py -q— 48 passed, after thebump-leg.pyandfamily.pycommits.Full suite, in two bounded chunks from a neutral cwd on this worktree at
head
73e7201:1136 passed, 1 skipped in total — the same count the brief for this
cluster names as today's baseline on
main. The one skip is thepre-existing, location-dependent naming-policy test unaffected by this
branch.
complexipy --max-complexity-allowed 15against all four touched files: seethe table above — every function passes, none newly introduced above the
line.
🤖 Generated with Claude Code
Summary by Sourcery
Reduce cognitive complexity in six repository tooling functions without changing their behavior.
Enhancements:
Tests: