Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(health): respect in-source # noqa suppression comments on Python except lines (#2853)
Rishabh Mishra2ebc6399745a
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py b/packages/core/src/repowise/core/analysis/health/complexity/error_handling.pyindex 7fc5d37a3..838591697 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py@@ -258,6 +258,27 @@ def _eh_java_try_ends_in_fail(catch_clause: Node, language: str) -> bool:_EH_RUST_KINDS = frozenset({"call_expression", "macro_invocation"})_EH_GO_KINDS = frozenset({"if_statement", "short_var_declaration", "assignment_statement"})+_NOQA_RE = re.compile(r"#\s*noqa(?::\s*([A-Za-z0-9,\s]+))?", re.IGNORECASE)+++def _eh_noqa_codes(clause: Node) -> set[str] | None:+ """Return uppercase noqa codes on the except clause line, or None if absent.++ Returns an empty set `set()` for blanket `# noqa`.+ Only trailing comments on the except clause line (before the block) count.+ """+ start_row = clause.start_point[0]+ for child in clause.children:+ if child.type == "comment" and child.start_point[0] == start_row:+ text = _eh_text(child).strip()+ m = _NOQA_RE.search(text)+ if m:+ codes_raw = m.group(1)+ if codes_raw is None or not codes_raw.strip():+ return set()+ return {c.upper() for c in re.split(r"[,\s]+", codes_raw.strip()) if c}+ return None+def _eh_node_kinds(language: str, lmap: LanguageNodeMap) -> frozenset[str]:"""Every node type ``_eh_visit`` can emit a hit on, so the shared file scan@@ -284,10 +305,15 @@ def _eh_visit("""catch_kinds = lmap.catch_kindsif catch_kinds and node.type in catch_kinds:+ noqa_codes = _eh_noqa_codes(node) if language == "python" else Noneblock = _eh_find_body_block(node)+ swallowed_unsuppressed = noqa_codes is None or (+ len(noqa_codes) > 0 and "S110" not in noqa_codes+ )if (block is not Noneand _eh_body_is_swallowed(block, language)+ and swallowed_unsuppressedand not (language == "java" and _eh_java_try_ends_in_fail(node, language))):hits.append(ErrorHandlingHit("swallowed_catch", node.start_point[0] + 1))@@ -298,8 +324,15 @@ def _eh_visit():# ``except:`` / ``except BaseException:`` also swallow# KeyboardInterrupt & SystemExit; ``except Exception:`` cannot.- kind = "bare_except" if _eh_catches_base(node) else "broad_except"- hits.append(ErrorHandlingHit(kind, node.start_point[0] + 1))+ is_base = _eh_catches_base(node)+ kind = "bare_except" if is_base else "broad_except"+ suppress = noqa_codes is not None and (+ len(noqa_codes) == 0+ or (kind == "broad_except" and "BLE001" in noqa_codes)+ or (kind == "bare_except" and bool(noqa_codes & {"BLE001", "E722"}))+ )+ if not suppress:+ hits.append(ErrorHandlingHit(kind, node.start_point[0] + 1))elif language == "rust" and _eh_rust_hit(node):# A panic-family macro aborts unconditionally; unwrap/expect converts# a Result/Option into a panic. Different claims → different kinds.diff --git a/tests/unit/health/test_error_handling.py b/tests/unit/health/test_error_handling.pyindex 8aadac989..1b668bf32 100644--- a/tests/unit/health/test_error_handling.py+++ b/tests/unit/health/test_error_handling.py@@ -338,3 +338,71 @@ def test_idiomatic_hit_is_labelled_and_not_scored():assert plain.details == {"kind": "unsafe_unwrap"}assert plain.deduction is Noneassert plain.reason == "unwrap/expect turns a recoverable error into a crash"+++ ("source", "expected", "note"),+ [+ (+ b"try:\n x()\nexcept Exception: # noqa: BLE001\n pass\n",+ {"swallowed_catch": 1},+ "BLE001 suppresses broad_except; swallowed_catch remains",+ ),+ (+ b"try:\n x()\nexcept Exception: # noqa: BLE001, E722\n pass\n",+ {"swallowed_catch": 1},+ "multi-code noqa suppresses broad_except",+ ),+ (+ b"try:\n x()\nexcept: # noqa: E722\n pass\n",+ {"swallowed_catch": 1},+ "E722 suppresses bare_except",+ ),+ (+ b"try:\n x()\nexcept: # noqa: BLE001\n pass\n",+ {"swallowed_catch": 1},+ "BLE001 suppresses bare_except",+ ),+ (+ b"try:\n x()\nexcept Exception: # noqa\n pass\n",+ {},+ "blanket noqa suppresses broad_except and swallowed_catch",+ ),+ (+ b"try:\n x()\nexcept: # noqa\n pass\n",+ {},+ "blanket noqa suppresses bare_except and swallowed_catch",+ ),+ (+ b"try:\n x()\nexcept ValueError: # noqa: S110\n pass\n",+ {},+ "S110 suppresses swallowed_catch",+ ),+ (+ b"try:\n x()\nexcept Exception: # noqa: BLE001, S110\n pass\n",+ {},+ "BLE001 + S110 suppresses both broad_except and swallowed_catch",+ ),+ (+ b"try:\n x()\nexcept Exception: # noqa: F401\n pass\n",+ {"swallowed_catch": 1, "broad_except": 1},+ "unrelated noqa code does not suppress broad_except",+ ),+ (+ b"try:\n x()\nexcept: # noqa: F401\n pass\n",+ {"swallowed_catch": 1, "bare_except": 1},+ "unrelated noqa code does not suppress bare_except",+ ),+ (+ b"try:\n x()\nexcept Exception:\n # noqa: BLE001\n pass\n",+ {"swallowed_catch": 1, "broad_except": 1},+ "noqa on the next line inside block does not suppress header",+ ),+ ],+)+def test_python_noqa_suppression(source: bytes, expected: dict, note: str):+ hits = _hits("python", source)+ by_kind: dict[str, int] = {}+ for h in hits:+ by_kind[h.kind] = by_kind.get(h.kind, 0) + 1+ assert by_kind == expected, f"{note}: expected {expected}, got {by_kind}"