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(graph): keep a rebound Python class or function as its own symbol (#4813)
Swati Ahuja05656ef09a71
diff --git a/packages/core/src/repowise/core/ingestion/parser/symbols.py b/packages/core/src/repowise/core/ingestion/parser/symbols.pyindex 9185397a8..11a38dc51 100644--- a/packages/core/src/repowise/core/ingestion/parser/symbols.py+++ b/packages/core/src/repowise/core/ingestion/parser/symbols.py@@ -1,4 +1,8 @@-"""Symbol extraction: one query match to one ``Symbol``, and the parent it belongs to."""+"""Symbol extraction: one query match to one ``Symbol``, and the parent it belongs to.++Exception: a Python assignment that rebinds a ``def``/``class`` of its name yields+no symbol (see ``_drop_python_rebindings``).+"""from __future__ import annotations@@ -594,6 +598,8 @@ def _finalize_symbols(# The grammar reads a C# 14 extension block as a constructor# named ``extension``; its members were added above instead.symbols = drop_extension_block_symbols(symbols, root, src)+ if lang == "python":+ symbols = _drop_python_rebindings(symbols)# Anonymous members go last, once the overloads holding them carry# their discriminators.anonymous = [s for s in symbols if is_anonymous_member(s)]@@ -602,6 +608,21 @@ def _finalize_symbols(disambiguate_colliding_ids(anonymous, lang)return symbols++_PYTHON_ASSIGNMENT_KINDS = frozenset({"variable", "constant"})+++def _drop_python_rebindings(symbols: list[Symbol]) -> list[Symbol]:+ """Drop a module-level assignment that rebinds a ``def`` or ``class`` of its name.++ ``Factory = _add_hash(Factory)`` declares nothing new, yet it shares the+ class's id, so the graph node took the assignment's line and every call to+ ``Factory`` landed there.+ """+ defined = {s.id for s in symbols if s.kind not in _PYTHON_ASSIGNMENT_KINDS}+ return [s for s in symbols if s.kind not in _PYTHON_ASSIGNMENT_KINDS or s.id not in defined]++def _python_name_alias_chain(def_node: Node, src: str) -> tuple[list[str], str | None]:"""Every bare-identifier target of a Python assignment, and its terminal name.diff --git a/tests/unit/ingestion/test_call_resolver_strategies.py b/tests/unit/ingestion/test_call_resolver_strategies.pyindex b4ad7a64b..d3d146fc6 100644--- a/tests/unit/ingestion/test_call_resolver_strategies.py+++ b/tests/unit/ingestion/test_call_resolver_strategies.py@@ -2245,6 +2245,35 @@ class TestExternalReceiverType:assert [d.type_name for d in scan_declarations("List<Foo> xs;\n", "java")] == ["external:List"]+++class TestPythonRebindingOfADefinition:+ """``Factory = deco(Factory)`` rebinds the class; it is not a second symbol."""++ def test_a_rebound_class_keeps_its_own_symbol(self, tmp_path: Path) -> None:+ parsed = _parse_all(+ tmp_path,+ {+ "mod.py": (+ "python",+ "def deco(cls):\n return cls\n\n"+ "class Factory:\n pass\n\n"+ "Factory = deco(Factory)\n\n"+ "def use_it():\n Factory()\n",+ )+ },+ )+ factories = [s for s in parsed["mod.py"].symbols if s.name == "Factory"]+ assert [(s.kind, s.start_line) for s in factories] == [("class", 4)]+ assert ("mod.py::use_it", "mod.py::Factory", 0.95, "same_file") in _edges(+ parsed, tmp_path+ )++ def test_an_assignment_with_no_definition_stays_a_symbol(self, tmp_path: Path) -> None:+ parsed = _parse_all(tmp_path, {"mod.py": ("python", "handler = make()\n")})+ assert [s.kind for s in parsed["mod.py"].symbols] == ["variable"]++class TestPythonNameAliasAssignment:"""A plain-name alias must redirect onto the real definition, not its own line (#2791)."""