The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(code-health): resolve import aliases per lexical scope (F12)
Teknium2cd844f1141f
diff --git a/scripts/code_health/py_rules.py b/scripts/code_health/py_rules.pyindex 61c73295ca1..97d3e4bcaa8 100644--- a/scripts/code_health/py_rules.py+++ b/scripts/code_health/py_rules.py@@ -39,40 +39,150 @@ class Ctx:known_env: set[str] = field(default_factory=set)+@dataclass+class _Scope:+ """One lexical scope's bindings: names an import binds, and names anything else binds."""++ parent: _Scope | None = None+ kind: str = "function" # "module" | "class" | "function" | "comprehension"+ imports: dict[str, str] = field(default_factory=dict)+ others: set[str] = field(default_factory=set)++ def resolve(self, name: str) -> str | None:+ """Import target ``name`` means here: the innermost scope binding it decides, and a+ scope that binds it any other way (or by two imports) makes it a plain local. Class+ bodies are invisible to the functions and comprehensions nested in them."""+ scope: _Scope | None = self+ while scope is not None:+ if scope is self or scope.kind != "class":+ if name in scope.others:+ return None+ if name in scope.imports:+ return scope.imports[name]+ scope = scope.parent+ return None+++class _Binder(ast.NodeVisitor):+ """Records each scope's bindings and the scope every loaded name is read in."""++ def __init__(self) -> None:+ self.module = self.scope = _Scope(kind="module")+ self.loads: dict[int, _Scope] = {}++ def _bind(self, name: str) -> None:+ self.scope.others.add(name)++ def _import(self, name: str, target: str) -> None:+ if self.scope.imports.get(name, target) != target:+ self._bind(name)+ self.scope.imports[name] = target++ def _enter(self, kind: str, nodes: Iterable[ast.AST]) -> None:+ outer, self.scope = self.scope, _Scope(self.scope, kind)+ for node in nodes:+ self.visit(node)+ self.scope = outer++ def visit_Import(self, node: ast.Import) -> None:+ for alias in node.names:+ top = alias.name.split(".")[0]+ self._import(alias.asname or top, alias.name if alias.asname else top)++ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:+ # A relative import keeps its module path minus the dots: rules match the leaf.+ for alias in node.names:+ if alias.name != "*":+ target = f"{node.module}.{alias.name}" if node.module else alias.name+ self._import(alias.asname or alias.name, target)++ def visit_Name(self, node: ast.Name) -> None:+ if isinstance(node.ctx, ast.Load):+ self.loads[id(node)] = self.scope+ else:+ self._bind(node.id)++ def visit_Global(self, node: ast.Global) -> None:+ self.scope.others.update(node.names)+ self.module.others.update(node.names)++ def visit_Nonlocal(self, node: ast.Nonlocal) -> None:+ scope: _Scope | None = self.scope+ while scope is not None and scope.kind != "module":+ scope.others.update(node.names)+ scope = scope.parent++ def generic_visit(self, node: ast.AST) -> None:+ # except-as, match captures and **rest bind a plain string attribute.+ for attr in ("name", "rest"):+ value = getattr(node, attr, None)+ if isinstance(value, str) and not isinstance(node, ast.alias):+ self._bind(value)+ super().generic_visit(node)++ def _visit_function(self, node: ast.FunctionDef | ast.AsyncFunctionDef | ast.Lambda) -> None:+ """Decorators, defaults and annotations run in the enclosing scope; the body does not."""+ args = node.args+ every = [*args.posonlyargs, *args.args, *args.kwonlyargs, args.vararg, args.kwarg]+ params = [a for a in every if a is not None]+ returns = getattr(node, "returns", None)+ outer = [*getattr(node, "decorator_list", []), *args.defaults,+ *(d for d in args.kw_defaults if d is not None),+ *(a.annotation for a in params if a.annotation), *([returns] if returns else [])]+ for expr in outer:+ self.visit(expr)+ if not isinstance(node, ast.Lambda):+ self._bind(node.name)+ self._enter("function", [*params, *(node.body if isinstance(node.body, list) else [node.body])])++ visit_FunctionDef = visit_AsyncFunctionDef = visit_Lambda = _visit_function++ def visit_arg(self, node: ast.arg) -> None:+ self._bind(node.arg)++ def visit_ClassDef(self, node: ast.ClassDef) -> None:+ for expr in (*node.decorator_list, *node.bases, *node.keywords):+ self.visit(expr)+ self._bind(node.name)+ self._enter("class", node.body)++ def _visit_comprehension(self, node: ast.ListComp | ast.SetComp | ast.DictComp | ast.GeneratorExp) -> None:+ """The first iterable runs outside; targets, conditions and elements run inside."""+ first, *rest = node.generators+ self.visit(first.iter)+ elements = [node.key, node.value] if isinstance(node, ast.DictComp) else [node.elt]+ inner = [first.target, *first.ifs, *(n for g in rest for n in (g.iter, g.target, *g.ifs))]+ self._enter("comprehension", [*inner, *elements])++ visit_ListComp = visit_SetComp = visit_DictComp = visit_GeneratorExp = _visit_comprehension++ def visit_NamedExpr(self, node: ast.NamedExpr) -> None:+ # A walrus in a comprehension binds in the enclosing function (PEP 572).+ scope = self.scope+ while scope.kind == "comprehension" and scope.parent is not None:+ scope = scope.parent+ scope.others.add(node.target.id)+ self.visit(node.value)++def canonical_tree(tree: ast.Module) -> ast.Module:"""``tree`` with every import-bound name spelled out: ``sp.run`` -> ``subprocess.run``,``execute`` (``from subprocess import run as execute``) -> ``subprocess.run``.Rules then match canonical API names, so an alias neither hides a call nor lets a local- helper that merely shares a leaf name (``def wait_for``) pass as the real API. A name the- module also rebinds (def, class, assignment, parameter) is left alone: it is ambiguous.+ helper that merely shares a leaf name (``def wait_for``) pass as the real API. Each read is+ resolved in its own scope: a parameter or local of the same name shadows the import only+ in the function that binds it, and a scope that rebinds the name itself (def, class,+ assignment, parameter) leaves it alone there, since the name is ambiguous."""- bound: dict[str, str] = {}- for node in ast.walk(tree):- if isinstance(node, ast.Import):- for alias in node.names:- if alias.asname:- bound[alias.asname] = alias.name- else:- top = alias.name.split(".")[0]- bound[top] = top- elif isinstance(node, ast.ImportFrom):- # A relative import keeps its module path minus the dots: rules match the leaf.- for alias in node.names:- if alias.name != "*":- target = f"{node.module}.{alias.name}" if node.module else alias.name- bound[alias.asname or alias.name] = target- rebound = {- n.id for n in ast.walk(tree) if isinstance(n, ast.Name) and not isinstance(n.ctx, ast.Load)- } | {n.arg for n in ast.walk(tree) if isinstance(n, ast.arg)} | {- n.name for n in ast.walk(tree) if isinstance(n, (*_FUNCS, ast.ClassDef))- }- names = {k: v for k, v in bound.items() if k not in rebound and k != v}+ binder = _Binder()+ binder.visit(tree)class _Spell(ast.NodeTransformer):def visit_Name(self, node: ast.Name) -> ast.AST:- target = names.get(node.id)- if target is None or not isinstance(node.ctx, ast.Load):+ scope = binder.loads.get(id(node))+ target = scope.resolve(node.id) if scope else None+ if target is None or target == node.id:return nodeparts = target.split(".")expr: ast.expr = ast.Name(parts[0], ast.Load())@@ -80,7 +190,7 @@ def canonical_tree(tree: ast.Module) -> ast.Module:expr = ast.Attribute(expr, part, ast.Load())return ast.copy_location(expr, node)- return ast.fix_missing_locations(_Spell().visit(tree)) if names else tree+ return ast.fix_missing_locations(_Spell().visit(tree))def _dotted(node: ast.AST) -> str:diff --git a/tests/scripts/test_code_health_rules.py b/tests/scripts/test_code_health_rules.pynew file mode 100644index 00000000000..7724d8e50f5--- /dev/null+++ b/tests/scripts/test_code_health_rules.py@@ -0,0 +1,55 @@+"""Rule and structure precision of the code-health ratchet (scripts/code_health): each case is a+reviewer's repro paired with the control that must keep its verdict."""++from __future__ import annotations++import ast++import pytest++from scripts.code_health.config import RULES_BY_ID+from scripts.code_health.py_rules import CHECKERS, Ctx, canonical_tree+from scripts.code_health.py_structure import body_hash, nesting_depth+from tests.scripts.test_code_health import _commit, _repo, _verdict+++def _hits(rule: str, src: str) -> list[int]:+ """Lines ``rule`` reports for ``src``, through the same canonical tree the measurer uses."""+ return sorted(set(CHECKERS[rule](canonical_tree(ast.parse(src)), Ctx())))+++def _judge(tmp_path, capsys, base_files: dict[str, str], head_files: dict[str, str | None]):+ repo, base = _repo(tmp_path)+ if base_files:+ base = _commit(repo, base_files)+ return _verdict(repo, base, head_files, capsys)+++# --- F12: an import alias is shadowed only in the scope that rebinds it ---++_ALIAS = "from subprocess import run as execute\n\n\n{}\n\n\ndef launch(cmd):\n{} return execute(cmd)\n"++[email protected]("elsewhere, local, flagged", [+ ("def identity(execute):\n return execute", "", True), # unrelated parameter+ ("def other():\n execute = 42\n return execute", "", True), # unrelated local+ ("def other():\n def execute():\n return 1\n return execute", "", True), # nested def+ ("def identity(x):\n return x", "", True),+ # genuine shadowing where the call is made still stops resolution+ ("def identity(x):\n return x", " execute = print\n", False),+ ("execute = print", "", False), # the module itself rebinds it: ambiguous+])+def test_import_alias_resolves_per_scope(elsewhere, local, flagged):+ src = _ALIAS.format(elsewhere, local)+ assert bool(_hits("HX006", src)) is flagged, src+++def test_import_alias_parameter_of_consuming_function_shadows():+ src = "from subprocess import run as execute\n\n\ndef launch(cmd, execute):\n return execute(cmd)\n"+ assert _hits("HX006", src) == []+++def test_import_alias_with_unrelated_parameter_blocks_through_the_verdict(tmp_path, capsys):+ head = {"pkg/c.py": _ALIAS.format("def identity(execute):\n return execute", "")}+ code, out = _judge(tmp_path, capsys, {}, head)+ assert code == 1 and "HX006" in out, out