Skip to content

feat(python): add an opinionated high-complexity guide#73

Merged
devill merged 2 commits into
habit-hooks:mainfrom
LiinaSuoniemi:python-high-complexity-guide
Jul 2, 2026
Merged

feat(python): add an opinionated high-complexity guide#73
devill merged 2 commits into
habit-hooks:mainfrom
LiinaSuoniemi:python-high-complexity-guide

Conversation

@LiinaSuoniemi

@LiinaSuoniemi LiinaSuoniemi commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

What this adds

An opinionated Python guide for high-complexity (ruff C901), so a python config coaches the smell with python-specific moves instead of the generic default. Same shape as the swallowed-exception guide.

The coaching stance

The smell is tangled responsibilities, not the number itself. The guide gives three concrete moves: name each branch's responsibility (branches that share a name belong together; a branch with no name wants its own function), lift guards out into early returns so the happy path stays flat, and change the shape rather than just moving it. Reshape uniform branches into a dict dispatch, comprehension or polymorphism; when polymorphism is not the fit and the branches are genuinely separate jobs, extract one named method per branch by responsibility, not a mechanical _part1/_part2 split. The test it leaves is whether a first-time reader can hold the function in their head.

Anti-gaming

It calls out gaming the metric, including a specific one: ruff's mccabe does not count ternary expressions, so rewriting ifs as ternaries lowers the C901 score while a human still has to hold every condition. Verified locally: a four-branch if chain scores 5 and trips C901 at max-complexity 3, while the equivalent ternary chain is not counted.

Notes

  • Uses the shared {% include "includes/line_level_issues.md" %} (now in core), consistent with the other guides.
  • No spec test, matching the decision on the swallowed-exception guide (routing is already covered by the "a finding's language selects a plugin's guide" case).

Adds a python-owned guide for high-complexity (ruff C901), matching the swallowed-exception guide's shape and using the shared line-level include now in core. Coaches the fix (name each branch's responsibility, lift guards out into early returns, change the shape rather than spread the same decisions across helpers) and warns against gaming the metric, including that ruff's mccabe does not count ternary expressions, so rewriting ifs as ternaries lowers the C901 score without lowering the real complexity.
…t the fit

From Ivett's review of habit-hooks#73: when polymorphism is not the right choice,
encourage extracting one named method per branch, each named for the
responsibility it handles. Inspired by oversized-function.md's warning
against mechanical splitting into helpers.
@devill
devill merged commit 55fe0a1 into habit-hooks:main Jul 2, 2026
2 checks passed
@devill

devill commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR. Merged

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.

2 participants