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 by full dotted path, never by stem (#2674)
Swati Ahuja1567b8a2b81a
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/pytest_edges.py b/packages/core/src/repowise/core/ingestion/framework_edges/pytest_edges.pyindex 44d3c225b..6fdc0e561 100644--- a/packages/core/src/repowise/core/ingestion/framework_edges/pytest_edges.py+++ b/packages/core/src/repowise/core/ingestion/framework_edges/pytest_edges.py@@ -199,6 +199,13 @@ def _fixture_scopes(parsed: Any, class_name: str | None) -> list[str | None]:return scopes+# Stamped on the test -> conftest edge. pytest loads a conftest for every test+# under its directory, so the edge is real for fixture visibility, but no+# import statement makes it: consumers that mean "imports" (the import-edge+# oracle, density metrics) must be able to tell it from one.+CONFTEST_HINT = "pytest_conftest"++def _add_conftest_edges(graph: nx.DiGraph, path_set: set[str]) -> int:"""conftest.py -> test files in the same or child directories."""count = 0@@ -216,6 +223,7 @@ def _add_conftest_edges(graph: nx.DiGraph, path_set: set[str]) -> int:if (p.startswith(prefix) or (prefix == "" and "/" not in p)) and _add_edge_if_new(graph, p, conf):+ graph[p][conf]["hint_source"] = CONFTEST_HINTcount += 1return countdiff --git a/packages/core/src/repowise/core/ingestion/resolvers/python.py b/packages/core/src/repowise/core/ingestion/resolvers/python.pyindex 2a9381dc1..689dac98b 100644--- a/packages/core/src/repowise/core/ingestion/resolvers/python.py+++ b/packages/core/src/repowise/core/ingestion/resolvers/python.py@@ -6,7 +6,11 @@ import sysfrom pathlib import Pathfrom typing import TYPE_CHECKING-from ..languages.python_modules import build_python_module_index+from ..languages.python_modules import (+ _index_priority,+ _is_package_dir,+ build_python_module_index,+)from .context import ResolverContextif TYPE_CHECKING:@@ -36,8 +40,37 @@ def _module_index(ctx: ResolverContext) -> dict[str, str]:return idx+def _module_file(stem: str, path_set: frozenset[str] | set[str]) -> str | None:+ """``<stem>.py`` or ``<stem>/__init__.py``, whichever the repo has."""+ for c in (f"{stem}.py", f"{stem}/__init__.py"):+ if c in path_set:+ return c+ return None++def resolve_python_import(module_path: str, importer_path: str, ctx: ResolverContext) -> str | None:"""Resolve a Python import to a repo-relative file path."""+ hit = _resolve_file(module_path, importer_path, ctx)+ if hit:+ # A module naming itself is no dependency.+ return None if hit == importer_path else hit+ return _external(module_path, ctx)+++def _external(module_path: str, ctx: ResolverContext) -> str | None:+ """The ``external:`` node for an absolute import no repo file defines."""+ if module_path.startswith("."):+ return None+ # Nothing in the repo defines this module, so register it the way every+ # other language resolver does: the packages tab and the import counts can+ # only see a third-party or stdlib dependency if the miss becomes a node.+ # Stdlib is included on purpose, as Go and TypeScript already do, and+ # io_kind.py seeds stdlib names for exactly this consumer.+ return ctx.add_external_node(module_path)+++def _resolve_file(module_path: str, importer_path: str, ctx: ResolverContext) -> str | None:+ """The repo file *module_path* names from *importer_path*, or None."""importer_dir = Path(importer_path).parent# Relative import: ".sibling" or "..parent.module"@@ -47,63 +80,58 @@ def resolve_python_import(module_path: str, importer_path: str, ctx: ResolverConbase = importer_dirfor _ in range(dots - 1):base = base.parent- candidates = [- (base / rest).with_suffix(".py").as_posix() if rest else None,- (base / rest / "__init__.py").as_posix() if rest else None,- ]- for c in candidates:- if c and c in ctx.path_set:- return c- return None+ return _module_file((base / rest).as_posix(), ctx.path_set) if rest else None- # Absolute import: resolve via the source-root-aware module index first.- # This maps the fully-qualified dotted name to its defining file no- # matter how deeply the source root is nested (``src/``,- # ``packages/*/src/``, …) — the case the naive layout probes below miss,- # and the reason cross-package imports such as- # ``from repowise.core.persistence.models import SecurityFinding`` used- # to fall through to an ambiguous stem match.+ # Absolute import. A script (a file whose directory is not a package) has+ # its own directory first on sys.path, so its siblings shadow everything,+ # the stdlib included. A package member has no such entry: since Python 3+ # an absolute ``import types`` inside ``pkg/`` is the stdlib, never+ # ``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)+++def _resolve_in_repo(module_path: str, dotted: str, ctx: ResolverContext) -> str | None:+ """The repo file importable as *module_path* from a repo import root, or None."""+ # The source-root-aware module index maps the fully-qualified dotted name+ # to its defining file however deeply the source root is nested (``src/``,+ # ``packages/*/src/``, …).hit = _module_index(ctx).get(module_path)if hit:return hit- # Fallback: obvious flat / single-``src`` filesystem layouts. Kept as a- # belt-and-suspenders path for PEP 420 namespace packages (no- # ``__init__.py``) that the index cannot derive a dotted name for.- dotted = module_path.replace(".", "/")- candidates = [- f"{dotted}.py",- f"{dotted}/__init__.py",- f"src/{dotted}.py",- f"src/{dotted}/__init__.py",- ]- for c in candidates:- if c in ctx.path_set:- return c-- # Stem-only fallback, Python files only. The stem map holds every indexed- # file, so without the suffix guard ``import httpx`` resolved to a- # ``baselines/httpx.json`` fixture; Ruby and PHP guard theirs the same way.- # Stdlib names skip it: a nested ``specs/json.py`` is not what ``import- # json`` loads, and that one stem drew 527 false edges on this repo. A- # sibling of the importer still wins, as it does for a script run from- # its own directory.- stem = module_path.split(".")[-1].lower()- if module_path.split(".")[0] in _STDLIB_NAMES:- sibling = (importer_dir / f"{module_path.replace('.', '/')}.py").as_posix()- if sibling in ctx.path_set:- return sibling- else:- for candidate in ctx.stem_map.get(stem, ()):- if candidate.endswith((".py", ".pyi")):- return candidate-- # Nothing in the repo defines this module, so register it the way every- # other language resolver does: the packages tab and the import counts can- # only see a third-party or stdlib dependency if the miss becomes a node.- # Stdlib is included on purpose, as Go and TypeScript already do, and- # io_kind.py seeds stdlib names for exactly this consumer.- return ctx.add_external_node(module_path)+ # The repo root and a single ``src`` are import roots even for a loose+ # module the index cannot name.+ return (+ _module_file(dotted, ctx.path_set)+ or _module_file(f"src/{dotted}", ctx.path_set)+ or _suffix_match(module_path, dotted, ctx)+ )+++def _suffix_match(module_path: str, dotted: str, ctx: ResolverContext) -> str | None:+ """The best file whose path ends with the whole dotted path, or None."""+ # A dotted path the index cannot name, typically a PEP 420 namespace+ # package (``ns/pkg/mod.py`` with no ``ns/__init__.py``) under a nested+ # root: accept a file whose path *ends* with the whole dotted path, below a+ # directory that is not itself a package (so it can be a sys.path entry).+ # A bare top-level name is never matched this way: ``import pydantic`` is+ # not ``utils/pydantic.py``, and matching one component is a guess. Nor is+ # a stdlib name, which precedes every root but a script's own directory:+ # ``os.path`` is not ``compat/os/path.py``.+ if "." not in module_path or module_path.split(".")[0] in _STDLIB_NAMES:+ return None+ matches = []+ for c in ctx.stem_map.get(module_path.rsplit(".", 1)[-1].lower(), ()):+ for suffix in (f"/{dotted}.py", f"/{dotted}/__init__.py"):+ if c.endswith(suffix) and not _is_package_dir(c[: -len(suffix)], ctx.path_set):+ matches.append(c)+ return min(matches, key=_index_priority) if matches else Nonedef resolve_python_import_all(@@ -116,21 +144,30 @@ def resolve_python_import_all(inbound edge and the dead-code analyzer reports it unreachable (#666) —FastAPI apps wiring routers through their package are the canonical hit.Probe every imported name against the package directory and emit the- submodule targets alongside the package itself. The bare-relative form+ submodule targets, plus the package itself unless every name was a+ submodule. The bare-relative form(``from . import a, b``) is already split upstream by``expand_bare_relative_imports``; this covers the named-package forms,both absolute and relative."""- base = resolve_python_import(imp.module_path, importer_path, ctx)+ # A package ``__init__.py`` importing its own submodules (``from pkg import+ # a`` inside ``pkg/__init__.py``) still fans out; only the self edge goes.+ base = _resolve_file(imp.module_path, importer_path, ctx)if base is None:- return ()+ external = _external(imp.module_path, ctx)+ return (external,) if external else ()targets = [base]names = imp.imported_names or []+ # Whether some imported name is not a submodule, so it is read from the+ # package ``__init__.py`` itself.+ needs_base = Trueif base.endswith("__init__.py") and names and names != ["*"]:+ needs_base = Falseindex = _module_index(ctx)base_dir = Path(base).parent.as_posix()for name in names:if not name or name == "*" or "." in name:+ needs_base = Truecontinue# Source-root-aware index first (absolute imports), then direct# sibling probes, which also cover the relative form.@@ -138,11 +175,10 @@ def resolve_python_import_all(if not imp.is_relative:hit = index.get(f"{imp.module_path}.{name}")if hit is None:- for candidate in (f"{base_dir}/{name}.py", f"{base_dir}/{name}/__init__.py"):- if candidate in ctx.path_set:- hit = candidate- break- if hit and hit != base:+ hit = _module_file(f"{base_dir}/{name}", ctx.path_set)+ if hit is None or hit == base:+ needs_base = True+ else:targets.append(hit)# Point the binding for this name at the submodule file, not# the package ``__init__.py``. ``from pkg import submodule``@@ -154,4 +190,9 @@ def resolve_python_import_all(if (binding.exported_name or binding.local_name) == name:binding.source_file = hitbreak- return tuple(dict.fromkeys(targets))+ # ``from pkg import a, b`` where both are submodules reads nothing from+ # ``pkg/__init__.py``; an edge there would claim a dependency no name+ # carries, just as ``from pkg.a import x`` draws none to the package.+ if not needs_base and len(targets) > 1:+ targets.remove(base)+ return tuple(t for t in dict.fromkeys(targets) if t != importer_path)diff --git a/packages/core/src/repowise/core/pipeline/persist.py b/packages/core/src/repowise/core/pipeline/persist.pyindex 09c185b9d..d3512e4e4 100644--- a/packages/core/src/repowise/core/pipeline/persist.py+++ b/packages/core/src/repowise/core/pipeline/persist.py@@ -674,14 +674,14 @@ async def _edges_predate_cohesion(session: Any, repo_id: str, graph_builder: AnyThe signal is the store's own content rather than a version marker: aroutine update deliberately clamps ``store_format_version`` below the firstreindex gate, so a version comparison would refire on every run. Here, a- repo whose graph now has cohesion edges but whose table has no+ repo whose graph now has cohesion edges but whose table has no cohesion``hint_source`` anywhere is exactly a pre-cohesion store. Rewriting itstamps those rows, so this answers False from then on and the fullreconcile happens once."""from sqlalchemy import select- from repowise.core.ingestion.cohesion import is_cohesion_edge+ from repowise.core.ingestion.cohesion import COHESION_HINTS, is_cohesion_edgefrom repowise.core.persistence.models import GraphEdgetry:@@ -694,7 +694,12 @@ async def _edges_predate_cohesion(session: Any, repo_id: str, graph_builder: Anyrow = (await session.execute(select(GraphEdge.id)- .where(GraphEdge.repository_id == repo_id, GraphEdge.hint_source.is_not(None))+ .where(+ GraphEdge.repository_id == repo_id,+ # Cohesion hints only: other passes (pytest conftest) stamp a+ # hint too, and one of theirs is not a cohesion stamp.+ GraphEdge.hint_source.in_(COHESION_HINTS),+ ).limit(1))).first()diff --git a/packages/core/src/repowise/core/upgrade/version.py b/packages/core/src/repowise/core/upgrade/version.pyindex 6ccb5d6e0..6c6d56298 100644--- a/packages/core/src/repowise/core/upgrade/version.py+++ b/packages/core/src/repowise/core/upgrade/version.py@@ -54,7 +54,12 @@ STORE_FORMAT_VERSION: int = 2#: change makes ``persist_incremental_edges`` reconcile every file's edges once#: on the next update, so an existing index does not keep half its Python files#: without external edges.-PARSER_SCHEMA_VERSION: int = 3+#:+#: v4: Python absolute imports resolve only by their full dotted path (no stem+#: guess, no sibling lookup inside a package, never the importer itself), and+#: ``from pkg import a, b`` over submodules no longer edges into+#: ``pkg/__init__.py``. Reconciling once drops the stale edges from old stores.+PARSER_SCHEMA_VERSION: int = 4#: state.json key holding the store format version that wrote the store.STORE_FORMAT_VERSION_KEY = "store_format_version"diff --git a/tests/unit/ingestion/test_graph.py b/tests/unit/ingestion/test_graph.pyindex bdc899b0c..bed80d342 100644--- a/tests/unit/ingestion/test_graph.py+++ b/tests/unit/ingestion/test_graph.py@@ -307,18 +307,19 @@ class TestStemDisambiguation:target_b = build_with_order(list(reversed(files)))assert target_a == target_b == "src/widget.py"- def test_parent_dir_match_beats_shorter_path(self) -> None:- """A nested file whose parent directory matches the stem beats a- shallower file whose parent doesn't — canonical package layout- is the strongest signal."""+ def test_a_bare_name_is_not_guessed_from_a_nested_stem(self) -> None:+ """``import util`` from the repo root names a top-level module. Neither+ ``vendor/util.py`` nor ``src/util/util.py`` is importable as ``util``,+ so it stays external rather than picking one by stem."""b = GraphBuilder()- # Shallower path, parent dir doesn't match stemb.add_file(_parsed("vendor/util.py"))- # Deeper path, but parent dir == stem (canonical layout)b.add_file(_parsed("src/util/util.py"))b.add_file(_parsed("main.py", imports=[_imp("util")]))b.build()- assert b.graph().has_edge("main.py", "src/util/util.py")+ g = b.graph()+ assert not g.has_edge("main.py", "src/util/util.py")+ assert not g.has_edge("main.py", "vendor/util.py")+ assert g.has_edge("main.py", "external:util")def test_src_layout_direct_match(self) -> None:"""`from flask.app import X` finds src/flask/app.py via the newdiff --git a/tests/unit/ingestion/test_pytest_fixture_edges.py b/tests/unit/ingestion/test_pytest_fixture_edges.pyindex 71f3f4c4c..5e8d7b920 100644--- a/tests/unit/ingestion/test_pytest_fixture_edges.py+++ b/tests/unit/ingestion/test_pytest_fixture_edges.py@@ -415,3 +415,29 @@ class TestFixtureIsNotDeadCode:graph[pred][target].get("edge_type") in REACHABILITY_USE_EDGE_TYPESfor pred in graph.predecessors(target))+++def test_conftest_edge_is_marked_as_a_convention_not_an_import() -> None:+ """Collection loads a conftest for the tests below it; no import says so."""+ from repowise.core.ingestion.framework_edges.pytest_edges import (+ CONFTEST_HINT,+ _add_conftest_edges,+ )++ graph = nx.DiGraph()+ graph.add_node("tests/conftest.py", is_test=True)+ graph.add_node("tests/test_api.py", is_test=True)+ graph.add_node("tests/test_db.py", is_test=True)+ graph.add_node("app.py", is_test=False)+ # A real import of the conftest keeps its own, unhinted edge.+ graph.add_edge("tests/test_db.py", "tests/conftest.py", edge_type="imports")++ _add_conftest_edges(graph, set(graph.nodes))++ assert graph["tests/test_api.py"]["tests/conftest.py"] == {+ "edge_type": "framework",+ "imported_names": [],+ "hint_source": CONFTEST_HINT,+ }+ assert "hint_source" not in graph["tests/test_db.py"]["tests/conftest.py"]+ assert not graph.has_edge("app.py", "tests/conftest.py")diff --git a/tests/unit/ingestion/test_python_resolver_external.py b/tests/unit/ingestion/test_python_resolver_external.pyindex 8febcc74a..7b0cf6904 100644--- a/tests/unit/ingestion/test_python_resolver_external.py+++ b/tests/unit/ingestion/test_python_resolver_external.py@@ -2,13 +2,17 @@Pins the tail of ``resolve_python_import``: an absolute import no repo filedefines registers an external node, a relative miss stays None, and a-resolvable import is untouched.+resolvable import is untouched. The layout fixtures pin which absolute imports+a repo file may claim: the full dotted path under an import root, a script's+own directory, and nothing else (no stem guesses, no self-imports)."""from __future__ import annotationsimport networkx as nx+import pytest+from repowise.core.ingestion.graph import build_stem_mapfrom repowise.core.ingestion.models import Importfrom repowise.core.ingestion.resolvers.context import ResolverContextfrom repowise.core.ingestion.resolvers.python import (@@ -77,12 +81,11 @@ def test_a_script_sibling_shadows_the_stdlib_module() -> None:assert resolve_python_import("secrets", "tools/deploy.py", ctx) == "tools/secrets.py"-def test_non_stdlib_stem_fallback_still_resolves() -> None:+def test_a_bare_name_never_stem_matches_a_nested_file() -> None:+ """``import shared`` from the repo root is not ``pkg/helpers/shared.py``."""path = "pkg/helpers/shared.py"- ctx = ResolverContext(- path_set={*PATHS, path}, stem_map={"shared": [path]}, graph=nx.DiGraph()- )- assert resolve_python_import("shared", "app.py", ctx) == path+ ctx = ResolverContext(path_set={*PATHS, path}, stem_map={"shared": [path]}, graph=nx.DiGraph())+ assert resolve_python_import("shared", "app.py", ctx) == "external:shared"def test_relative_miss_stays_none_and_adds_nothing() -> None:@@ -103,17 +106,159 @@ def test_resolve_all_returns_the_external_key_without_probing() -> None:assert targets == ("external:requests",)-def test_stem_fallback_never_lands_on_a_non_python_file() -> None:+def test_a_non_python_file_is_never_a_target() -> None:ctx = ResolverContext(path_set={"app.py", "baselines/httpx.json", "vendor/httpx.py"},stem_map={"httpx": ["baselines/httpx.json", "vendor/httpx.py"]},graph=nx.DiGraph(),)- assert resolve_python_import("httpx", "app.py", ctx) == "vendor/httpx.py"+ assert resolve_python_import("httpx", "app.py", ctx) == "external:httpx"+ # From its own directory the sibling is what a script run there loads.+ assert resolve_python_import("httpx", "vendor/run.py", ctx) == "vendor/httpx.py"- only_fixture = ResolverContext(- path_set={"app.py", "baselines/httpx.json"},- stem_map={"httpx": ["baselines/httpx.json"]},- graph=nx.DiGraph(),++# --- Layout fixtures: each is a repo's Python file set. -----------------------+++def _layout_ctx(paths: set[str]) -> ResolverContext:+ return ResolverContext(path_set=paths, stem_map=build_stem_map(paths), graph=nx.DiGraph())+++def py_shadow_thirdparty() -> ResolverContext:+ """A package whose helper modules share third-party and stdlib names."""+ return _layout_ctx(+ {+ "pkg/__init__.py",+ "pkg/client.py",+ "pkg/types.py",+ "pkg/utils/__init__.py",+ "pkg/utils/pydantic.py",+ "pkg/utils/json.py",+ "tests/apps/wsgi.py",+ }+ )+++def py_self_stdlib() -> ResolverContext:+ """Package modules named after the stdlib module they wrap."""+ return _layout_ctx({"pkg/__init__.py", "pkg/logging.py", "pkg/uuid.py"})+++def py_script_sibling() -> ResolverContext:+ """A scripts directory that is not a package, next to a real package."""+ return _layout_ctx(+ {+ "scripts/release.py",+ "scripts/helpers.py",+ "scripts/secrets.py",+ "scripts/tools/__init__.py",+ "scripts/tools/git.py",+ "pkg/__init__.py",+ "pkg/helpers.py",+ "pkg/core.py",+ }+ )+++def py_namespace_multi_root() -> ResolverContext:+ """One PEP 420 namespace split across two distribution roots."""+ return _layout_ctx(+ {+ "libs/alpha/acme/alpha/__init__.py",+ "libs/alpha/acme/alpha/core.py",+ "libs/beta/acme/beta/__init__.py",+ "libs/beta/acme/beta/client.py",+ "libs/beta/tests/acme/beta/client.py",+ }+ )+++def py_src_monorepo() -> ResolverContext:+ """Several ``packages/*/src`` distributions importing each other."""+ return _layout_ctx(+ {+ "packages/core/src/corelib/__init__.py",+ "packages/core/src/corelib/models.py",+ "packages/core/src/corelib/io.py",+ "packages/cli/src/clitool/__init__.py",+ "packages/cli/src/clitool/main.py",+ "packages/cli/tests/test_main.py",+ }+ )+++def test_third_party_names_do_not_bind_to_local_modules(py_shadow_thirdparty) -> None:+ ctx = py_shadow_thirdparty+ assert resolve_python_import("pydantic", "pkg/client.py", ctx) == "external:pydantic"+ # A dotted third-party path needs its whole suffix in the repo, not its tail.+ assert resolve_python_import("werkzeug.wsgi", "pkg/client.py", ctx) == "external:werkzeug.wsgi"+ assert (+ resolve_python_import("thirdparty.types", "pkg/client.py", ctx)+ == "external:thirdparty.types"+ )+ # The package's own modules still resolve by their full dotted name.+ assert resolve_python_import("pkg.utils.pydantic", "pkg/client.py", ctx) == (+ "pkg/utils/pydantic.py"+ )+++def test_stdlib_names_inside_a_package_stay_stdlib(py_shadow_thirdparty) -> None:+ """Absolute imports: ``import types`` in ``pkg/`` never means ``pkg/types.py``."""+ ctx = py_shadow_thirdparty+ assert resolve_python_import("types", "pkg/client.py", ctx) == "external:types"+ assert resolve_python_import("json", "pkg/utils/pydantic.py", ctx) == "external:json"+++def test_a_module_never_imports_itself(py_self_stdlib) -> None:+ ctx = py_self_stdlib+ assert resolve_python_import("logging", "pkg/logging.py", ctx) == "external:logging"+ assert resolve_python_import("uuid", "pkg/uuid.py", ctx) == "external:uuid"+ # Naming itself is no edge, and no external package either.+ assert resolve_python_import("pkg.logging", "pkg/logging.py", ctx) is None+ assert "external:pkg.logging" not in ctx.graph.nodes+++def test_script_directory_comes_first_on_sys_path(py_script_sibling) -> None:+ ctx = py_script_sibling+ assert resolve_python_import("helpers", "scripts/release.py", ctx) == "scripts/helpers.py"+ assert resolve_python_import("secrets", "scripts/release.py", ctx) == "scripts/secrets.py"+ assert resolve_python_import("tools.git", "scripts/release.py", ctx) == "scripts/tools/git.py"+ # The script itself is never its own import.+ assert resolve_python_import("release", "scripts/release.py", ctx) is None+++def test_sibling_rule_skips_package_members(py_script_sibling) -> None:+ """A package member's directory is not on sys.path, so no sibling lookup."""+ ctx = py_script_sibling+ assert resolve_python_import("helpers", "pkg/core.py", ctx) == "external:helpers"+ assert resolve_python_import("pkg.helpers", "pkg/core.py", ctx) == "pkg/helpers.py"+++def test_namespace_package_resolves_across_roots(py_namespace_multi_root) -> None:+ ctx = py_namespace_multi_root+ importer = "libs/alpha/acme/alpha/core.py"+ assert resolve_python_import("acme.beta.client", importer, ctx) == (+ "libs/beta/acme/beta/client.py"+ )+ assert resolve_python_import("acme.beta", importer, ctx) == "libs/beta/acme/beta/__init__.py"+ # A tail that is not the whole dotted path is not a match.+ assert resolve_python_import("other.beta.client", importer, ctx) == (+ "external:other.beta.client"+ )+++def test_src_monorepo_resolves_by_dotted_name(py_src_monorepo) -> None:+ ctx = py_src_monorepo+ main = "packages/cli/src/clitool/main.py"+ assert resolve_python_import("corelib.models", main, ctx) == (+ "packages/core/src/corelib/models.py")- assert resolve_python_import("httpx", "app.py", only_fixture) == "external:httpx"+ assert resolve_python_import("corelib", main, ctx) == "packages/core/src/corelib/__init__.py"+ # ``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) == maindiff --git a/tests/unit/ingestion/test_python_resolver_fanout.py b/tests/unit/ingestion/test_python_resolver_fanout.pyindex d43e3bcc0..e13fcd605 100644--- a/tests/unit/ingestion/test_python_resolver_fanout.py+++ b/tests/unit/ingestion/test_python_resolver_fanout.py@@ -54,6 +54,15 @@ def test_package_import_fans_out_to_submodules() -> None:)+def test_all_submodule_names_skip_the_package_init() -> None:+ # ``from routers import workspace, git`` reads nothing from+ # ``routers/__init__.py``, just as ``from routers.workspace import x``+ # draws no edge to it.+ imp = _imp("routers", ["workspace", "git"])+ targets = resolve_python_import_all(imp, "app.py", _ctx(PATHS))+ assert targets == ("routers/workspace.py", "routers/git/__init__.py")++def test_non_submodule_names_resolve_to_package_only() -> None:imp = _imp("routers", ["some_function"])targets = resolve_python_import_all(imp, "app.py", _ctx(PATHS))@@ -84,7 +93,8 @@ def test_relative_package_import_fans_out() -> None:# ``from .sub import handlers`` inside pkg/api.pyimp = _imp(".sub", ["handlers"], relative=True)targets = resolve_python_import_all(imp, "pkg/api.py", _ctx(paths))- assert targets == ("pkg/sub/__init__.py", "pkg/sub/handlers.py")+ # Every name is a submodule, so nothing is read from the package init.+ assert targets == ("pkg/sub/handlers.py",)def test_unresolvable_relative_import_returns_empty() -> None:@@ -106,7 +116,7 @@ def test_source_root_nested_package_fans_out() -> None:}imp = _imp("myapp.routers", ["users"])targets = resolve_python_import_all(imp, "src/myapp/server.py", _ctx(paths))- assert targets == ("src/myapp/routers/__init__.py", "src/myapp/routers/users.py")+ assert targets == ("src/myapp/routers/users.py",)def test_submodule_binding_points_at_submodule_file() -> None:@@ -127,7 +137,7 @@ def test_submodule_binding_points_at_submodule_file() -> None:bindings=[NamedBinding(local_name="foo", exported_name=None, source_file=None)],)targets = resolve_python_import_all(imp, "caller.py", _ctx(paths))- assert targets == ("sensors/__init__.py", "sensors/foo.py")+ assert targets == ("sensors/foo.py",)# The binding for ``foo`` must point at the submodule file.assert len(imp.bindings) == 1assert imp.bindings[0].local_name == "foo"@@ -174,7 +184,7 @@ def test_submodule_aliased_binding_points_at_submodule_file() -> None: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 targets == ("sensors/foo.py",)assert len(imp.bindings) == 1assert imp.bindings[0].local_name == "f"assert imp.bindings[0].source_file == "sensors/foo.py"@@ -201,3 +211,14 @@ def test_non_submodule_aliased_binding_keeps_package_init() -> None:assert len(imp.bindings) == 1assert imp.bindings[0].local_name == "sf"assert imp.bindings[0].source_file is None+++def test_package_init_importing_its_own_submodule_fans_out() -> None:+ """``from pkg import views`` inside ``pkg/__init__.py`` names the package+ itself: no self edge, but the submodule edge stays."""+ paths = {"app/pkg/__init__.py", "app/pkg/views.py"}+ imp = _imp("pkg", ["views"])+ targets = resolve_python_import_all(imp, "app/pkg/__init__.py", _ctx(paths))+ assert targets == ("app/pkg/views.py",)+ imp = _imp("pkg", ["some_function"])+ assert resolve_python_import_all(imp, "app/pkg/__init__.py", _ctx(paths)) == ()diff --git a/tests/unit/persistence/test_cohesion_store_gate.py b/tests/unit/persistence/test_cohesion_store_gate.pynew file mode 100644index 000000000..76b2faec5--- /dev/null+++ b/tests/unit/persistence/test_cohesion_store_gate.py@@ -0,0 +1,50 @@+"""A stored edge counts as a cohesion stamp only when its hint is a cohesion hint.++Other passes stamp ``hint_source`` too (the pytest conftest convention edge),+so a store holding only those rows still predates cohesion edges and must be+reconciled once.+"""++from __future__ import annotations++from types import SimpleNamespace++import networkx as nx++from repowise.core.ingestion.cohesion import SAME_PACKAGE_HINT+from repowise.core.ingestion.framework_edges.pytest_edges import CONFTEST_HINT+from repowise.core.persistence.models import GraphEdge+from repowise.core.pipeline.persist import _edges_predate_cohesion+from tests.unit.persistence.helpers import insert_repo+++def _builder_with_a_cohesion_edge() -> SimpleNamespace:+ graph = nx.DiGraph()+ graph.add_edge("pkg/a.go", "pkg/b.go", hint_source=SAME_PACKAGE_HINT)+ return SimpleNamespace(graph=lambda: graph)+++async def _store_edge(session, repo_id: str, hint: str) -> None:+ session.add(+ GraphEdge(+ repository_id=repo_id,+ source_node_id="tests/test_a.py",+ target_node_id="tests/conftest.py",+ hint_source=hint,+ )+ )+ await session.commit()+++async def test_a_conftest_hint_is_not_a_cohesion_stamp(async_session):+ repo = await insert_repo(async_session)+ await _store_edge(async_session, repo.id, CONFTEST_HINT)+ assert await _edges_predate_cohesion(async_session, repo.id, _builder_with_a_cohesion_edge())+++async def test_a_stored_cohesion_hint_ends_the_reconcile(async_session):+ repo = await insert_repo(async_session)+ await _store_edge(async_session, repo.id, SAME_PACKAGE_HINT)+ assert not await _edges_predate_cohesion(+ async_session, repo.id, _builder_with_a_cohesion_edge()+ )