Skip to content
Closed
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
1 change: 1 addition & 0 deletions changelog/15335.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed :func:`pytest.approx` raising :class:`decimal.InvalidOperation` when the actual value is a :class:`decimal.Decimal` NaN and the expected value is finite.
3 changes: 3 additions & 0 deletions src/_pytest/approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,9 @@ def __eq__(self, actual) -> bool:
if _is_nan(abs(self.expected)):
return self.nan_ok and _is_nan(abs(actual))

if _is_nan(abs(actual)):
return False

# Infinity shouldn't be approximately equal to anything but itself, but
# if there's a relative tolerance, it will be infinite and infinity
# will seem approximately equal to everything. The equal-to-itself
Expand Down
10 changes: 10 additions & 0 deletions testing/python/approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,16 @@ def test_decimal_outside_float_range(self):
assert expected != approx(Decimal("Infinity"))
assert Decimal("Infinity") != approx(expected)

def test_decimal_actual_nan(self):
"""Comparing a Decimal('NaN') actual value against approx() compares unequal (#15335)."""
assert Decimal("NaN") != approx(Decimal("1.1"))
assert Decimal("NaN") != approx(Decimal("1.1"), nan_ok=True)
assert Decimal("NaN") != approx(1.1)
assert [Decimal("NaN")] != approx([Decimal("1.1")])
assert {"val": Decimal("NaN")} != approx({"val": Decimal("1.1")})
assert Decimal("NaN") == approx(Decimal("NaN"), nan_ok=True)
assert Decimal("NaN") != approx(Decimal("NaN"), nan_ok=False)

def test_fraction(self):
within_1e6 = [
(1 + Fraction(1, 1000000), Fraction(1)),
Expand Down
Loading