Whole file
Melevir/cognitive_complexity
The author described this change as “FIX #3: correctly process nested functions and lambdas”. It counts as a record because the check below fails on the code as it stood at 85f0a1ab0 and passes on 59587af24, with nothing else changed between the two runs.
ProjectMelevir/cognitive_complexity
Fix saved2019-11-06
Sharing licenceMIT · LICENSE
Change size+8 −1
What the code was meant to do, written into the code itself as a save note
FIX #3: correctly process nested functions and lambdas
The change
| 23 | 23 | ast.For, | |
| 24 | 24 | ast.While, | |
| 25 | 25 | ) | |
| 26 | + | incrementers_nodes = ( | |
| 27 | + | ast.FunctionDef, | |
| 28 | + | ast.AsyncFunctionDef, | |
| 29 | + | ast.Lambda, | |
| 30 | + | ) | |
| 26 | 31 | ||
| 27 | 32 | base_complexity = 0 | |
| 28 | 33 | child_complexity = 0 | |
| ⋯ | |||
| 30 | 35 | if isinstance(node, control_flow_breakers): | |
| 31 | 36 | increment_by += 1 | |
| 32 | 37 | base_complexity += max(1, increment_by) | |
| 38 | + | elif isinstance(node, incrementers_nodes): | |
| 39 | + | increment_by += 1 | |
| 33 | 40 | elif isinstance(node, ast.BoolOp): | |
| 34 | 41 | inner_boolops_amount = len([n for n in ast.walk(node) if isinstance(n, ast.BoolOp)]) | |
| 35 | - | base_complexity += inner_boolops_amount * increment_by | |
| 42 | + | base_complexity += inner_boolops_amount * max(increment_by, 1) | |
| 36 | 43 | should_iter_children = False | |
| 37 | 44 | elif isinstance(node, (ast.Break, ast.Continue)): | |
| 38 | 45 | base_complexity += max(1, increment_by) | |
The check that tells the two apart
fail→pass·cognitive_complexity/tests/test_cognitive_complexity.py::test_nested_functions
Check file cognitive_complexity/tests/test_cognitive_complexity.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before it85f0a1ab0a06a903f1b00dbddc2d3c1b3d185a84
Broken version dated2019-11-06
Modulecognitive_complexity.api
Units changedget_cognitive_complexity_for_node
Fingerprint20ac4d5f907ce866
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.