feat(python): add an opinionated high-complexity guide#73
Merged
devill merged 2 commits intoJul 2, 2026
Conversation
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.
Collaborator
|
Thanks for the PR. Merged |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this adds
An opinionated Python guide for
high-complexity(ruffC901), 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/_part2split. 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-branchifchain scores 5 and trips C901 at max-complexity 3, while the equivalent ternary chain is not counted.Notes
{% include "includes/line_level_issues.md" %}(now in core), consistent with the other guides.