Repository navigation
scripts/repo_shape.py: the four functions Sonar flagged for cognitive complexity are split along the decisions they already enumerate, and the largest of them reads as the line grammar it implements (#135) - #143
Conversation
…t implements, one named helper per step, and its cognitive complexity is 9 rather than 71 (#135) `_tokenise` was the largest function in the repository by SonarSource's cognitive-complexity measure — 71 against a limit of 15 (`python:S3776`) — because it carried the whole block-scalar reader inline: a nested line-swallowing loop, the folded-scalar fold and both chomping indicators, on top of the tab refusal, the document-marker rule, the `- ` peeling loop and the explicit-key and merge-key refusals. The rule that function implements is the ORDER those steps are decided in, so each step is now a helper named after the question it answers — `_significant_text`, `_document_marker`, `_peel_dashes`, `_refuse_unsupported`, `_opens_block_scalar`, `_block_scalar_body`, `_fold_block_body` and `_block_scalar_text` — and the loop that remains is the order itself, at complexity 9. Every helper carries a WHY docstring saying what it decides and that it was split from `_tokenise` for this issue. The block-scalar header pattern moves to a compiled `_BLOCK_HEADER_RE` beside the two functions that read the body it introduces, matching how `_DASH_RE` and every other pattern in this file are already spelled. NOTHING ABOUT THE OUTPUT CHANGES. Proven by a differential harness that parses every `.yaml` and `.yml` file in the tree plus fifty crafted edge cases — both chomping indicators, an explicit indentation digit, a folded body ending blank, `>= 3` as an ordinary scalar, a block scalar at end of file, peeled dash chains, two documents, a tab in the indentation, anchors, aliases and tags — through this reader and through `origin/main`'s, and compares the value or the exception type and message: 64 cases, 0 differences. `scripts/repo_shape.py` is digest-pinned, so this shows as `upstream-changed scripts/repo_shape.py` in every project's `update-shape.py check` until an estate re-pin. Lane: openreposhape-2 (openRepoShape-2) Co-Authored-By: Claude Fable 5.1 <[email protected]>
…what a bare `key:` is worth instead of spelling the three answers inline, and its cognitive complexity is 10 rather than 17 (#135) `_parse_mapping` was at 17 against `python:S3776`'s limit of 15, and the whole of the excess was one nested ladder: a `key:` with nothing after the colon is a sequence at the key's own column or deeper, a mapping strictly deeper than it, or null — three answers to ONE question, indented inside the loop that reads the rest of the mapping. That question is now `_nested_value`, whose docstring says why the two admitted columns differ: `- ` markers are conventionally written flush with the key they belong to, so a sequence is admitted at `>= indent`, while a mapping at the key's own column is a SIBLING KEY and not a value. The loop that remains reads as the four shapes a mapping entry can take. NOTHING ABOUT THE OUTPUT CHANGES — the same comparisons in the same order, moved. Proven by the same differential harness as the previous commit, which parses every `.yaml` and `.yml` file in the tree plus fifty crafted edge cases through this reader and through `origin/main`'s and compares the value or the exception type and message: 64 cases, 0 differences. Lane: openreposhape-2 (openRepoShape-2) Co-Authored-By: Claude Fable 5.1 <[email protected]>
…what it used to answer inline, and its cognitive complexity is 7 rather than 28 (#135) `NamingPolicy.resolve_referent` was at 28 against `python:S3776`'s limit of 15. It answered five separate questions in one body: which pins a manifest declares, which links a chain records, whether the chain's SHAPE holds (four checks), what each link's own tree says about the next one, and how to word the note when a link could not be read. Each is now its own function, named for the question — `_pinned_basenames`, `_chain_links`, `_referent_in`, `_link_declarations`, `_chain_link_findings`, `_unverified_note`, and the method `_chain_shape_problem`, which needs the policy's own `chain_rule()`. NONE OF THE MODULE-LEVEL ONES KNOWS WHAT A `ReferentResolution` IS, deliberately: `directly()` and `broken()` stay where they are, so a reader who wants to know how a STATUS is chosen still finds the whole of that in one function. `_chain_shape_problem` asks its four questions in the order `resolve_referent` asked them, and returns the `detail` a `broken` resolution is worded with, so the message still NAMES the link that broke. The rule order that the rest of the standard rests on is untouched: a recorded chain is still read before the direct pin, a broken chain beside a direct pin still answers `direct` with the chain reported as a warning, and an unreadable link is still `declared-unverified` rather than a failure. NOTHING ABOUT THE OUTPUT CHANGES. Proven by a differential harness that runs `resolve_referent`, `classify`, `matches`, `accepts_role` and `descendant_referents` through this module and through `origin/main`'s over 174,240 combinations of 31 names x 6 declared roles x 11 pin sets x 11 recorded chains x 8 link-pin mappings — including a chain longer than the policy admits, a repeated link, a chain beginning off the pins, one ending at a referent the name could not have, a link declaring nothing, a link that did not answer, and the singular and plural unverified notes — comparing status, referent, chain, reason, warnings and unverified entries: 0 differences. Lane: openreposhape-2 (openRepoShape-2) Co-Authored-By: Claude Fable 5.1 <[email protected]>
…n answer's INGREDIENTS to the one closure that builds a Classification, and its cognitive complexity is 13 rather than 49 (#135) `NamingPolicy.classify` was at 49 against `python:S3776`'s limit of 15. It decided seven branches in one body and built each branch's `--explain` sentence in place, so the ORDER of the decision — which is the whole rule this classifier implements, and which its docstring enumerates in seven numbered lines — could not be read without reading the wording of every answer on the way past. Each branch is now a helper that returns an answer's INGREDIENTS, `(family_id, role_id, reason, matched_key)`: `_unambiguous_answer` and `_descendant_answer` as methods, because they read the family's own `admits_declared_role:`, and `_leg_answer`, `_descendant_reason`, `_forms_by_family`, `_leg_roles` and `_other_forms` as functions. NONE OF THEM BUILDS A `Classification`. `classify`'s own `answer()` closure stays the single place `also_matches` is computed, because a branch that built its own classification would be a second place for that record to be got wrong — and `also_matches` contradicting the family a name was classified INTO is precisely the failure `matched_key` exists to prevent. What remains in `classify` is the seven-step order its docstring already describes. Two comments became docstrings where the code they annotated became a function of its own: the 2026-09-05 neutral-product ruling and why its `or ()` fallback is stricter than the descendant branch's `or ("assembly",)` now sit on `_unambiguous_answer`, the 2026-09-02 descendant-carries-legs ruling on `_descendant_answer`, and NAME THE LINK on `_leg_answer`. The role-clause sentence a descendant's reason ends with was spelled twice, in the chain branch and the direct-pin branch; `_descendant_reason` reaches it by one path and spells it once. NOTHING ABOUT THE OUTPUT CHANGES. Proven by the same differential harness as the previous commit, running `classify`, `resolve_referent`, `matches`, `accepts_role` and `descendant_referents` through this module and through `origin/main`'s over 174,240 combinations of 31 names x 6 declared roles x 11 pin sets x 11 recorded chains x 8 link-pin mappings, comparing the 2-tuple, the family, the role, `also_matches`, the reason sentence and the whole referent resolution: 0 differences. 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 2 days and 2 hours by commenting @sourcery-ai review. Upgrade to get a review now.
Reviewer's GuideThis PR restructures the four Sonar-flagged functions in the digest-pinned Sequence diagram for referent chain resolutionsequenceDiagram
participant C as classify
participant R as NamingPolicy.resolve_referent
participant S as _chain_shape_problem
participant F as _chain_link_findings
participant N as _unverified_note
C->>R: resolve_referent(name, declared_pins, referent_chain, link_pins)
R->>R: _pinned_basenames(declared_pins)
R->>R: _referent_in(referents, pins)
R->>R: _chain_links(referent_chain)
alt chain has invalid shape
R->>S: _chain_shape_problem(name, chain, pins, referents)
S-->>R: problem detail
else chain shape is valid
R->>F: _chain_link_findings(chain, link_pins)
F-->>R: contradiction, warnings, unverified
R->>N: _unverified_note(unverified)
N-->>R: reason suffix
end
R-->>C: ReferentResolution
Flow diagram for the refactored YAML line grammarflowchart TD
A[_tokenise] --> B[_significant_text]
B --> C[_document_marker]
C --> D[_peel_dashes]
D --> E[_refuse_unsupported]
E --> F{_opens_block_scalar}
F -->|yes| G[_block_scalar_body]
G --> H[_block_scalar_text]
H --> I[Append block record]
F -->|no| J[Append ordinary record]
Flow diagram for naming classification decision orderflowchart TD
A[classify] --> B[matches]
B --> C[_forms_by_family]
C --> D[_leg_roles]
D --> E{_unambiguous_answer}
E -->|answer| Z[answer closure builds Classification]
E -->|none| F{Declared-only form requested}
F -->|yes| Z
F -->|no| G{Claimed descendant reached or referent not required}
G -->|yes| H[_descendant_answer]
H --> I[_descendant_reason]
I --> Z
G -->|no| J[_leg_answer]
J -->|answer| Z
J -->|none| K[Fallback classification]
K --> Z
Z --> L[_other_forms computes also_matches]
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.
🔵 Needs a closer look
A documentation nit remains, and the broad refactor warrants human review.
Pull request overview
Refactors four high-complexity functions in scripts/repo_shape.py into focused helpers while preserving behavior and public APIs.
Changes:
- Extracted YAML tokenization and block-scalar handling.
- Simplified mapping and referent resolution.
- Split naming classification branches.
- Preserved rule ordering and Python 3.9 compatibility.
File summaries
| File | Summary | Review note |
|---|---|---|
scripts/repo_shape.py |
Refactors parser and naming-policy complexity. | Nit (1 vote): Update the helper docstring to describe both supported paths. |
Review details
Suppressed comments (1)
scripts/repo_shape.py:1595
- This helper is also called when the descendant policy does not require a referent (
classifypassesdeclared=Nonein that case), and_descendant_reasonreturns the “this policy does not require a referent” explanation. The docstring therefore describes only one of the helper's two supported paths; please state that it handles either a reached referent or a policy that does not require one.
"""The answer for a descendant claim that REACHED its referent.
- Files reviewed: 1/1 changed files
- Comments generated: 0
- 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. Chef's kiss. 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". |
…ths that reach it, not only the one where a referent was reached (#135) Copilot's review of #143 is right. The helper's first line said "a descendant claim that REACHED its referent", but `classify` also enters this branch when the policy's descendant family does not require a referent at all — `if claimed and (declared or not self.requires_referent("domain-descendant"))` — and on that path `declared` is None and `_descendant_reason` returns "descendant form (this policy does not require a referent)". The docstring described one of the two supported paths and left a reader to discover the other from the caller. A comment-only change; no code, no behaviour, no test. Lane: openreposhape-2 (openRepoShape-2) Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Lane: openreposhape-2 (openRepoShape-2) Copilot round 1, one finding, taken — 57feee5. Taken. Sourcery declined (own 7-day review budget spent, 2 days 2 hours to reset) and Codex found no major issues on 81b0573 — both before this commit, whose only change is that docstring. The PR-scoped Sonar query at 81b0573 returned 0 open issues of any rule, the four |
|
|
Lane: openreposhape-2 (openRepoShape-2) Copilot round 2 at 57feee5: 0 new comments, no findings — the review body asks only for final human review of a broad refactor, which is a disposition and not something to fix. No inline comment threads exist on this PR at all ( Re-verified at 57feee5: PR-scoped Sonar 0 open issues of any rule, gate OK on all five conditions; CI green on ubuntu (3m29s), macOS (6m45s) and Windows (12m46s). Suite from the worktree, in bounded chunks: 1136 passed, 1 skipped. Two rounds, one finding, taken. Nothing further from this lane — the orchestrator merges. 🤖 Generated with Claude Code |



Lane: openreposhape-2 (openRepoShape-2)
Claim: #135 (comment)
Closes #135.
What was wrong
SonarCloud reported
python:S3776— Cognitive Complexity of functions shouldnot be too high, limit 15 — against four functions in
scripts/repo_shape.py,the digest-pinned, dependency-free module every validator and every shipped
script imports. At
maind682127:_tokenisewas the largest function in the repository by that measure. Itcarried the whole block-scalar reader inline — a nested line-swallowing loop,
the folded-scalar fold and both chomping indicators — on top of the tab
refusal, the document-marker rule, the
-peeling loop and two morerefusals.
classifydecided seven branches in one body and built eachbranch's
--explainsentence in place, so the ORDER of the decision, which isthe whole rule it implements and which its own docstring enumerates in seven
numbered lines, could not be read without reading the wording of every answer
on the way past.
resolve_referentanswered five separate questions at once,and
_parse_mappingspelled the three answers to "what is a barekey:worth" as a nested ladder inside its loop.
What landed
scripts/repo_shape.pyand nothing else — it is DIGEST-PINNED, so thisshows as
upstream-changed scripts/repo_shape.pyin every project'supdate-shape.py checkuntil an estate re-pin. No other file in therepository is touched; no public name, signature or default changes; the
module stays Python 3.9-safe and standard-library only.
Four commits, one per flagged function. Each split follows the decisions the
function already enumerated, in the order it enumerated them, and each new
helper carries a WHY docstring in the file's voice saying what it decides and
that it was split from its parent for this issue.
_tokenise_significant_text(tab refusal, blank and comment lines),_document_marker,_peel_dashes,_refuse_unsupported,_opens_block_scalar,_block_scalar_body,_fold_block_body,_block_scalar_text. The header pattern becomes a compiled_BLOCK_HEADER_RE, spelled the way_DASH_REand every other pattern in this file already is._parse_mappingkey:with nothing after the colon worth" becomes_nested_value, whose docstring says why a sequence is admitted at the key's own column and a mapping is not.NamingPolicy.resolve_referent_pinned_basenames,_chain_links,_referent_in,_link_declarations,_chain_link_findings,_unverified_note, and the method_chain_shape_problem, which needs the policy's ownchain_rule()and asks its four questions in the order the parent asked them.directly()andbroken()stay where they are, so a reader who wants to know how a STATUS is chosen still finds the whole of that in one function.NamingPolicy.classify(family_id, role_id, reason, matched_key):_unambiguous_answerand_descendant_answeras methods (they read the family's ownadmits_declared_role:), plus_leg_answer,_descendant_reason,_forms_by_family,_leg_rolesand_other_forms. None of them builds aClassification—classify's ownanswer()closure stays the single placealso_matchesis computed.cc beforeis SonarCloud's own number for that line;cc afteriscomplexipy's, which implements the same SonarSource specification andadditionally counts comprehensions, so it reads at or above Sonar's. The
highest cognitive complexity left in the file is 9 for a helper
(
_tokeniseitself), and every new helper is at 8 or below.Three comments became docstrings, because the code they annotated became a
function of its own: the 2026-09-05 neutral-product ruling and why its
or ()fallback is deliberately stricter than the descendant branch's
or ("assembly",)now sit on_unambiguous_answer; the 2026-09-02descendant-carries-legs ruling on
_descendant_answer; NAME THE LINK on_leg_answer. The role clause a descendant's reason ends with was spelledtwice, in the chain branch and the direct-pin branch;
_descendant_reasonreaches it by one path and spells it once.
The rule orders the rest of the standard rests on are byte-identical.
resolve_link_source's four rules are untouched — rule 2's environmentoverride still sits ahead of rule 3's sibling checkout (#114, #120, #126).
checked_valueandSAFE_PATH_REare untouched (#119's accepted securitydisposition).
pin_source_env_nameandPIN_SOURCE_ENV_PREFIX, whichtests/conftest.pyimports, are untouched. A recorded chain is still readbefore the direct pin; a broken chain beside a direct pin still answers
directwith the chain reported as a warning; an unreadable link is stilldeclared-unverifiedrather than a failure.How it was proven
Two differential harnesses against
origin/main's module, both loadingthis module and d682127's side by side in one interpreter and comparing the
returned value — or the exception type AND message — for every input:
.yamland.ymlfile in the tree plus fiftycrafted edge cases (both chomping indicators, an explicit indentation
digit, a folded body ending blank, a folded body beginning blank,
>= 3asan ordinary scalar, a block scalar at end of file, blank lines inside and
after a block body, peeled dash chains, two documents,
..., a tab in theindentation, a tab inside a quoted scalar, an escaped Windows path, anchors,
aliases, tags, explicit keys, merge keys, CRLF): 64 cases, 0 differences.
recorded chains x 8 link-pin mappings — 174,240 combinations, 0
differences — comparing
classify(the 2-tuple,family,role,also_matchesand thereasonsentence),resolve_referent(status,referent, chain, reason, warnings, unverified,
reached,by_chain),matches,accepts_roleanddescendant_referents. The inputs include achain longer than the policy admits, a repeated link, a chain beginning off
the pins, one ending at a referent the name could not have, a link declaring
nothing, a link that did not answer, and both the singular and the plural
unverified notes.
The suite, from this worktree, in the foreground, in bounded chunks:
1136 passed, 1 skipped —
tests/test_[a-f]*.py328 passed 1 skipped;tests/test_[g-r]*.py387 passed; the four scaffold/setup files 186 passed;test_shape_doctor.py+test_update_shape*.py+test_windows_paths.py235passed. That is the count
maingives today. The four files the issue namesas load-bearing were run after every individual split as well:
tests/test_naming_policy.py(162),tests/test_shape_doctor.py(144),tests/test_adopt_plan.py,tests/test_windows_paths.py.No tool's
--helptext changed —scripts/repo_shape.pyhas no command line— so
docs/cli.mdis unchanged andtests/test_cli_reference.pypassesunregenerated.
The PR-scoped SonarCloud query, at head 57feee5:
No
python:S3776in this file and no new issue of any other rule — noduplicated literal, no unused parameter, nothing in a new helper. The quality
gate is OK on all five conditions (reliability 1, security 1,
maintainability 1, duplicated lines 0.0%, security hotspots reviewed 100%).
CI green on all three platforms at that commit: ubuntu 3m29s, macOS 6m45s,
Windows 12m46s.
Reviewers. Codex: no major issues (at 81b0573). Sourcery: declined, its
own 7-day review budget is spent. Copilot: two rounds — round 1 raised one
documentation nit, taken in 57feee5; round 2 generated no new comments.
Left for later, deliberately
python:S1845atscripts/repo_shape.py:831(Rename method "reached" to prevent any misunderstanding/clash with field "REACHED") is open atmainand stays open here.ReferentResolution.reachedis read by nameoutside this module, so renaming it is a behaviour change and not this
issue's; no issue is filed for it yet.
_flowreads 18 undercomplexipyand is NOT flagged by Sonar, whichcounts no comprehension and puts it at or below 15. It was not in the four
this issue claims and is left alone rather than refactored on a local
tool's number.
working before.
🤖 Generated with Claude Code