The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(code-health): HX009 follows annotated results and projects loop positions
Teknium9b6b48a88131
diff --git a/scripts/code_health/py_rules.py b/scripts/code_health/py_rules.pyindex 4b0bef01669..202b71f118e 100644--- a/scripts/code_health/py_rules.py+++ b/scripts/code_health/py_rules.py@@ -721,16 +721,31 @@ def _from_results(node: ast.AST | None, results: set[str]) -> bool:return False+def _projected(target: ast.AST, iterable: ast.AST, results: set[str]) -> set[str]:+ """Names in ``target`` that receive a result when it takes one item of ``iterable``.++ ``for payload, result in zip(payloads, results)`` taints only ``result`` and+ ``for i, r in enumerate(results)`` only ``r``: each position follows its own source."""+ if isinstance(target, (ast.Tuple, ast.List)) and isinstance(iterable, ast.Call):+ name, elts, args = _call_name(iterable), target.elts, iterable.args+ if name == "zip" and len(elts) == len(args):+ return set().union(*(_projected(e, a, results) for e, a in zip(elts, args, strict=True)))+ if name == "enumerate" and len(elts) == 2 and args:+ return _projected(elts[1], args[0], results)+ return _names(target) if _from_results(iterable, results) else set()++def _result_names(func: ast.AST) -> set[str]:"""Names bound to gather(return_exceptions=True) results in ``func``'s own body."""results: set[str] = set()nodes = [n for stmt in func.body for n in _eager(stmt)]for _ in range(3): # results -> loop vars -> unpacked loop varsfor node in nodes:- if isinstance(node, ast.Assign) and _from_results(node.value, results):- results |= set().union(*(_names(t) for t in node.targets))- elif isinstance(node, (ast.For, ast.AsyncFor, ast.comprehension)) and _from_results(node.iter, results):- results |= _names(node.target)+ if isinstance(node, (ast.Assign, ast.AnnAssign)) and _from_results(node.value, results):+ targets = node.targets if isinstance(node, ast.Assign) else [node.target]+ results |= set().union(*(_names(t) for t in targets))+ elif isinstance(node, (ast.For, ast.AsyncFor, ast.comprehension)):+ results |= _projected(node.target, node.iter, results)return resultsdiff --git a/tests/scripts/test_code_health_rules.py b/tests/scripts/test_code_health_rules.pyindex 6cc1cc0528b..23e347ca250 100644--- a/tests/scripts/test_code_health_rules.py+++ b/tests/scripts/test_code_health_rules.py@@ -168,3 +168,23 @@ def test_assignment_refactored_into_return_after_kill_passes(tmp_path, capsys):head = _KILL.format(" proc.kill()\n return proc.wait()\n")code, out = _judge(tmp_path, capsys, {"pkg/k.py": base}, {"pkg/k.py": head})assert code == 0, out+++# --- HX009: annotated results and projected loop variables ---++_GATHER = "import asyncio\n\n\nasync def run(awaitables, payloads):\n results{ann} = await asyncio.gather(\n *awaitables, return_exceptions=True\n )\n{loop}"++[email protected]("ann, loop, flagged", [+ (": list", " for result in results:\n if isinstance(result, Exception):\n raise result\n", True),+ ("", " for result in results:\n if isinstance(result, Exception):\n raise result\n", True),+ ("", " for payload, result in zip(payloads, results):\n if isinstance(result, Exception):\n raise result\n", True),+ ("", " for payload, result in zip(payloads, results):\n if isinstance(payload, Exception):\n raise payload\n", False),+ ("", " for payload, result in zip(payloads, results, strict=True):\n if isinstance(payload, Exception):\n raise payload\n", False),+ ("", " for i, r in enumerate(results):\n if isinstance(r, Exception):\n raise r\n", True),+ ("", " for i, r in enumerate(results):\n if isinstance(i, Exception):\n raise i\n", False),+ ("", " return [r for i, r in enumerate(results) if isinstance(i, Exception)]\n", False),+ (": list", " return [r for i, r in enumerate(results) if isinstance(r, Exception)]\n", True),+])+def test_gather_results_projection(ann, loop, flagged):+ assert bool(_hits("HX009", _GATHER.format(ann=ann, loop=loop))) is flagged