Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/mutmut/mutation/file_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from mutmut.mutation.data import LineSpan
from mutmut.mutation.mutators import OPERATORS_TYPE
from mutmut.mutation.mutators import mutation_operators
from mutmut.mutation.mutators import operator_swap_op
from mutmut.mutation.pragma_handling import IgnoredCode
from mutmut.mutation.pragma_handling import get_ignored_lines
from mutmut.mutation.trampoline_templates import build_mutants_dict_and_name
Expand Down Expand Up @@ -188,6 +189,22 @@ def on_visit(self, node: cst.CSTNode) -> bool:
return True


class _MatchPatternUnaryCollector(cst.CSTVisitor):
"""Collect unary operations inside a match-case pattern.

Flipping `-`/`+` there can produce invalid syntax (e.g. `case -2`
becomes `case +2`), so those nodes are skipped by the mutators."""

def __init__(self) -> None:
super().__init__()
self.nodes: list[cst.UnaryOperation] = []

def on_visit(self, node: cst.CSTNode) -> bool:
if isinstance(node, cst.UnaryOperation):
self.nodes.append(node)
return True


class MutationVisitor(cst.CSTVisitor):
"""Iterate through all nodes in the module and create mutations for them.

Expand Down Expand Up @@ -217,11 +234,17 @@ def __init__(
)
self.ignored_classes: set[str] = set()
self.ignored_functions: set[str] = set()
self._match_pattern_unary_ops: set[int] = set()

def on_visit(self, node: cst.CSTNode) -> bool:
if self._skip_node_and_children(node):
return False

if isinstance(node, cst.MatchCase):
collector = _MatchPatternUnaryCollector()
node.pattern.visit(collector)
self._match_pattern_unary_ops.update(map(id, collector.nodes))

if self._should_mutate_node(node):
self._create_mutations(node)

Expand All @@ -231,6 +254,10 @@ def on_visit(self, node: cst.CSTNode) -> bool:
def _create_mutations(self, node: cst.CSTNode) -> None:
for t, operator in self._operators:
if isinstance(node, t):
if operator is operator_swap_op and id(node) in self._match_pattern_unary_ops:
# Swapping -/+ inside a match-case pattern can produce
# invalid syntax (e.g. `case -2` becomes `case +2`).
continue
for mutated_node in operator(node):
if self._removes_ignored_code(node, mutated_node):
continue
Expand Down
20 changes: 20 additions & 0 deletions tests/mutation/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,26 @@ def concat():
assert sorted(mutants) == []


def test_match_case_does_not_flip_unary_sign():
# Flipping `-` to `+` inside a match-case pattern produces `case +2`,
# which is a syntax error (see #583).
source = """
match x:
case -2:
y = 1
case _:
y = 2""".strip()

mutants = mutants_for_source(source)

assert not any("case +2" in mutant for mutant in mutants)
# Every generated mutant must still be valid syntax.
for mutant in mutants:
compile(mutant, "<mutant>", "exec")
# Other mutants (case drop, number bump, body changes) are unaffected.
assert any("case -3" in mutant for mutant in mutants)


def test_basic_class():
source = """
class Foo:
Expand Down