Skip to content

The agent that grows with you

fix(code-health): recursive-rename normalization honours nested scopes (F15)

Tekniumae60f7ccc16b
diff --git a/scripts/code_health/py_structure.py b/scripts/code_health/py_structure.py
index db4efbf1af4..8ed96efdba1 100644
--- a/scripts/code_health/py_structure.py
+++ b/scripts/code_health/py_structure.py
@@ -54,23 +54,86 @@ def nesting_depth(stmts: list[ast.stmt], depth: int = 0) -> int:
return deepest
+_SCOPES = (*_FUNCS, ast.Lambda, ast.ClassDef, ast.ListComp, ast.SetComp, ast.DictComp, ast.GeneratorExp)
+
+
+def _own_scope(node: ast.AST):
+ """Nodes in ``node``'s own scope: a nested def/lambda/class/comprehension is yielded but not
+ entered (its parameters and targets bind inside it, not here)."""
+ stack = list(ast.iter_child_nodes(node))
+ while stack:
+ child = stack.pop()
+ yield child
+ if not isinstance(child, _SCOPES):
+ stack.extend(ast.iter_child_nodes(child))
+
+
+def _bound_names(node: ast.AST) -> tuple[str, ...]:
+ if isinstance(node, ast.Name):
+ return () if isinstance(node.ctx, ast.Load) else (node.id,)
+ if isinstance(node, ast.arg):
+ return (node.arg,)
+ if isinstance(node, ast.alias):
+ return ((node.asname or node.name).split(".")[0],)
+ if isinstance(node, (ast.Global, ast.Nonlocal)):
+ return tuple(node.names)
+ # def/class names, `except ... as name`, match captures and `**rest`
+ found = (getattr(node, "name", None), getattr(node, "rest", None))
+ return tuple(n for n in found if isinstance(n, str))
+
+
+def _binds(scope: ast.AST, name: str) -> bool:
+ return any(name in _bound_names(child) for child in _own_scope(scope))
+
+
+def _self_references(node: ast.AST, name: str) -> list[ast.Name | ast.Attribute]:
+ """References in ``node`` to its own ``name``: ``name`` loads that no nested scope rebinds,
+ and ``self.name`` / ``cls.name``."""
+ refs: list[ast.Name | ast.Attribute] = []
+ todo = [node]
+ while todo:
+ for child in _own_scope(todo.pop()):
+ if isinstance(child, _SCOPES):
+ if not _binds(child, name):
+ todo.append(child)
+ elif isinstance(child, ast.Name) and child.id == name and isinstance(child.ctx, ast.Load):
+ refs.append(child)
+ elif (isinstance(child, ast.Attribute) and child.attr == name
+ and isinstance(child.ctx, ast.Load)
+ and isinstance(child.value, ast.Name) and child.value.id in ("self", "cls")):
+ refs.append(child)
+ return refs
+
+
def body_hash(node: ast.AST) -> str:
"""Name-independent hash, so a function moved or renamed unchanged keeps its cap.
The declared name is dropped, and so are the body's references to it (``name(...)``,
- ``self.name(...)``, ``cls.name(...)``), unless the body rebinds that name itself: a
- recursive function renamed together with its self-call is still the same code.
+ ``self.name(...)``, ``cls.name(...)``), unless the function's own scope rebinds that name:
+ a recursive function renamed together with its self-call is still the same code. A nested
+ scope that binds the name (``def identity(legacy)``) only keeps its own reads.
"""
name = getattr(node, "name", "")
- dumped = ast.dump(node, annotate_fields=False).replace(repr(name), "''", 1)
- if name and f"Name({name!r}, Store())" not in dumped and f"arg({name!r}" not in dumped:
- dumped = dumped.replace(f"Name({name!r}, Load())", "Name('', Load())")
- for receiver in ("self", "cls"):
- dumped = dumped.replace(f"Attribute(Name({receiver!r}, Load()), {name!r}, Load())",
- f"Attribute(Name({receiver!r}, Load()), '', Load())")
+ refs = _self_references(node, name) if name and not _binds(node, name) else []
+ # Blank the references in place for the dump, then put them back: the tree is shared.
+ saved = [(ref, ref.id if isinstance(ref, ast.Name) else ref.attr) for ref in refs]
+ for ref, _ in saved:
+ _rename(ref, "")
+ try:
+ dumped = ast.dump(node, annotate_fields=False).replace(repr(name), "''", 1)
+ finally:
+ for ref, original in saved:
+ _rename(ref, original)
return hashlib.sha1(dumped.encode("utf-8")).hexdigest()[:16]
+def _rename(ref: ast.Name | ast.Attribute, value: str) -> None:
+ if isinstance(ref, ast.Name):
+ ref.id = value
+ else:
+ ref.attr = value
+
+
def _first_line(node: ast.AST) -> int:
"""A def/class starts at its first decorator: decorator code belongs to it (and moves
with it), not to the module."""
diff --git a/tests/scripts/test_code_health_rules.py b/tests/scripts/test_code_health_rules.py
index 7d6995f20e5..f6541e6473d 100644
--- a/tests/scripts/test_code_health_rules.py
+++ b/tests/scripts/test_code_health_rules.py
@@ -209,3 +209,37 @@ _FUTURE = "from __future__ import annotations\n\n"
])
def test_annotation_evaluation_time(src, flagged):
assert bool(_hits("HX005", src)) is flagged, src
+
+
+# --- F15: only the function's own bindings stop the recursive-rename normalization ---
+
+def _recursive(name: str, inner: str = "legacy") -> str:
+ return (f"def {name}(n):\n def identity({inner}):\n return {inner}\n" + " pass\n" * 300
+ + f" return {name}(n-1) if n else identity(0)\n")
+
+
+def _hash(src: str) -> str:
+ return body_hash(ast.parse(src).body[0])
+
+
[email protected]("inner", ["legacy", "value"])
+def test_recursive_rename_ignores_nested_bindings(inner):
+ assert _hash(_recursive("legacy", inner)) == _hash(_recursive("renamed", inner))
+
+
+def test_recursive_rename_with_nested_parameter_keeps_its_cap(tmp_path, capsys):
+ code, out = _judge(tmp_path, capsys, {"pkg/r.py": _recursive("legacy")},
+ {"pkg/r.py": _recursive("renamed")})
+ assert code == 0, out
+
+
+def test_recursive_rename_controls():
+ # a rename that leaves the old self-call behind is different code
+ stale = _recursive("renamed").replace("return renamed(", "return legacy(")
+ assert _hash(_recursive("legacy")) != _hash(stale)
+ # a body that rebinds the name in its own scope refers to that local, not to itself
+ own = "def {0}(n):\n {0} = n\n return {0}\n"
+ assert _hash(own.format("legacy")) != _hash(own.format("renamed"))
+ # a closure's reference to the enclosing function is still a self-reference
+ closure = "def {0}(n):\n def helper():\n return {0}(n - 1)\n return helper()\n"
+ assert _hash(closure.format("legacy")) == _hash(closure.format("renamed"))