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(python): properly pin bindings for aliased submodules (#1785) (#1255)

Jyotirmya Sharmaf44f99babc3c
diff --git a/packages/core/src/repowise/core/ingestion/resolvers/python.py b/packages/core/src/repowise/core/ingestion/resolvers/python.py
index 103e41014..441520f1f 100644
--- a/packages/core/src/repowise/core/ingestion/resolvers/python.py
+++ b/packages/core/src/repowise/core/ingestion/resolvers/python.py
@@ -121,7 +121,7 @@ def resolve_python_import_all(
# (#1193). Without this the binding's source_file stays the
# package init, which declares nothing, and the call is missed.
for binding in imp.bindings:
- if binding.local_name == name:
+ if (binding.exported_name or binding.local_name) == name:
binding.source_file = hit
break
return tuple(dict.fromkeys(targets))
diff --git a/tests/unit/ingestion/test_python_resolver_fanout.py b/tests/unit/ingestion/test_python_resolver_fanout.py
index a25dc8a89..335863526 100644
--- a/tests/unit/ingestion/test_python_resolver_fanout.py
+++ b/tests/unit/ingestion/test_python_resolver_fanout.py
@@ -156,3 +156,45 @@ def test_non_submodule_binding_keeps_package_init() -> None:
assert len(imp.bindings) == 1
assert imp.bindings[0].local_name == "some_function"
assert imp.bindings[0].source_file is None
+
+
+def test_submodule_aliased_binding_points_at_submodule_file() -> None:
+ """``from pkg import submodule as alias`` binds ``alias`` to the submodule file."""
+ paths = {
+ "sensors/__init__.py",
+ "sensors/foo.py",
+ "caller.py",
+ }
+ imp = _imp(
+ "sensors",
+ ["foo"],
+ bindings=[NamedBinding(local_name="f", exported_name="foo", source_file=None)],
+ )
+ targets = resolve_python_import_all(imp, "caller.py", _ctx(paths))
+ assert targets == ("sensors/__init__.py", "sensors/foo.py")
+ assert len(imp.bindings) == 1
+ assert imp.bindings[0].local_name == "f"
+ assert imp.bindings[0].source_file == "sensors/foo.py"
+
+
+def test_non_submodule_aliased_binding_keeps_package_init() -> None:
+ """An aliased name that is not a submodule file keeps the package init as its source."""
+ paths = {
+ "sensors/__init__.py",
+ "sensors/foo.py",
+ "caller.py",
+ }
+ imp = _imp(
+ "sensors",
+ ["some_function"],
+ bindings=[
+ NamedBinding(
+ local_name="sf", exported_name="some_function", source_file=None
+ )
+ ],
+ )
+ targets = resolve_python_import_all(imp, "caller.py", _ctx(paths))
+ assert targets == ("sensors/__init__.py",)
+ assert len(imp.bindings) == 1
+ assert imp.bindings[0].local_name == "sf"
+ assert imp.bindings[0].source_file is None