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): resolve Python test paths anchored at Path(__file__) ancestors (#3245) (#3518)
Rishabh Mishracfd8351df881
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/test_path_strings.py b/packages/core/src/repowise/core/ingestion/framework_edges/test_path_strings.pyindex cc14db727..f6dfa2b17 100644--- a/packages/core/src/repowise/core/ingestion/framework_edges/test_path_strings.py+++ b/packages/core/src/repowise/core/ingestion/framework_edges/test_path_strings.py@@ -19,6 +19,7 @@ a path-join call before resolving.from __future__ import annotations+import astimport posixpathimport refrom typing import TYPE_CHECKING, Any@@ -56,8 +57,369 @@ def path_literals(text: bytes) -> list[bytes]:return [m.group(1) for m in _PATH_LITERAL_RE.finditer(text)]+def _is_file_path_call(node: ast.AST) -> bool:+ """Matches Path(__file__) or pathlib.Path(__file__)."""+ if not isinstance(node, ast.Call):+ return False+ func = node.func+ is_path = (+ isinstance(func, ast.Name) and func.id == "Path"+ ) or (+ isinstance(func, ast.Attribute)+ and func.attr == "Path"+ and isinstance(func.value, ast.Name)+ and func.value.id == "pathlib"+ )+ if not is_path:+ return False+ return (+ len(node.args) == 1+ and isinstance(node.args[0], ast.Name)+ and node.args[0].id == "__file__"+ )+++def _strip_resolve(node: ast.AST) -> ast.AST | None:+ """Unwraps a zero-argument .resolve() or .absolute() call."""+ if (+ isinstance(node, ast.Call)+ and isinstance(node.func, ast.Attribute)+ and node.func.attr in ("resolve", "absolute")+ and not node.args+ ):+ return node.func.value+ return None+++def _get_anchor_n(node: ast.AST) -> int | None:+ """Return ancestor index n if node anchors on __file__, else None.++ n=0 is the directory containing the file (Path(__file__).parent).+ n=1 is 1 directory above, etc. Matches Path(__file__).parents[n],+ Path(__file__).parent.parent..., and .resolve().+ """+ unwrapped = _strip_resolve(node)+ if unwrapped is not None:+ node = unwrapped+ if (+ isinstance(node, ast.Subscript)+ and isinstance(node.value, ast.Attribute)+ and node.value.attr == "parents"+ ):+ base = node.value.value+ inner = _strip_resolve(base)+ if inner is not None:+ base = inner+ if (+ _is_file_path_call(base)+ and isinstance(node.slice, ast.Constant)+ and isinstance(node.slice.value, int)+ and node.slice.value >= 0+ ):+ return node.slice.value+ k = 0+ curr = node+ while isinstance(curr, ast.Attribute) and curr.attr == "parent":+ k += 1+ curr = curr.value+ if k > 0:+ inner = _strip_resolve(curr)+ if inner is not None:+ curr = inner+ if _is_file_path_call(curr):+ return k - 1+ return None+++def _climb_ancestor(test_path: str, n: int) -> str | None:+ """Resolve an ancestor of *test_path*'s directory by *n* steps up.++ n=0 is the directory containing test_path (same as Path(__file__).parent).+ n=1 is 1 level above that, etc. Returns '' for repository root.+ Returns None if n < 0 or n climbs past the repository root.+ """+ if n < 0:+ return None+ dir_part = posixpath.dirname(test_path)+ if not dir_part:+ return "" if n == 0 else None+ parts = dir_part.split("/")+ if n > len(parts):+ return None+ return "/".join(parts[: len(parts) - n])+++def _unroll_div_chain(node: ast.AST) -> tuple[ast.AST, list[ast.AST]]:+ """Unroll left-associative Division BinOp tree into (base, [right_nodes])."""+ chain = []+ curr = node+ while isinstance(curr, ast.BinOp) and isinstance(curr.op, ast.Div):+ chain.append(curr.right)+ curr = curr.left+ chain.reverse()+ return curr, chain+++class _Scope:+ def __init__(self, is_class: bool = False) -> None:+ self.bindings: dict[str, str | None] = {}+ self.is_class: bool = is_class+ self.globals: set[str] = set()+++def _bind_function_args(scope: _Scope, args: ast.arguments) -> None:+ def _bind_arg(arg: ast.arg | None) -> None:+ if arg is not None:+ scope.bindings[arg.arg] = None++ for a in getattr(args, "posonlyargs", []):+ _bind_arg(a)+ for a in args.args:+ _bind_arg(a)+ if args.vararg:+ _bind_arg(args.vararg)+ for a in args.kwonlyargs:+ _bind_arg(a)+ if args.kwarg:+ _bind_arg(args.kwarg)+++def python_ancestor_targets(test_path: str, text: bytes, path_set: set[str]) -> list[str]:+ """Indexed code files *test_path* names via Path(__file__) ancestor anchors or variables."""+ try:+ tree = ast.parse(text)+ except Exception:+ return []++ scopes: list[_Scope] = [_Scope()]+ found: dict[str, None] = {}++ def resolve_name(name: str) -> str | None:+ if name in scopes[-1].globals:+ return scopes[0].bindings.get(name)+ for idx in range(len(scopes) - 1, -1, -1):+ scope = scopes[idx]+ if idx < len(scopes) - 1 and scope.is_class:+ continue+ if name in scope.bindings:+ return scope.bindings[name]+ return None++ def resolve_expr(node: ast.AST) -> str | None:+ anchor_n = _get_anchor_n(node)+ if anchor_n is not None:+ return _climb_ancestor(test_path, anchor_n)+ if isinstance(node, ast.Name):+ return resolve_name(node.id)+ if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Div):+ base, chain = _unroll_div_chain(node)+ prefix = resolve_expr(base)+ if prefix is None:+ return None+ rel_parts: list[str] = []+ for part in chain:+ if isinstance(part, ast.Constant) and isinstance(part.value, str):+ rel_parts.append(part.value)+ else:+ return None+ if not rel_parts:+ return prefix+ rel_path = posixpath.normpath(posixpath.join(*rel_parts))+ return posixpath.normpath(posixpath.join(prefix, rel_path)) if prefix else rel_path+ return None++ def check_candidate(candidate: str | None) -> None:+ if (+ candidate+ and candidate.endswith(_CODE_EXTS)+ and candidate in path_set+ and _is_helper(candidate)+ and candidate != test_path+ ):+ found[candidate] = None++ class Visitor(ast.NodeVisitor):+ def _bind_or_invalidate(self, target: ast.AST, prefix: str | None) -> None:+ if isinstance(target, ast.Name):+ if target.id in scopes[-1].globals:+ scopes[0].bindings[target.id] = prefix+ else:+ scopes[-1].bindings[target.id] = prefix+ elif isinstance(target, (ast.Tuple, ast.List)):+ for elt in target.elts:+ self._bind_or_invalidate(elt, None)+ elif isinstance(target, ast.Starred):+ self._bind_or_invalidate(target.value, None)++ def visit_Assign(self, node: ast.Assign) -> None:+ prefix = resolve_expr(node.value)+ for target in node.targets:+ self._bind_or_invalidate(target, prefix)+ if prefix is not None:+ check_candidate(prefix)+ self.generic_visit(node)++ def visit_AnnAssign(self, node: ast.AnnAssign) -> None:+ prefix = resolve_expr(node.value) if node.value is not None else None+ self._bind_or_invalidate(node.target, prefix)+ if prefix is not None:+ check_candidate(prefix)+ self.generic_visit(node)++ def visit_AugAssign(self, node: ast.AugAssign) -> None:+ self._bind_or_invalidate(node.target, None)+ self.generic_visit(node)++ def visit_NamedExpr(self, node: ast.NamedExpr) -> None:+ prefix = resolve_expr(node.value)+ self._bind_or_invalidate(node.target, prefix)+ if prefix is not None:+ check_candidate(prefix)+ self.generic_visit(node)++ def visit_For(self, node: ast.For) -> None:+ self.visit(node.iter)+ self._bind_or_invalidate(node.target, None)+ for stmt in node.body:+ self.visit(stmt)+ for stmt in node.orelse:+ self.visit(stmt)++ def visit_AsyncFor(self, node: ast.AsyncFor) -> None:+ self.visit(node.iter)+ self._bind_or_invalidate(node.target, None)+ for stmt in node.body:+ self.visit(stmt)+ for stmt in node.orelse:+ self.visit(stmt)++ def visit_With(self, node: ast.With) -> None:+ for item in node.items:+ self.visit(item.context_expr)+ if item.optional_vars is not None:+ self._bind_or_invalidate(item.optional_vars, None)+ for stmt in node.body:+ self.visit(stmt)++ def visit_AsyncWith(self, node: ast.AsyncWith) -> None:+ for item in node.items:+ self.visit(item.context_expr)+ if item.optional_vars is not None:+ self._bind_or_invalidate(item.optional_vars, None)+ for stmt in node.body:+ self.visit(stmt)++ def visit_Global(self, node: ast.Global) -> None:+ scopes[-1].globals.update(node.names)+ self.generic_visit(node)++ def visit_Delete(self, node: ast.Delete) -> None:+ for target in node.targets:+ self._bind_or_invalidate(target, None)+ self.generic_visit(node)++ def _visit_function(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> None:+ for dec in node.decorator_list:+ self.visit(dec)+ for tp in getattr(node, "type_params", []):+ self.visit(tp)+ self.visit(node.args)+ if node.returns:+ self.visit(node.returns)+ func_scope = _Scope()+ for tp in getattr(node, "type_params", []):+ if hasattr(tp, "name"):+ func_scope.bindings[tp.name] = None+ _bind_function_args(func_scope, node.args)+ scopes.append(func_scope)+ try:+ for stmt in node.body:+ self.visit(stmt)+ finally:+ scopes.pop()++ def visit_FunctionDef(self, node: ast.FunctionDef) -> None:+ self._visit_function(node)++ def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:+ self._visit_function(node)++ def visit_Lambda(self, node: ast.Lambda) -> None:+ self.visit(node.args)+ lam_scope = _Scope()+ _bind_function_args(lam_scope, node.args)+ scopes.append(lam_scope)+ try:+ self.visit(node.body)+ finally:+ scopes.pop()++ def visit_ClassDef(self, node: ast.ClassDef) -> None:+ for dec in node.decorator_list:+ self.visit(dec)+ for tp in getattr(node, "type_params", []):+ self.visit(tp)+ for b in node.bases:+ self.visit(b)+ for kw in node.keywords:+ self.visit(kw)+ class_scope = _Scope(is_class=True)+ scopes.append(class_scope)+ try:+ for stmt in node.body:+ self.visit(stmt)+ finally:+ scopes.pop()++ def _visit_comprehension(+ self, node: ast.ListComp | ast.SetComp | ast.DictComp | ast.GeneratorExp+ ) -> None:+ comp_scope = _Scope()+ first_gen = node.generators[0] if node.generators else None+ if first_gen:+ self.visit(first_gen.iter)+ scopes.append(comp_scope)+ try:+ for idx, gen in enumerate(node.generators):+ if idx > 0:+ self.visit(gen.iter)+ self._bind_or_invalidate(gen.target, None)+ for if_expr in gen.ifs:+ self.visit(if_expr)+ if isinstance(node, ast.DictComp):+ self.visit(node.key)+ self.visit(node.value)+ else:+ self.visit(node.elt)+ finally:+ scopes.pop()++ def visit_ListComp(self, node: ast.ListComp) -> None:+ self._visit_comprehension(node)++ def visit_SetComp(self, node: ast.SetComp) -> None:+ self._visit_comprehension(node)++ def visit_DictComp(self, node: ast.DictComp) -> None:+ self._visit_comprehension(node)++ def visit_GeneratorExp(self, node: ast.GeneratorExp) -> None:+ self._visit_comprehension(node)++ def visit_BinOp(self, node: ast.BinOp) -> None:+ if isinstance(node.op, ast.Div):+ prefix = resolve_expr(node)+ check_candidate(prefix)+ self.generic_visit(node)++ Visitor().visit(tree)+ return list(found)++def path_string_targets(path: str, text: bytes, path_set: set[str]) -> list[str]:- """Indexed code files *text*, the source of *path*, names relative to its directory."""+ """Indexed code files *text*, the source of *path*, names relative to its directory+ or through Path(__file__) ancestor anchors.+ """base = posixpath.dirname(path)found: dict[str, None] = {}for literal in path_literals(text):@@ -65,6 +427,9 @@ def path_string_targets(path: str, text: bytes, path_set: set[str]) -> list[str]target = posixpath.normpath(posixpath.join(base, spec))if target in path_set and _is_helper(target) and target != path:found[target] = None+ if path.endswith(".py"):+ for target in python_ancestor_targets(path, text, path_set):+ found[target] = Nonereturn list(found)diff --git a/tests/unit/ingestion/test_test_wiring_edges.py b/tests/unit/ingestion/test_test_wiring_edges.pyindex 8c8199b7c..a5c06353a 100644--- a/tests/unit/ingestion/test_test_wiring_edges.py+++ b/tests/unit/ingestion/test_test_wiring_edges.py@@ -180,3 +180,223 @@ def test_path_string_edges_leave_from_test_files_only() -> None:)assert _add_path_string_edges(graph, parsed, ctx, ctx.path_set) == 1assert graph["tests/test_client.py"]["tests/_mock_server.py"]["hint_source"] == PATH_STRING_HINT+++def test_path_string_edges_link_repo_root_target() -> None:+ graph, parsed, ctx = _ctx(+ {+ "tests/unit/dead_code/test_confidence_parity.py": """+repo_root = Path(__file__).parents[3]+ts_file = repo_root / "packages" / "types" / "src" / "dead-code.ts"+""",+ "packages/types/src/dead-code.ts": "export const MEDIUM = 0.4;",+ }+ )+ assert _add_path_string_edges(graph, parsed, ctx, ctx.path_set) == 1+ assert (+ graph["tests/unit/dead_code/test_confidence_parity.py"][+ "packages/types/src/dead-code.ts"+ ]["hint_source"]+ == PATH_STRING_HINT+ )++++def test_ancestor_and_repo_root_paths_are_linked() -> None:+ path_set = {+ "tests/unit/dead_code/test_confidence_parity.py",+ "packages/types/src/dead-code.ts",+ "packages/types/src/doc-drift.ts",+ "packages/ui/src/docs/reader-persona.ts",+ "plugins/claude-code/hooks/lens/lens.js",+ "packages/data/sample.json",+ }+ # 1. Direct parents[n] chain inside a function+ src_local = b"""+def test_something():+ repo_root = Path(__file__).parents[3]+ ts_file = repo_root / "packages" / "types" / "src" / "dead-code.ts"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_local, path_set+ ) == ["packages/types/src/dead-code.ts"]++ # 2. Direct .resolve().parents[n] chained assignment at module level+ src_contract = b"""+_CONTRACT = (+ Path(__file__).resolve().parents[3] / "packages" / "types" / "src" / "doc-drift.ts"+)+"""+ assert path_string_targets(+ "tests/unit/doc_drift/test_ts_contract_parity.py", src_contract, path_set+ ) == ["packages/types/src/doc-drift.ts"]++ # 3. REPO_ROOT constant at module level used by another constant+ src_persona = b"""+REPO_ROOT = Path(__file__).resolve().parents[3]+READER_PERSONA_TS = REPO_ROOT / "packages" / "ui" / "src" / "docs" / "reader-persona.ts"+"""+ assert path_string_targets(+ "tests/unit/generation/test_structural_pages_read_like_a_wiki.py", src_persona, path_set+ ) == ["packages/ui/src/docs/reader-persona.ts"]++ # 4. Constant built from another constant (ROOT -> PLUGIN_ROOT -> bundle)+ src_plugin = b"""+ROOT = Path(__file__).resolve().parents[3]+PLUGIN_ROOT = ROOT / "plugins" / "claude-code"+def test_plugin():+ bundle = PLUGIN_ROOT / "hooks" / "lens" / "lens.js"+"""+ assert path_string_targets(+ "tests/unit/cli/test_claude_plugin.py", src_plugin, path_set+ ) == ["plugins/claude-code/hooks/lens/lens.js"]++ # 5. .parent.parent.parent.parent chain+ src_parent_chain = b"""+root = Path(__file__).parent.parent.parent.parent+target = root / "packages" / "types" / "src" / "dead-code.ts"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_parent_chain, path_set+ ) == ["packages/types/src/dead-code.ts"]++ # 6. Single string literal with slashes+ src_single = b"""+root = Path(__file__).parents[3]+target = root / "packages/types/src/dead-code.ts"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_single, path_set+ ) == ["packages/types/src/dead-code.ts"]++ # 7. Negative: variable in chain yields nothing+ src_var = b"""+root = Path(__file__).parents[3]+target = root / "packages" / var / "dead-code.ts"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_var, path_set+ ) == []++ # 8. Negative: n climbing past repo root yields nothing+ src_past_root = b"""+root = Path(__file__).parents[4]+target = root / "packages" / "types" / "src" / "dead-code.ts"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_past_root, path_set+ ) == []++ # 9. Negative: missing file yields nothing+ src_missing = b"""+root = Path(__file__).parents[3]+target = root / "packages" / "types" / "src" / "missing-file.ts"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_missing, path_set+ ) == []++ # 10. Negative: data file (.json) yields nothing+ src_data = b"""+root = Path(__file__).parents[3]+target = root / "packages" / "data" / "sample.json"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_data, path_set+ ) == []++ # 11. Negative: reassignment to dynamic expression invalidates tracked binding+ src_reassigned = b"""+root = Path(__file__).parents[3]+root = get_root()+target = root / "packages" / "types" / "src" / "dead-code.ts"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_reassigned, path_set+ ) == []++ # 12. Negative: annotated reassignment to dynamic expression invalidates tracked binding+ src_ann_reassigned = b"""+root = Path(__file__).parents[3]+root: Path = get_root()+target = root / "packages" / "types" / "src" / "dead-code.ts"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_ann_reassigned, path_set+ ) == []++ # 13. Negative: bare type annotation without assignment invalidates tracked binding+ src_ann_bare = b"""+root = Path(__file__).parents[3]+root: Path+target = root / "packages" / "types" / "src" / "dead-code.ts"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_ann_bare, path_set+ ) == []++ # 14. Negative: loop variable rebounds and invalidates tracked binding+ src_loop_rebound = b"""+root = Path(__file__).parents[3]+for root in get_roots():+ pass+target = root / "packages" / "types" / "src" / "dead-code.ts"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_loop_rebound, path_set+ ) == []++ # 15. Negative: function parameter shadows module-level ancestor binding+ src_param_shadowed = b"""+ROOT = Path(__file__).parents[3]+def f(ROOT):+ target = ROOT / "packages" / "types" / "src" / "dead-code.ts"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_param_shadowed, path_set+ ) == []++ # 16. Local function reassignment invalidates inside function but preserves outer binding+ src_local_preserve = b"""+ROOT = Path(__file__).parents[3]+def helper():+ ROOT = get_root()+ target1 = ROOT / "packages" / "types" / "src" / "dead-code.ts"++def test_fn():+ target2 = ROOT / "packages" / "types" / "src" / "dead-code.ts"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_local_preserve, path_set+ ) == ["packages/types/src/dead-code.ts"]++ # 17. Negative: comprehension loop variable shadows module-level ancestor binding+ src_comp_shadowed = b"""+ROOT = Path(__file__).parents[3]+targets = [ROOT / "packages" / "types" / "src" / "dead-code.ts" for ROOT in get_roots()]+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_comp_shadowed, path_set+ ) == []++ # 18. Negative: lambda parameter shadows module-level ancestor binding+ src_lambda_shadowed = b"""+ROOT = Path(__file__).parents[3]+fn = lambda ROOT: ROOT / "packages" / "types" / "src" / "dead-code.ts"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_lambda_shadowed, path_set+ ) == []++ # 19. Negative: context manager target invalidates tracked binding+ src_with_invalidated = b"""+ROOT = Path(__file__).parents[3]+with open("some_file") as ROOT:+ target = ROOT / "packages" / "types" / "src" / "dead-code.ts"+"""+ assert path_string_targets(+ "tests/unit/dead_code/test_confidence_parity.py", src_with_invalidated, path_set+ ) == []+++