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(ingestion): resolve Python imports under the importer's own import root (#2717)
Raghav Chamadiya9b256752fb6c
diff --git a/packages/core/src/repowise/core/ingestion/resolvers/python.py b/packages/core/src/repowise/core/ingestion/resolvers/python.pyindex 689dac98b..93737e7b4 100644--- a/packages/core/src/repowise/core/ingestion/resolvers/python.py+++ b/packages/core/src/repowise/core/ingestion/resolvers/python.py@@ -10,6 +10,7 @@ from ..languages.python_modules import (_index_priority,_is_package_dir,build_python_module_index,+ dotted_module_for,)from .context import ResolverContext@@ -89,11 +90,31 @@ def _resolve_file(module_path: str, importer_path: str, ctx: ResolverContext) -># ``pkg/types.py``.dotted = module_path.replace(".", "/")if not _is_package_dir(importer_dir.as_posix(), ctx.path_set):- base = "" if importer_dir.as_posix() == "." else f"{importer_dir.as_posix()}/"- hit = _module_file(f"{base}{dotted}", ctx.path_set)- if hit:- return hit- return _resolve_in_repo(module_path, dotted, ctx)+ hit = _module_file(_under(importer_dir.as_posix(), dotted), ctx.path_set)+ else:+ hit = _resolve_in_own_root(module_path, dotted, importer_path, ctx)+ return hit or _resolve_in_repo(module_path, dotted, ctx)+++def _under(root: str, rel: str) -> str:+ return rel if root in ("", ".") else f"{root}/{rel}"+++def _resolve_in_own_root(+ module_path: str, dotted: str, importer_path: str, ctx: ResolverContext+) -> str | None:+ """*module_path* under the root the importer's own package is imported from."""+ # ``libs/x/pkg/mod.py`` is ``pkg.mod``, so ``libs/x`` is on sys.path+ # whenever it runs, and ``import helper`` there means ``libs/x/helper.py``.+ # A stdlib name stays external, as for any other package member.+ if module_path.split(".")[0] in _STDLIB_NAMES:+ return None+ own = dotted_module_for(importer_path, ctx.path_set)+ if not own:+ return None+ parts = Path(importer_path).with_suffix("").parts+ depth = own.count(".") + 1 + (parts[-1] == "__init__")+ return _module_file(_under("/".join(parts[:-depth]), dotted), ctx.path_set)def _resolve_in_repo(module_path: str, dotted: str, ctx: ResolverContext) -> str | None:diff --git a/tests/unit/ingestion/test_python_resolver_external.py b/tests/unit/ingestion/test_python_resolver_external.pyindex 7b0cf6904..a67e21052 100644--- a/tests/unit/ingestion/test_python_resolver_external.py+++ b/tests/unit/ingestion/test_python_resolver_external.py@@ -262,3 +262,74 @@ def test_src_monorepo_resolves_by_dotted_name(py_src_monorepo) -> None:# ``io`` is the stdlib even though ``corelib/io.py`` exists.assert resolve_python_import("io", main, ctx) == "external:io"assert resolve_python_import("clitool.main", "packages/cli/tests/test_main.py", ctx) == main+++def py_nested_import_root() -> ResolverContext:+ """microdot's layout: a vendored package beside a loose module it imports."""+ return _layout_ctx(+ {+ "libs/circuitpython/adafruit_ticks.py",+ "libs/circuitpython/asyncio/__init__.py",+ "libs/circuitpython/asyncio/core.py",+ "libs/circuitpython/asyncio/types.py",+ "libs/micropython/adafruit_ticks.py",+ "libs/circuitpython/tools/run.py",+ "libs/circuitpython/tools/helper.py",+ "src/microdot/__init__.py",+ "src/microdot/helpers.py",+ "vendor/helpers.py",+ }+ )+++def test_package_member_resolves_a_bare_name_under_its_own_root(py_nested_import_root) -> None:+ """``asyncio/core.py`` is imported from ``libs/circuitpython``, so its siblings are too."""+ ctx = py_nested_import_root+ for importer in (+ "libs/circuitpython/asyncio/core.py",+ "libs/circuitpython/asyncio/__init__.py",+ ):+ assert resolve_python_import("adafruit_ticks", importer, ctx) == (+ "libs/circuitpython/adafruit_ticks.py"+ )+ # A stdlib name stays the stdlib, even beside ``asyncio/types.py``.+ assert resolve_python_import("types", "libs/circuitpython/asyncio/core.py", ctx) == (+ "external:types"+ )+++def test_a_script_sibling_in_a_nested_non_package_dir_resolves(py_nested_import_root) -> None:+ ctx = py_nested_import_root+ assert resolve_python_import("helper", "libs/circuitpython/tools/run.py", ctx) == (+ "libs/circuitpython/tools/helper.py"+ )+++def test_own_root_never_matches_a_same_stem_module_elsewhere(py_nested_import_root) -> None:+ ctx = py_nested_import_root+ # ``src/microdot`` is imported from ``src``, which has no ``adafruit_ticks.py``.+ assert resolve_python_import("adafruit_ticks", "src/microdot/helpers.py", ctx) == (+ "external:adafruit_ticks"+ )+ # ``src`` has no ``helpers.py``; ``vendor/helpers.py`` and the package's own are other roots.+ assert resolve_python_import("helpers", "src/microdot/__init__.py", ctx) == "external:helpers"+ assert resolve_python_import("helper", "libs/circuitpython/asyncio/core.py", ctx) == (+ "external:helper"+ )+++def test_own_root_wins_over_a_same_dotted_name_in_another_root() -> None:+ """Two distributions each with a ``tests`` package: each imports its own."""+ ctx = _layout_ctx(+ {+ "libs/a/tests/__init__.py",+ "libs/a/tests/stubs.py",+ "libs/b/tests/__init__.py",+ "libs/b/tests/stubs.py",+ "libs/b/tests/test_x.py",+ }+ )+ assert resolve_python_import("tests.stubs", "libs/b/tests/test_x.py", ctx) == (+ "libs/b/tests/stubs.py"+ )