Files
claude-config/bin/task_bench/python_bugs.py
T
2026-11-23 22:31:03 +01:00

60 lines
3.0 KiB
Python

"""The bugs task-bench plants in Python: mutmut's killed mutants, re-applied from the copy of each file it writes, and the message
pytest prints when a test fails on one."""
import re
from failure import Failure
from mutate_changes.mutmut_tool import killed_by_mutmut
from task_bench.planted_mutant import Mutant
from mutate_changes.tool_process import Run
# pytest's report of the failed tests, in its default form or with --tb=short: under this heading, a section per test.
FAILURES = re.compile(r"^=+ FAILURES =+$", re.MULTILINE)
# A section's heading, such as `_____ test_pick _____` or `_____ PickTest.test_empty _____`. A traceback's `_ _ _ _` between
# two frames is none.
SECTION = re.compile(r"^_+ (?!_)(.+?) _+$")
# The heading of what follows the failed tests, such as `===== short test summary info =====`.
NEXT_PART = re.compile(r"^=+ .+ =+$")
# A line of the error a test failed with: `E assert 3 == 4`, and `E + where 3 = pick([])` below it.
ERROR = re.compile(r"^E(?: (.*)|\s*)$")
# The heading of what a failed test printed, such as `----- Captured stdout call -----`, which may hold a line that looks like an error's.
CAPTURED = re.compile(r"^-+ Captured .+ -+$")
TEST_FILE = re.compile(r"\btest_\w*\.py|\w+_test\.py|\bconftest\.py")
def killed_mutants(top: str, files: list[str], run: Run, jobs: int) -> list[Mutant] | Failure:
"""The mutants in `files` that the tests of the tree at top kill, as mutmut finds them there."""
killed = killed_by_mutmut(top, files, run, jobs)
if isinstance(killed, Failure):
return killed
return [Mutant(path, change.line, f"{path}:{change.line} {name.rpartition('.')[2]}", encoded(change.was), encoded(change.now)) for path, name, change in killed]
def encoded(lines: tuple[str, ...]) -> tuple[bytes, ...]:
return tuple(line.encode() for line in lines)
def task_text(output: str) -> str | None:
"""What Haiku is told of a planted bug: the error of the first failed test that has one in pytest's output, without the test's
name or file, so it says what goes wrong and not which test shows it. None where pytest reports no failed test with an
error, as on a passing run, a file that fails to import or a run with --tb=no, or where the error only names the test."""
failures = FAILURES.split(output, maxsplit=1)
if len(failures) < 2:
return None
test, captured = "", False
error: list[str] = []
for line in failures[1].splitlines():
heading = SECTION.match(line)
if NEXT_PART.match(line) or heading and error:
break
if heading:
test, captured = heading[1], False
elif CAPTURED.match(line):
captured = True
elif test and not captured and (said := ERROR.match(line)):
error.append((said[1] or "").strip())
# A heading names a method after its class, and a parametrized test or a subtest before its `[case]` or ` (case)`.
whole = re.split(r"[\s\[(]", test)[0]
names = {whole, *whole.split(".")} - {""}
kept = [text for text in error if text and not TEST_FILE.search(text) and not any(name in text for name in names)]
return "\n".join(kept) or None