Parse each mutants/ copy once when reporting mutmut's mutants, and kill more survivors with tests of by_folder, the in-diff tools' folder and their start failures

This commit is contained in:
2026-11-11 20:19:02 +01:00
parent 4c9bc080bb
commit 11804ac6aa
4 changed files with 79 additions and 28 deletions
+29 -8
View File
@@ -2,6 +2,9 @@
mutmut names a mutant `<module>.<key>__mutmut_<number>`. The key is x_<function> for a function at a module's top, and xǁ<class>ǁ<method> for a method of a class there; mutmut mutates no other code. Its mutants/ copy of a file holds the original of each function it mutates, as `<key>__mutmut_orig`, and each mutant as a copy of it."""
import ast
import functools
from typing import NamedTuple
from collections.abc import Sequence
def function_keys(source: str, lines: frozenset[int]) -> list[str]:
@@ -46,22 +49,40 @@ def mutated_line(source: str, copy: str, key: str, number: int) -> tuple[int, st
"""The source line a mutant changes, with that line as the mutant has it; None where the mutants/ copy lacks the mutant, or holds an original unlike the source.
A mutant keeps the original's lines, so the first line where they differ sits at the same offset from the source function's def."""
try:
function = defined(ast.parse(source), key)
copied = {node.name: node for node in ast.walk(ast.parse(copy)) if isinstance(node, (ast.AsyncFunctionDef, ast.FunctionDef))}
except SyntaxError:
module, copied = parsed(source), parsed(copy)
if module is None or copied is None:
return None
original, mutant = copied.get(f"{key}__mutmut_orig"), copied.get(f"{key}__mutmut_{number}")
function = defined(module.tree, key)
original, mutant = copied.functions.get(f"{key}__mutmut_orig"), copied.functions.get(f"{key}__mutmut_{number}")
if function is None or original is None or mutant is None:
return None
expected = as_named(source.splitlines(), function, function.name)
kept, changed = (as_named(copy.splitlines(), node, function.name) for node in (original, mutant))
expected = as_named(module.lines, function, function.name)
kept, changed = (as_named(copied.lines, node, function.name) for node in (original, mutant))
if kept != expected:
return None
offset = next((index for index, (was, now) in enumerate(zip(kept, changed)) if was != now), None)
return None if offset is None else (function.lineno + offset, changed[offset].strip())
class Parsed(NamedTuple):
tree: ast.Module
functions: dict[str, ast.AsyncFunctionDef | ast.FunctionDef]
lines: tuple[str, ...]
# mutated_line reads the same source and mutants/ copy for each of a file's mutants, and a copy holds every mutant, so parsing it
# anew for each one made the report take minutes.
@functools.lru_cache(maxsize=4)
def parsed(text: str) -> Parsed | None:
"""A module's tree, each function or method in it by name, and its lines; None where it isn't valid Python."""
try:
tree = ast.parse(text)
except SyntaxError:
return None
functions = {node.name: node for node in ast.walk(tree) if isinstance(node, (ast.AsyncFunctionDef, ast.FunctionDef))}
return Parsed(tree, functions, tuple(text.splitlines()))
def defined(tree: ast.Module, key: str) -> ast.AsyncFunctionDef | ast.FunctionDef | None:
"""The function or method mutmut's key names, from a module's top or a class there."""
if key.startswith("x_"):
@@ -75,7 +96,7 @@ def defined(tree: ast.Module, key: str) -> ast.AsyncFunctionDef | ast.FunctionDe
return next((node for node in scope if isinstance(node, (ast.AsyncFunctionDef, ast.FunctionDef)) and node.name == name), None)
def as_named(lines: list[str], function: ast.AsyncFunctionDef | ast.FunctionDef, name: str) -> list[str]:
def as_named(lines: Sequence[str], function: ast.AsyncFunctionDef | ast.FunctionDef, name: str) -> list[str]:
"""A function's lines from its def, decorators left out, with the def naming it name."""
spanned = lines[function.lineno - 1:function.end_lineno]
return [spanned[0].replace(function.name, name), *spanned[1:]]