Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(health): count Python match arms toward cyclomatic complexity (#1795)

Sloed206f6a23449
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py b/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py
index 40ab4cdcd..f1f245c16 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/cyclomatic.py
@@ -206,7 +206,17 @@ def _is_flat_match(node: Node, lmap: LanguageNodeMap) -> bool:
``loop``, ``if_let``, ``while_let`` expressions, and no ``block``
with multiple statements). Flat matches contribute 1 CCN point for
the match keyword itself but do NOT count each arm individually.
+
+ Python's ``match_statement`` is excluded: ``match``/``case`` in Python
+ is *pattern matching* — each ``case`` is a distinct branch point,
+ structurally the same decision as ``if``/``elif``. Suppressing its arms
+ like a C-style flat switch would report a ``match`` with N arms as ~N
+ points too low (measured: a 3-arm ``match`` gave CCN 2 while the
+ equivalent ``if``/``elif`` gave 4). Rust ``match_expression`` and other
+ languages' dispatch switches keep the flat behaviour.
"""
+ if node.type == "match_statement":
+ return False
complex_types = lmap.branch_kinds | lmap.loop_kinds | lmap.switch_kinds
cases = _collect_case_children(node, lmap)
if not cases:
diff --git a/tests/fixtures/lang_samples/python/match.py b/tests/fixtures/lang_samples/python/match.py
new file mode 100644
index 000000000..1e9262146
--- /dev/null
+++ b/tests/fixtures/lang_samples/python/match.py
@@ -0,0 +1,27 @@
+# Python match/case is pattern matching — each case is a distinct branch
+# point, structurally the same decision as if/elif. Unlike a flat Rust
+# match (or C-style switch), the arms must count toward CCN.
+#
+# python_match 3 arms -> base 1 + 3 cases = 4
+def python_match(x):
+ match x:
+ case 1:
+ return "one"
+ case 2:
+ return "two"
+ case 3:
+ return "three"
+ case _:
+ return "other"
+
+
+# Equivalent if/elif — must report the same CCN as python_match above.
+def python_if_elif(x):
+ if x == 1:
+ return "one"
+ elif x == 2:
+ return "two"
+ elif x == 3:
+ return "three"
+ else:
+ return "other"
diff --git a/tests/unit/health/test_complexity_walker.py b/tests/unit/health/test_complexity_walker.py
index 018488b24..8b2674f10 100644
--- a/tests/unit/health/test_complexity_walker.py
+++ b/tests/unit/health/test_complexity_walker.py
@@ -283,6 +283,24 @@ def test_rust_flat_match_complexity():
)
+def test_python_match_arms_count_toward_ccn():
+ """Python match/case is pattern matching — each case is a real branch.
+
+ Unlike Rust ``match`` (a flat dispatch that counts once), a Python
+ ``match`` with N ``case`` arms is structurally an ``if``/``elif`` chain
+ and each arm must count toward CCN. Regression for #1774: the flat-match
+ suppression was over-applied to Python, so a 4-arm match reported 2
+ (base 1 + 1 for the match, all arms dropped) instead of counting each arm.
+ """
+ results = _walk("python/match.py", "python")
+ match_fn = _find(results, "python_match")
+ assert match_fn is not None, "python_match not detected"
+ # base 1 + 4 case arms (1, 2, 3, wildcard _) = 5.
+ assert match_fn.ccn == 5, f"python match CCN expected 5, got {match_fn.ccn}"
+ # The arms are genuinely counted, not flattened away.
+ assert match_fn.ccn >= 4, f"match arms must count toward CCN, got {match_fn.ccn}"
+
+
def test_unsupported_language_returns_empty():
results = walk_file_complexity("/tmp/x.unknown", "klingon", b"")
assert results == []
diff --git a/tests/unit/health/test_ff_track_iljk_structural.py b/tests/unit/health/test_ff_track_iljk_structural.py
index 7791fa19d..4b913100b 100644
--- a/tests/unit/health/test_ff_track_iljk_structural.py
+++ b/tests/unit/health/test_ff_track_iljk_structural.py
@@ -150,18 +150,29 @@ def test_unbounded_update_still_flagged(dialect):
# --------------------------------------------------------------------------
-# large_method: a flat match dispatch is layout, not a size smell
+# large_method: a large Python match dispatch is real branching (each case
+# is an if/elif-equivalent branch), so it is a size + complexity smell
# --------------------------------------------------------------------------
-def test_large_method_skips_flat_match_dispatch():
+def test_large_method_fires_on_match_dispatch():
+ """A 31-case Python ``match`` dispatch is real branching, not layout.
+
+ Regression for #1774: Python match/case is pattern matching — each case
+ is a distinct branch, structurally an if/elif chain. The old flat-match
+ suppression reported this dispatch at CCN 2, which let a 65-line
+ dispatcher dodge the large-method detector. It now counts each arm and
+ must be flagged like any other tangled large method.
+ """
fm = walk_file("m.py", "python", _flat_match_src().encode())
assert fm.functions, "expected a function to be parsed"
fn = fm.functions[0]
- # The flat dispatch trips the NLOC threshold but sits at CCN 2 (layout).
+ # Trips the NLOC threshold and now also the CCN floor (31 arms + base).
assert fn.nloc >= LargeMethodDetector._NLOC_THRESHOLD
- assert fn.ccn == 2
- assert LargeMethodDetector().detect(_fn_ctx([fn])) == []
+ assert fn.ccn >= LargeMethodDetector._CCN_FLOOR
+ out = LargeMethodDetector().detect(_fn_ctx([fn]))
+ assert len(out) == 1, "a large match dispatch must be flagged as large"
+ assert out[0].function_name == "dispatch"
def test_large_method_still_fires_on_real_branching():