Skip to content

scripts/repo_shape.py: four functions carry cognitive complexity above the 15 Sonar allows — the YAML tokeniser at 71, the naming classifier at 49, the referent resolver at 28 and the block-mapping parser at 17 #135

Closed

Description

@brettheap

Lane: openreposhape-2 (openRepoShape-2)

What is wrong. SonarCloud reports python:S3776 (Cognitive Complexity of
functions should not 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. Queried against main at
d682127:

$ curl -s 'https://sonarcloud.io/api/issues/search?componentKeys=opensoft_openRepoShape&rules=python:S3776&statuses=OPEN&ps=100'
scripts/repo_shape.py 360  Refactor this function to reduce its Cognitive Complexity from 71 to the 15 allowed.
scripts/repo_shape.py 469  Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed.
scripts/repo_shape.py 1015 Refactor this function to reduce its Cognitive Complexity from 28 to the 15 allowed.
scripts/repo_shape.py 1159 Refactor this function to reduce its Cognitive Complexity from 49 to the 15 allowed.
line function cc what it is
360 _tokenise 71 the YAML subset reader's line tokeniser — the largest single function in the repository
469 _parse_mapping 17 the block-mapping parser
1015 NamingPolicy.resolve_referent 28 how a <Domainx><Product> name reaches its referent, directly or through a recorded chain
1159 NamingPolicy.classify 49 the ordered classifier over contracts/repository-naming.yaml

_tokenise carries the whole of the block-scalar reader inline: a nested
line-swallowing loop, the folded-scalar fold, and the two chomping
indicators, on top of the tab check, the document-marker check, the -
peeling loop and the two refusals. classify carries seven decision branches
and builds each branch's --explain sentence in place. resolve_referent
carries four chain-shape checks, the per-link verification loop and the
plural-agreement note for the unverified count. None of them can be read
without holding the whole function in your head, which is exactly what the
rule measures.

Locally, complexipy -d low (which implements the same SonarSource
specification, and additionally counts comprehensions, so it reads at or
above Sonar) agrees: _tokenise 71, _parse_mapping 17,
NamingPolicy::resolve_referent 40, NamingPolicy::classify 51.

What would fix it. Split each of the four along the decisions it already
enumerates — one private helper per decision, in the order the function's own
docstring and comments list them — so that no behaviour changes at all: not a
message, not an exit code, not the order of two side effects, not an
exception type. Specifically:

  • _tokenise: lift the block-scalar reader (swallow, fold, chomp), the -
    peeling loop, the document-marker rule and the two refusals into named
    helpers, leaving a loop that reads as the line grammar it implements.
  • _parse_mapping: lift the value of a key: with nothing after the colon
    into its own helper.
  • NamingPolicy.resolve_referent: lift the chain-shape checks, the per-link
    verification and the unverified note out, keeping directly() and
    broken() where they are — they close over direct and the rendered
    chain.
  • NamingPolicy.classify: give each branch a helper that returns the
    answer's INGREDIENTS — (family_id, role_id, reason, matched_key) — so the
    single answer() closure that builds also_matches stays the one place a
    Classification is constructed.

The rule order that the rest of the standard depends on must be preserved
exactly: resolve_link_source's four rules (#114, #120, #126 assert rule 2's
environment override sits ahead of rule 3's sibling checkout),
checked_value / SAFE_PATH_RE (#119's accepted security disposition rests
on them), pin_source_env_name / PIN_SOURCE_ENV_PREFIX (imported by
tests/conftest.py), and NamingPolicy.classify / accepts_role, asserted
across tests/test_naming_policy.py, tests/test_shape_doctor.py and
tests/test_adopt_plan.py. No public name and no signature changes; the
module stays Python 3.9-safe and standard-library only.

scripts/repo_shape.py is digest-pinned: a change to it shows as
upstream-changed scripts/repo_shape.py in every project's update-shape.py check until an estate re-pin.

Activity

  1. brettheap commented on Sep 12, 2026

    @brettheap
    ContributorAuthor

    (lane openreposhape-2 (openRepoShape-2), 2026-09-12T03:32Z) CLAIMED — session 9c6378ea, on Brett Heap's word 'go, fan out on all of it'. Branch refactor/repo-shape-cognitive-complexity, worktree ~/projects/openRepoShape--s3776-repo-shape on Eagle, from origin/main d682127. Scope: the four python:S3776 functions in scripts/repo_shape.py and nothing else — no other file in the repository is touched by this lane.

  2. brettheap commented on Sep 12, 2026

    @brettheap
    ContributorAuthor

    (lane openreposhape-2 (openRepoShape-2), 2026-09-12T14:43Z) PULL REQUEST OPEN: #143 (head 81b0573).

  3. brettheap commented on Sep 12, 2026

    @brettheap
    ContributorAuthor

    (lane openreposhape-2 (openRepoShape-2), 2026-09-12T15:22:43Z) MERGED — #143 → c224971 (squash), on Brett Heap's word 'go, fan out on all of it, hold the doctor refactor'. scripts/repo_shape.py: _tokenise (Sonar 71, the largest in the repository), _parse_mapping (17), NamingPolicy.resolve_referent (28) and NamingPolicy.classify (49) now read 9, 10, 7 and 13, every helper at 8 or below; behaviour proven byte-identical by two differential harnesses against d682127 — 64 YAML cases and 174,240 classify/resolve_referent combinations with 0 differences; resolve_link_source's rule order, checked_value and the pin-source names untouched; the one digest-pinned module every validator imports, so every project reads it upstream-changed until the owed re-pin; PR-scoped Sonar 0, gate OK; Copilot one docstring nit taken then clean; Codex no major issues; 1136 passed / 1 skipped. Object complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions