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(dead-code): count every use of a private symbol, not only a call (#1662)
Raghav Chamadiyae889ea70a73f
diff --git a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py b/packages/core/src/repowise/core/analysis/dead_code/analyzer.pyindex ffaffc037..d090b1df2 100644--- a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py+++ b/packages/core/src/repowise/core/analysis/dead_code/analyzer.py@@ -1412,11 +1412,19 @@ class DeadCodeAnalyzer:):continue- has_callers = any(- self.graph.get_edge_data(pred, node, {}).get("edge_type") == "calls"+ # Any inbound use, not only a call. A base class that is subclassed+ # rather than instantiated, a collaborator the container constructs,+ # and a handler named as a value rather than invoked each carry a+ # symbol-level edge of their own; reading only ``calls`` here+ # reported all three as unused. The set is shared with the+ # unused-export pass, so it also carries types this population can+ # never hold — a method or an interface is filtered out above.+ is_used = any(+ self.graph.get_edge_data(pred, node, {}).get("edge_type")+ in REACHABILITY_USE_EDGE_TYPESfor pred in self.graph.predecessors(node))- if has_callers:+ if is_used:continue# Dispatch-table pattern: a private helper imported by name@@ -1453,7 +1461,7 @@ class DeadCodeAnalyzer:symbol_name=sym_name,symbol_kind=node_data.get("kind"),confidence=0.65,- reason=f"Private symbol '{sym_name}' has no callers",+ reason=f"Private symbol '{sym_name}' is not used anywhere",last_commit_at=git_meta.get("last_commit_at")if isinstance(git_meta.get("last_commit_at"), datetime)else None,@@ -1467,7 +1475,7 @@ class DeadCodeAnalyzer:if node_data.get("start_line")else None,package=self._get_package(file_path),- evidence=[f"No CALL edges to '{sym_name}'"],+ evidence=[f"No call, reference or override reaches '{sym_name}'"],safe_to_delete=False,primary_owner=git_meta.get("primary_owner_name"),age_days=git_meta.get("age_days"),diff --git a/packages/core/src/repowise/core/ingestion/models.py b/packages/core/src/repowise/core/ingestion/models.pyindex 372b60dd7..a33673e4e 100644--- a/packages/core/src/repowise/core/ingestion/models.py+++ b/packages/core/src/repowise/core/ingestion/models.py@@ -458,10 +458,11 @@ FILE_DEPENDENCY_EDGE_TYPES: frozenset[str] = frozenset(# Symbol → symbol references. "Something reaches this symbol", so containment# is excluded: a class containing a method is not the method being used.#-# `reads` is the one type that spans both layers, which is why it is the one-# member of both sets. Two extractors share the name: `csharp_member_reads`-# emits it file → file (596 rows locally) and `framework_edges/express` emits-# it symbol → symbol from a `path::__module__` node (1 row).+# `reads` is a member here for a reason that no longer holds: its symbol-level+# producer moved to `framework_binds`, so `csharp_member_reads` is the only one+# left and it emits file → file. A file node can never be a symbol node's+# predecessor, so membership is inert rather than wrong. Retiring it moves the+# vocabulary and belongs to a diff that can measure that.SYMBOL_USE_EDGE_TYPES: frozenset[str] = frozenset({"calls",diff --git a/tests/unit/dead_code/test_unused_internal.py b/tests/unit/dead_code/test_unused_internal.pyindex f0f7753ee..710e8489d 100644--- a/tests/unit/dead_code/test_unused_internal.py+++ b/tests/unit/dead_code/test_unused_internal.py@@ -87,6 +87,154 @@ def test_unused_internal_explicit_opt_out():assert internals == []+def test_unused_internal_skipped_when_subclassed():+ """A private base class reached only via ``extends`` is still used."""+ g = _build_graph(+ nodes={+ "pkg/base.py": {+ "is_entry_point": False,+ "is_test": False,+ "is_api_contract": False,+ "symbol_count": 1,+ "symbols": [+ {+ "name": "_BaseThing",+ "kind": "class",+ "visibility": "private",+ "decorators": [],+ "start_line": 1,+ "end_line": 12,+ "complexity_estimate": 1,+ },+ ],+ },+ "pkg/child.py": {+ "is_entry_point": False,+ "is_test": False,+ "is_api_contract": False,+ "symbol_count": 1,+ "symbols": [+ {+ "name": "Thing",+ "kind": "class",+ "visibility": "public",+ "decorators": [],+ "start_line": 1,+ "end_line": 8,+ "complexity_estimate": 1,+ },+ ],+ },+ },+ edges=[+ (+ "pkg/child.py::Thing",+ "pkg/base.py::_BaseThing",+ {"edge_type": "extends"},+ ),+ ],+ )+ analyzer = DeadCodeAnalyzer(g, git_meta_map={})+ report = analyzer.analyze(+ {+ "detect_unreachable_files": False,+ "detect_unused_exports": False,+ "detect_zombie_packages": False,+ "min_confidence": 0.0,+ }+ )+ names = {f.symbol_name for f in report.findings if f.kind == DeadCodeKind.UNUSED_INTERNAL}+ assert "_BaseThing" not in names+++def test_unused_internal_skipped_when_referenced_as_a_value():+ """A private helper named in a dispatch table is used without being called."""+ g = _build_graph(+ nodes={+ "pkg/handlers.py": {+ "is_entry_point": False,+ "is_test": False,+ "is_api_contract": False,+ "symbol_count": 2,+ "symbols": [+ {+ "name": "_handle",+ "kind": "function",+ "visibility": "private",+ "decorators": [],+ "start_line": 1,+ "end_line": 6,+ "complexity_estimate": 1,+ },+ {+ "name": "register",+ "kind": "function",+ "visibility": "public",+ "decorators": [],+ "start_line": 8,+ "end_line": 12,+ "complexity_estimate": 1,+ },+ ],+ },+ },+ edges=[+ (+ "pkg/handlers.py::register",+ "pkg/handlers.py::_handle",+ {"edge_type": "references"},+ ),+ ],+ )+ analyzer = DeadCodeAnalyzer(g, git_meta_map={})+ report = analyzer.analyze(+ {+ "detect_unreachable_files": False,+ "detect_unused_exports": False,+ "detect_zombie_packages": False,+ "min_confidence": 0.0,+ }+ )+ names = {f.symbol_name for f in report.findings if f.kind == DeadCodeKind.UNUSED_INTERNAL}+ assert "_handle" not in names+++def test_unused_internal_still_flagged_with_only_containment_edges():+ """A ``defines`` / ``has_method`` containment edge alone must not count as use."""+ g = _build_graph(+ nodes={+ "pkg/lonely.py": {+ "is_entry_point": False,+ "is_test": False,+ "is_api_contract": False,+ "symbol_count": 1,+ "symbols": [+ {+ "name": "_orphan",+ "kind": "function",+ "visibility": "private",+ "decorators": [],+ "start_line": 1,+ "end_line": 6,+ "complexity_estimate": 1,+ },+ ],+ },+ },+ )+ analyzer = DeadCodeAnalyzer(g, git_meta_map={})+ report = analyzer.analyze(+ {+ "detect_unreachable_files": False,+ "detect_unused_exports": False,+ "detect_zombie_packages": False,+ "min_confidence": 0.0,+ }+ )+ names = {f.symbol_name for f in report.findings if f.kind == DeadCodeKind.UNUSED_INTERNAL}+ assert "_orphan" in names++def test_unused_internal_skipped_when_imported_by_name():"""A private helper imported by name into a sibling module (typicaldispatch-table pattern: ``HANDLERS = {"python": _extract_py, ...}``)