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.

feat(graph): resolve a method on a type imported through a re-export (#1664)

Raghav Chamadiya4c0533e3a6aa
diff --git a/packages/core/src/repowise/core/ingestion/call_resolver.py b/packages/core/src/repowise/core/ingestion/call_resolver.py
index be2a6fc43..ffa4165df 100644
--- a/packages/core/src/repowise/core/ingestion/call_resolver.py
+++ b/packages/core/src/repowise/core/ingestion/call_resolver.py
@@ -271,7 +271,11 @@ class CallResolver:
for path, name_to_file in self._import_names.items():
file_syms = self._file_symbols.get(path, {})
for name, source_file in name_to_file.items():
- if name not in file_syms:
+ # An origin outside the repo names no file any reader of this
+ # map can look up, and the wildcard pass below already refuses
+ # one. Holding the invariant in one pass and not its twin is
+ # what makes a later reader look safe when it is not.
+ if name not in file_syms and not source_file.startswith("external:"):
self._barrel_origins[path][name] = source_file
# Track wildcard re-exports, which forward every symbol of the imported
@@ -1087,6 +1091,25 @@ class CallResolver:
bound = self._import_names.get(file_path, {}).get(type_name)
if bound is not None and not bound.startswith("external:"):
sym_id = self._file_methods.get(bound, {}).get(key)
+ if sym_id is None:
+ # An import names the module it was written against, which in
+ # Python is usually a package ``__init__`` that re-exports the
+ # type rather than declaring it. Free calls already chase that
+ # chain; without it the import settles which type this is and
+ # then refuses every method of it.
+ #
+ # Keyed on the *exported* name, as the free-call chase is: the
+ # map holds each file's own local names, so an alias would ask
+ # the bound file about a name that means something else there.
+ # ``!= bound`` because a mutual re-export can leave an entry
+ # naming its own file.
+ binding = self._import_bindings.get(file_path, {}).get(type_name)
+ exported = (binding.exported_name if binding else None) or type_name
+ declaring = self._barrel_origins.get(bound, {}).get(exported)
+ if declaring is not None and declaring != bound:
+ sym_id = self._file_methods.get(declaring, {}).get(
+ (exported, call.target_name)
+ )
return None if sym_id is None else (sym_id, "import")
# Bound to something outside the repo and there is no edge to find,
diff --git a/tests/unit/ingestion/test_call_resolver_strategies.py b/tests/unit/ingestion/test_call_resolver_strategies.py
index 07d87c41f..b6ec4577a 100644
--- a/tests/unit/ingestion/test_call_resolver_strategies.py
+++ b/tests/unit/ingestion/test_call_resolver_strategies.py
@@ -490,3 +490,156 @@ class TestPythonTypedReceiver:
edges = _edges(parsed, tmp_path)
assert not [e for e in edges if str(e[3]).startswith("receiver_field_")]
assert not [e for e in edges if str(e[3]).startswith("receiver_typed_")]
+
+
+class TestImportedTypeThroughAReExport:
+ """An import binds the package, not the module that declares the type.
+
+ ``from pkg import Engine`` names ``pkg/__init__.py``, which re-exports
+ ``Engine`` and declares no method of it. Treating that as settling the type
+ and then refusing costs every method call on it.
+ """
+
+ def _repo(self) -> dict[str, tuple[str, str]]:
+ return {
+ "app.py": (
+ "python",
+ "from pkg import Engine\n\n"
+ "def run():\n"
+ " engine = Engine()\n"
+ " engine.render(1)\n",
+ ),
+ "pkg/__init__.py": ("python", "from pkg.engine import Engine\n"),
+ "pkg/engine.py": (
+ "python",
+ "class Engine:\n def render(self, obj):\n return obj\n",
+ ),
+ }
+
+ def test_a_re_exported_type_still_resolves_its_method(self, tmp_path: Path) -> None:
+ parsed = _parse_all(tmp_path, self._repo())
+ _link_imports(
+ parsed,
+ {
+ "app.py": {"pkg": "pkg/__init__.py"},
+ "pkg/__init__.py": {"pkg.engine": "pkg/engine.py"},
+ },
+ )
+ assert (
+ "app.py::run",
+ "pkg/engine.py::Engine::render",
+ 0.88,
+ "receiver_typed_import",
+ ) in _edges(parsed, tmp_path)
+
+ def test_a_bound_file_that_declares_the_pair_answers_directly(
+ self, tmp_path: Path
+ ) -> None:
+ """The chain is a fallback, not a first choice.
+
+ This is the shape the import tier always handled, and it had no test —
+ so nothing pinned that the chase runs only after a direct hit misses.
+ """
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "app.py": (
+ "python",
+ "from pkg.engine import Engine\n\n"
+ "def run():\n"
+ " engine = Engine()\n"
+ " engine.render(1)\n",
+ ),
+ "pkg/engine.py": (
+ "python",
+ "class Engine:\n def render(self, obj):\n return obj\n",
+ ),
+ },
+ )
+ _link_imports(parsed, {"app.py": {"pkg.engine": "pkg/engine.py"}})
+ assert (
+ "app.py::run",
+ "pkg/engine.py::Engine::render",
+ 0.88,
+ "receiver_typed_import",
+ ) in _edges(parsed, tmp_path)
+
+ def test_a_re_export_chain_does_not_invent_a_method(self, tmp_path: Path) -> None:
+ """Following the chain must not weaken the validator above it."""
+ files = self._repo()
+ files["app.py"] = (
+ "python",
+ "from pkg import Engine\n\n"
+ "def run():\n"
+ " engine = Engine()\n"
+ " engine.missing(1)\n",
+ )
+ parsed = _parse_all(tmp_path, files)
+ _link_imports(
+ parsed,
+ {
+ "app.py": {"pkg": "pkg/__init__.py"},
+ "pkg/__init__.py": {"pkg.engine": "pkg/engine.py"},
+ },
+ )
+ assert not [
+ e for e in _edges(parsed, tmp_path) if str(e[3]).startswith("receiver_typed_")
+ ]
+
+ def test_an_alias_does_not_read_the_bound_file_s_own_binding(
+ self, tmp_path: Path
+ ) -> None:
+ """The chain is keyed by each file's own local names, so an alias must
+ be translated back before it is used as a key.
+
+ Here ``consumer`` calls ``lib.Renderer`` under the name ``Engine``,
+ while ``lib`` itself binds that same name to an unrelated class. Asking
+ ``lib`` about ``Engine`` answers about the wrong one.
+ """
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "consumer.py": (
+ "python",
+ "from lib import Renderer as Engine\n\n"
+ "def run():\n"
+ " engine = Engine()\n"
+ " engine.render(1)\n",
+ ),
+ "lib.py": (
+ "python",
+ "from other import Engine\n\nclass Renderer:\n pass\n",
+ ),
+ "other.py": (
+ "python",
+ "class Engine:\n def render(self, obj):\n return obj\n",
+ ),
+ },
+ )
+ _link_imports(
+ parsed,
+ {"consumer.py": {"lib": "lib.py"}, "lib.py": {"other": "other.py"}},
+ )
+ assert not [
+ e
+ for e in _edges(parsed, tmp_path)
+ if e[1] == "other.py::Engine::render" and str(e[3]).startswith("receiver_")
+ ]
+
+ def test_an_origin_outside_the_repo_is_never_recorded(self, tmp_path: Path) -> None:
+ """A re-export map holding an unreadable path makes a reader look safe."""
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "app.py": ("python", "from thirdparty import Engine\n\ndef run():\n pass\n"),
+ },
+ )
+ for imp in parsed["app.py"].imports:
+ imp.resolved_file = "external:thirdparty"
+ resolver = CallResolver(parsed, {p: set() for p in parsed}, repo_path=str(tmp_path))
+ recorded = [
+ origin
+ for origins in resolver._barrel_origins.values()
+ for origin in origins.values()
+ ]
+ assert not [o for o in recorded if o.startswith("external:")]