Skip to content
Merged
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
15 changes: 13 additions & 2 deletions src/mutmut/mutation/mutators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""This module contains the mutations for indidvidual nodes, e.g. replacing a != b with a == b."""

import cmath
import math
import re
from collections.abc import Callable
from collections.abc import Iterable
Expand All @@ -22,10 +24,19 @@


def operator_number(node: cst.BaseNumber) -> Iterable[cst.BaseNumber]:
# A float literal too large for a float, like 1e999 (a common way to spell
# infinity), evaluates to inf. inf + 1 is still inf, which has no literal
# form, so there is no number mutant to make.
if isinstance(node, cst.Integer | cst.Float):
yield node.with_changes(value=repr(node.evaluated_value + 1))
value = node.evaluated_value + 1
if isinstance(value, float) and math.isinf(value):
return
yield node.with_changes(value=repr(value))
elif isinstance(node, cst.Imaginary):
yield node.with_changes(value=repr(node.evaluated_value + 1j))
complex_value = node.evaluated_value + 1j
if cmath.isinf(complex_value):
return
yield node.with_changes(value=repr(complex_value))
else:
print("Unexpected number type", node)

Expand Down
5 changes: 5 additions & 0 deletions tests/mutation/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ def mutated_module(source: str) -> str:
("1.0j", "2j"),
("0o1", "2"),
("1.0e10", "10000000001.0"),
# too large for a float, so they evaluate to inf, which has no literal form
("1e999", []),
("1e999j", []),
("a = 1e999", "a = None"),
("1.5e400 + 2", ["1.5e400 - 2", "1.5e400 + 3"]),
("a = {x for x in y}", "a = None"),
("x+=1", ["x = 1", "x -= 1", "x+=2"]),
("x-=1", ["x = 1", "x += 1", "x-=2"]),
Expand Down
Loading