Skip to content

The agent that grows with you

fix(code-health): HX006 post-kill exemption covers returned and annotated waits

Teknium5b5b4ebf6691
diff --git a/scripts/code_health/py_rules.py b/scripts/code_health/py_rules.py
index 0afab6dab90..4b0bef01669 100644
--- a/scripts/code_health/py_rules.py
+++ b/scripts/code_health/py_rules.py
@@ -633,6 +633,8 @@ def _process_kinds(tree: ast.Module) -> dict[int, str]:
# Process methods that wait for the child, with the slot of their ``timeout`` (sync Popen);
# the asyncio Process versions take no timeout and must be bounded by asyncio.
_PROCESS_WAITS = {"communicate": 1, "wait": 0}
+# A reaping wait's value may be dropped, assigned (annotated or not) or returned.
+_WAIT_STATEMENTS = (ast.Expr, ast.Assign, ast.AnnAssign, ast.Return)
def _reaped_after_kill(tree: ast.Module, kinds: dict[int, str]) -> set[int]:
@@ -647,7 +649,7 @@ def _reaped_after_kill(tree: ast.Module, kinds: dict[int, str]) -> set[int]:
continue
for first, second in zip(stmts, stmts[1:]):
kill = first.value if isinstance(first, ast.Expr) else None
- wait = second.value if isinstance(second, (ast.Expr, ast.Assign)) else None
+ wait = second.value if isinstance(second, _WAIT_STATEMENTS) else None
if not (isinstance(kill, ast.Call) and isinstance(wait, ast.Call)):
continue
head, _, leaf = _call_name(kill).rpartition(".")
diff --git a/tests/scripts/test_code_health_rules.py b/tests/scripts/test_code_health_rules.py
index c1dd16497e3..6cc1cc0528b 100644
--- a/tests/scripts/test_code_health_rules.py
+++ b/tests/scripts/test_code_health_rules.py
@@ -136,3 +136,35 @@ def test_process_handle_on_an_attribute_spans_the_class_methods():
closure = ("import subprocess\n\n\ndef f(c):\n proc = subprocess.Popen(c)\n\n"
" def reap():\n return proc.wait()\n return reap\n")
assert _hits("HX006", closure) == [8]
+
+
+# --- HX006: a wait right after kill() is bounded, however its value is used ---
+
+_KILL = "import asyncio\nimport subprocess\n\n\ndef reap(cmd, other):\n proc = subprocess.Popen(cmd)\n{}"
+
+
[email protected]("tail, flagged", [
+ (" proc.kill()\n proc.wait()\n", False),
+ (" proc.kill()\n rc = proc.wait()\n return rc\n", False),
+ (" proc.kill()\n return proc.wait()\n", False),
+ (" proc.kill()\n rc: int = proc.wait()\n return rc\n", False),
+ (" proc.kill()\n return proc.wait(timeout=5)\n", False),
+ (" return proc.wait()\n", True), # no preceding kill
+ (" other.kill()\n return proc.wait()\n", True), # a different receiver was killed
+ (" proc.kill()\n return proc.communicate()\n", True), # reads until grandchildren exit
+])
+def test_wait_after_kill(tail, flagged):
+ assert bool(_hits("HX006", _KILL.format(tail))) is flagged
+
+
+def test_async_wait_after_kill_stays_flagged():
+ src = ("import asyncio\n\n\nasync def f(c):\n proc = await asyncio.create_subprocess_exec(*c)\n"
+ " proc.kill()\n return await proc.wait()\n")
+ assert _hits("HX006", src) == [7]
+
+
+def test_assignment_refactored_into_return_after_kill_passes(tmp_path, capsys):
+ base = _KILL.format(" proc.kill()\n rc = proc.wait()\n return rc\n")
+ 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