Blazingly fast cognitive complexity analysis for Python, written in Rust.
Cognitive complexity measures how hard code is to understand by humans, not machines.
Unlike traditional metrics like cyclomatic complexity, cognitive complexity accounts for nesting depth and control flow patterns that affect human comprehension. Inspired by G. Ann Campbell's research at SonarSource, complexipy provides a fast, accurate implementation for Python.
Key benefits:
- Human-focused - Penalizes nesting, flow breaks, and human-unfriendly logic
- Actionable insights - Identifies genuinely hard-to-maintain code
- Different from cyclomatic - Measures readability while cyclomatic measures structural, testing, and branch density
Try it with:
pip install complexipy
complexipy . --failed --suggest-refactors # in a python projectExample output for JoinMarket NG:
joinmarket-ng/tumbler/src/tumbler/runner.py
TumbleRunner::_run_taker_phase 19 ❌ FAILED
Refactor plans:
1. Extract complex block into helper function, lines 351-405
estimated: 19 -> 7 (-12)
steps:
- move the selected block into a private helper
- pass required values as parameters
- return the result/status needed by the caller
TumbleRunner::_run_maker_phase 24 ❌ FAILED
Refactor plans:
1. Extract complex block into helper function, lines 496-523
estimated: 24 -> 6 (-18)
steps:
- move the selected block into a private helper
- pass required values as parameters
- return the result/status needed by the caller
2. Flatten loop body with continue guards, lines 500-521
estimated: 24 -> 14 (-10)
steps:
- add negative condition checks at the top of the loop
- use continue for skipped items
- keep the main processing path unindented
3. Flatten nested condition block with guard clauses, lines 505-506
estimated: 24 -> 22 (-2)
steps:
- invert the outer condition
- return early when the condition fails
- move the main success path one indentation level left
- repeat for inner nested conditions where safe