Backtestspytestrun-20261106-234554#15005
pytest.approx returns False for in-tolerance finite Decimal values above the float range
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches fix the root cause identically: they replace the two `math.isinf(abs(self.expected))` calls in `ApproxScalar.__repr__` and `__eq__` with a helper that uses `Decimal.is_infinite()` for Decimals and `math.isinf` otherwise, and all seven independent tests pass on both. Both add tests that would fail on base and pass after; Patch B's test is well targeted (positive and negative values, rel/abs/default tolerance, out-of-tolerance cases, sequences and mappings, true infinities still distinct). Patch A's `test_decimal_outside_float_range` covers the same ground slightly less thoroughly. The deciding difference is scope and risk. Patch B is a minimal, self-contained fix with a single changelog entry. Patch A bundles several unrelated changes: a new public API (`pytest.PytestApproxDecimalToleranceWarning`, exported and documented) that makes `approx(Decimal(...), rel=0.01)` start emitting a warning, which will break existing suites running with `-W error`; removal of `ApproxDecimal.__repr__` with changed repr output that required rewriting existing test expectations (`2.60 ± 1.0e-2` to `2.60 ± 2.6e-2`); a rework of `_repr_compare` accumulators across three classes; numpy object-dtype handling; and reordering of NaN/negative tolerance checks. None of this is asked for by the issue, it introduces user-visible behavior changes and new API surface, and it buries the two-line actual fix in a ~400-line diff, making review and bisection harder. Under the stated criteria, that extra work earns no credit and counts against A because it adds risk and obscures the fix. B is the better patch.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
4m 47s
Wall-clock time in the container
Full test suite
No new failures
4,533 passed · 54 skipped · 4,675 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
43vs 454 merged
Changed lines, ours vs merged
The issue
View on GitHubpytest.approx returns False for two finite Decimal values even though their exact difference is below the requested Decimal tolerance.
Reproducer
from decimal import Decimal
import pytest
expected = Decimal("1e400")
actual = Decimal("1.0000001e400")
rel = Decimal("0.000001")
difference = abs(actual - expected)
tolerance = rel * abs(expected)
print("difference:", difference)
print("tolerance:", tolerance)
print("both finite:", expected.is_finite() and actual.is_finite())
print("in tolerance:", difference < tolerance)
print("relative comparison:", actual == pytest.approx(expected, rel=rel))
print("absolute comparison:", actual == pytest.approx(expected, abs=tolerance))
Output on pytest 9.1.1:
difference: 1E+393
tolerance: 1E+394
both finite: True
in tolerance: True
relative comparison: False
absolute comparison: False
Both comparisons should be True. The same result occurs on current main at 3fd8675d6d798507c06cf9c60753be6d9d7b0e17.
This appears to happen because the infinity guard calls math.isinf(abs(self.expected)), which treats finite Decimal("1e400") as binary-float infinity before the exact Decimal tolerance is used.
Environment: CPython 3.12.3, Ubuntu 24.04.4 LTS, Linux 6.8.0-100-generic x86-64. Reproduced on pytest 9.1.1 and the current main commit above.
<details>
<summary>Installed packages</summary>
Package Version
---------------- -------
attrs 26.1.0
hypothesis 6.168.0
iniconfig 2.3.0
numpy 2.5.3
packaging 26.3
pluggy 1.6.0
pygments 2.21.0
pytest 9.1.1
sortedcontainers 2.4.0
</details>
The closest reports I found, #8495 and #3247, concern different Decimal failures.
AI tools assisted with discovery and independent verification. I reviewed and reproduced this issue.
The two patches
changelog/15005.bugfix.rstnew file mode 100644index 000000000..467a56f2c@@ -0,0 +1 @@+:func:`pytest.approx` now correctly compares finite :class:`~decimal.Decimal` values that are too large to be represented as a :class:`float`, instead of treating them as infinite and always reporting them as unequal.src/_pytest/approx.pyindex 780de1467..994a585ed 100644@@ -427,6 +427,19 @@ class ApproxSequenceLike(Approx[Sequence[Any]]): return zip(actual, self.expected, strict=True) +def _is_infinite(value: Complex | Decimal) -> bool:+ """Return whether a numeric value is infinite.++ Decimals are checked natively, because finite Decimals beyond the float+ range (e.g. ``Decimal("1e400")``) would overflow to ``inf`` in+ :func:`math.isinf`. The abs() call is for compatibility with complex+ numbers.+ """+ if isinstance(value, Decimal):+ return value.is_infinite()+ return math.isinf(abs(value))++ class ApproxScalar(Approx[ExpectedT]): """Perform approximate comparisons where the expected value is a single number.""" @@ -475,7 +488,7 @@ class ApproxScalar(Approx[ExpectedT]): if ( _is_bool(self.expected) or (not isinstance(self.expected, Complex | Decimal))- or math.isinf(abs(self.expected))+ or _is_infinite(self.expected) ): return str(self.expected) @@ -535,7 +548,7 @@ class ApproxScalar(Approx[ExpectedT]): # case would have been short circuited above, so here we can just # return false if the expected value is infinite. The abs() call is # for compatibility with complex numbers.- if math.isinf(abs(self.expected)):+ if _is_infinite(self.expected): return False # Return true if the two numbers are within the tolerance.testing/python/approx.pyindex b63a1c7a0..8bb878430 100644@@ -620,6 +620,31 @@ class TestApprox: assert approx(x, rel=0, abs=Decimal("5e-3")) == a assert approx(x, rel=0, abs=Decimal("5e-7")) != a + @pytest.mark.parametrize(+ ("actual", "expected"),+ [+ (Decimal("1.0000001e400"), Decimal("1e400")),+ (Decimal("-1.0000001e400"), Decimal("-1e400")),+ ],+ )+ def test_decimal_beyond_float_range(self, actual, expected):+ # Finite Decimals too large for a float must not be treated as+ # infinite (#15005).+ rel = Decimal("0.000001")+ tolerance = rel * abs(expected)+ assert actual == approx(expected, rel=rel)+ assert approx(expected, rel=rel) == actual+ assert actual == approx(expected, abs=tolerance)+ assert actual == approx(expected)+ assert actual != approx(expected, rel=Decimal("1e-8"), abs=0)+ assert actual != approx(expected, rel=0, abs=tolerance / 100)+ assert [actual] == approx([expected], rel=rel)+ assert {"a": actual} == approx({"a": expected}, rel=rel)+ # Real Decimal infinities are still only equal to themselves.+ assert Decimal("Infinity") != approx(expected)+ assert expected != approx(Decimal("Infinity"))+ assert Decimal("Infinity") == approx(Decimal("Infinity"))+ def test_fraction(self): within_1e6 = [ (1 + Fraction(1, 1000000), Fraction(1)),
changelog/15005.bugfix.rstnew file mode 100644index 000000000..d076abf2d@@ -0,0 +1 @@+:func:`pytest.approx` no longer treats finite :class:`~decimal.Decimal` values outside the :class:`float` range (such as ``Decimal("1e400")``) as infinite, which made in-tolerance comparisons fail.changelog/15006.bugfix.1.rstnew file mode 100644index 000000000..283599c09@@ -0,0 +1 @@+The failure message for :func:`pytest.approx` comparisons of :class:`~decimal.Decimal` collections no longer reports ``Max absolute difference: -inf``, or crashes for mappings, when the :class:`decimal.FloatOperation` trap is set.changelog/15006.bugfix.2.rstnew file mode 100644index 000000000..6320aaa97@@ -0,0 +1 @@+:func:`pytest.approx` no longer raises ``AttributeError`` when comparing :class:`numpy.ndarray` instances with ``object`` dtype, such as arrays of :class:`~decimal.Decimal`.changelog/15006.bugfix.rstnew file mode 100644index 000000000..b6f6b0fdb@@ -0,0 +1 @@+Restored the tolerance shown by :func:`pytest.approx` for :class:`~decimal.Decimal` values, which since 9.0 displayed ``???`` for the default tolerance and reported the raw ``rel`` ratio instead of the absolute tolerance actually compared against.changelog/15006.improvement.rstnew file mode 100644index 000000000..016040eeb@@ -0,0 +1 @@+:func:`pytest.approx` now emits :class:`pytest.PytestApproxDecimalToleranceWarning` when a ``rel`` or ``abs`` tolerance is given as a :class:`float` that cannot be represented exactly for a :class:`~decimal.Decimal` comparison, since such a tolerance is widened to the float's exact binary value.doc/en/reference/reference.rstindex 41dd95684..63c673547 100644@@ -1290,6 +1290,9 @@ Custom warnings generated in some situations such as improper usage or deprecate .. autoclass:: pytest.PytestWarning :show-inheritance: +.. autoclass:: pytest.PytestApproxDecimalToleranceWarning+ :show-inheritance:+ .. autoclass:: pytest.PytestAssertRewriteWarning :show-inheritance: src/_pytest/approx.pyindex 780de1467..70a881e33 100644@@ -12,6 +12,7 @@ from collections.abc import Sized from datetime import datetime from datetime import timedelta from decimal import Decimal+from decimal import FloatOperation import math from numbers import Complex import pprint@@ -22,6 +23,9 @@ from typing import SupportsAbs from typing import TYPE_CHECKING from typing import TypeGuard from typing import TypeVar+import warnings++from _pytest.warning_types import PytestApproxDecimalToleranceWarning if TYPE_CHECKING:@@ -30,6 +34,34 @@ else: ndarray = object +def _max_diff(current: Any, candidate: Any) -> Any:+ """Track a running maximum without seeding it with a value of another type.++ Seeding with ``-math.inf`` compares a float against whatever is being+ accumulated. For Decimal that comparison is exact, but it signals+ ``decimal.FloatOperation`` when that trap is set, and the sequence variant+ used to swallow the signal (``decimal.FloatOperation`` is a ``TypeError``+ subclass) and report ``-inf`` as the difference.+ """+ return candidate if current is None else max(current, candidate)+++def _vetted_diffs(+ max_abs_diff: Any, max_rel_diff: Any, rel_diff_is_infinite: bool+) -> tuple[Any, Any]:+ """Turn the accumulators into the values shown in the failure message.++ ``-math.inf`` means "no difference could be computed at all", which happens+ when every mismatched element is a non-number (#13012).+ """+ if rel_diff_is_infinite:+ max_rel_diff = math.inf+ return (+ -math.inf if max_abs_diff is None else max_abs_diff,+ -math.inf if max_rel_diff is None else max_rel_diff,+ )++ def _compare_approx( full_object: object, message_data: Sequence[tuple[str, str, str]],@@ -153,7 +185,6 @@ class ApproxNumpy(Approx[ndarray]): def _repr_compare(self, other_side: ndarray | list[Any]) -> list[str]: import itertools- import math def get_value_from_nested_list( nested_list: list[Any], nd_index: tuple[Any, ...]@@ -183,21 +214,26 @@ class ApproxNumpy(Approx[ndarray]): ] number_of_elements = self.expected.size- max_abs_diff = -math.inf- max_rel_diff = -math.inf+ max_abs_diff: Any = None+ max_rel_diff: Any = None+ rel_diff_is_infinite = False different_ids = [] for index in itertools.product(*(range(i) for i in np_array_shape)): approx_value = get_value_from_nested_list(approx_side_as_seq, index) other_value = get_value_from_nested_list(other_side_as_array, index) if approx_value != other_value: abs_diff = abs(approx_value.expected - other_value)- max_abs_diff = max(max_abs_diff, abs_diff)- if other_value == 0.0:- max_rel_diff = math.inf+ max_abs_diff = _max_diff(max_abs_diff, abs_diff)+ if other_value == 0:+ rel_diff_is_infinite = True else:- max_rel_diff = max(max_rel_diff, abs_diff / abs(other_value))+ max_rel_diff = _max_diff(max_rel_diff, abs_diff / abs(other_value)) different_ids.append(index) + max_abs_diff, max_rel_diff = _vetted_diffs(+ max_abs_diff, max_rel_diff, rel_diff_is_infinite+ )+ message_data = [ ( str(index),@@ -240,10 +276,13 @@ class ApproxNumpy(Approx[ndarray]): if np.isscalar(actual): for i in np.ndindex(self.expected.shape):- yield actual, self.expected[i].item()+ yield actual, _unbox_numpy_scalar(self.expected[i]) else: for i in np.ndindex(self.expected.shape):- yield actual[i].item(), self.expected[i].item()+ yield (+ _unbox_numpy_scalar(actual[i]),+ _unbox_numpy_scalar(self.expected[i]),+ ) class ApproxMapping(Approx[Mapping[Any, Any]]):@@ -270,7 +309,6 @@ class ApproxMapping(Approx[Mapping[Any, Any]]): return f"approx({ ({k: self._approx_scalar(v) for k, v in self.expected.items()})!r})" def _repr_compare(self, other_side: Mapping[object, float]) -> list[str]:- import math if len(self.expected) != len(other_side): return [@@ -289,23 +327,24 @@ class ApproxMapping(Approx[Mapping[Any, Any]]): } number_of_elements = len(approx_side_as_map)- max_abs_diff = -math.inf- max_rel_diff = -math.inf+ max_abs_diff: Any = None+ max_rel_diff: Any = None+ rel_diff_is_infinite = False different_ids = [] for approx_key, approx_value in approx_side_as_map.items(): other_value = other_side[approx_key] if approx_value != other_value: if approx_value.expected is not None and other_value is not None: try:- max_abs_diff = max(+ max_abs_diff = _max_diff( max_abs_diff, # TODO: The type error here seems correct. abs(approx_value.expected - other_value), # type: ignore[operator] )- if approx_value.expected == 0.0:- max_rel_diff = math.inf+ if approx_value.expected == 0:+ rel_diff_is_infinite = True else:- max_rel_diff = max(+ max_rel_diff = _max_diff( max_rel_diff, abs( # TODO: The type error here seems correct.@@ -317,6 +356,10 @@ class ApproxMapping(Approx[Mapping[Any, Any]]): pass different_ids.append(approx_key) + max_abs_diff, max_rel_diff = _vetted_diffs(+ max_abs_diff, max_rel_diff, rel_diff_is_infinite+ )+ message_data = [ (str(key), str(other_side[key]), str(approx_side_as_map[key])) for key in different_ids@@ -371,7 +414,6 @@ class ApproxSequenceLike(Approx[Sequence[Any]]): return f"approx({seq_type(self._approx_scalar(x) for x in self.expected)!r})" def _repr_compare(self, other_side: Sequence[float]) -> list[str]:- import math if len(self.expected) != len(other_side): return [@@ -382,8 +424,9 @@ class ApproxSequenceLike(Approx[Sequence[Any]]): approx_side_as_map = _recursive_sequence_map(self._approx_scalar, self.expected) number_of_elements = len(approx_side_as_map)- max_abs_diff = -math.inf- max_rel_diff = -math.inf+ max_abs_diff: Any = None+ max_rel_diff: Any = None+ rel_diff_is_infinite = False different_ids = [] for i, (approx_value, other_value) in enumerate( zip(approx_side_as_map, other_side, strict=True)@@ -391,16 +434,26 @@ class ApproxSequenceLike(Approx[Sequence[Any]]): if approx_value != other_value: try: abs_diff = abs(approx_value.expected - other_value)- max_abs_diff = max(max_abs_diff, abs_diff)+ max_abs_diff = _max_diff(max_abs_diff, abs_diff)+ # decimal.FloatOperation subclasses TypeError, so it would be+ # caught below and reported as a missing difference.+ except FloatOperation:+ raise # Ignore non-numbers for the diff calculations (#13012). except TypeError: pass else:- if other_value == 0.0:- max_rel_diff = math.inf+ if other_value == 0:+ rel_diff_is_infinite = True else:- max_rel_diff = max(max_rel_diff, abs_diff / abs(other_value))+ max_rel_diff = _max_diff(+ max_rel_diff, abs_diff / abs(other_value)+ ) different_ids.append(i)++ max_abs_diff, max_rel_diff = _vetted_diffs(+ max_abs_diff, max_rel_diff, rel_diff_is_infinite+ ) message_data = [ (str(i), str(other_side[i]), str(approx_side_as_map[i])) for i in different_ids@@ -475,14 +528,21 @@ class ApproxScalar(Approx[ExpectedT]): if ( _is_bool(self.expected) or (not isinstance(self.expected, Complex | Decimal))- or math.isinf(abs(self.expected))+ or _is_inf(abs(self.expected)) ): return str(self.expected) # If a sensible tolerance can't be calculated, self.tolerance will # raise a ValueError. In this case, display '???'. try:- if 1e-3 <= self.tolerance < 1e3:+ if isinstance(self.tolerance, Decimal):+ # Never let a Decimal meet a float literal: comparing against+ # 1e-3/1e3 signals decimal.FloatOperation when that trap is set+ # (#13530). Scientific notation is also the only readable choice+ # here, because a tolerance derived from a float carries its+ # full exact binary expansion (dozens of digits).+ vetted_tolerance = f"{self.tolerance:.1e}"+ elif 1e-3 <= self.tolerance < 1e3: vetted_tolerance = f"{self.tolerance:n}" else: vetted_tolerance = f"{self.tolerance:.1e}"@@ -490,7 +550,7 @@ class ApproxScalar(Approx[ExpectedT]): if ( isinstance(self.expected, Complex) and self.expected.imag- and not math.isinf(self.tolerance)+ and not _is_inf(self.tolerance) ): vetted_tolerance += " ∠ ±180°" except ValueError:@@ -526,8 +586,8 @@ class ApproxScalar(Approx[ExpectedT]): # Allow the user to control whether NaNs are considered equal to each # other or not. The abs() calls are for compatibility with complex # numbers.- if math.isnan(abs(self.expected)):- return self.nan_ok and math.isnan(abs(actual))+ if _is_nan(abs(self.expected)):+ return self.nan_ok and _is_nan(abs(actual)) # Infinity shouldn't be approximately equal to anything but itself, but # if there's a relative tolerance, it will be infinite and infinity@@ -535,7 +595,7 @@ class ApproxScalar(Approx[ExpectedT]): # case would have been short circuited above, so here we can just # return false if the expected value is infinite. The abs() call is # for compatibility with complex numbers.- if math.isinf(abs(self.expected)):+ if _is_inf(abs(self.expected)): return False # Return true if the two numbers are within the tolerance.@@ -557,12 +617,12 @@ class ApproxScalar(Approx[ExpectedT]): self.abs if self.abs is not None else self.DEFAULT_ABSOLUTE_TOLERANCE ) + if _is_nan(absolute_tolerance):+ raise ValueError("absolute tolerance can't be NaN.") if absolute_tolerance < 0: raise ValueError( f"absolute tolerance can't be negative: {absolute_tolerance}" )- if math.isnan(absolute_tolerance):- raise ValueError("absolute tolerance can't be NaN.") # If the user specified an absolute tolerance but not a relative one, # just return the absolute tolerance.@@ -581,12 +641,12 @@ class ApproxScalar(Approx[ExpectedT]): abs_expected: ExpectedT = abs(self.expected) # type: ignore[arg-type] relative_tolerance: float | Decimal = rel * abs_expected # type: ignore[operator] + if _is_nan(relative_tolerance):+ raise ValueError("relative tolerance can't be NaN.") if relative_tolerance < 0: raise ValueError( f"relative tolerance can't be negative: {relative_tolerance}" )- if math.isnan(relative_tolerance):- raise ValueError("relative tolerance can't be NaN.") # Return the larger of the relative and absolute tolerances. return max(relative_tolerance, absolute_tolerance)@@ -617,15 +677,6 @@ class ApproxDecimal(ApproxScalar[Decimal]): abs_ = abs super().__init__(expected, rel_, abs_, nan_ok) - def __repr__(self) -> str:- tol_str = "???"- if self.rel is not None and Decimal("1e-3") <= self.rel <= Decimal("1e3"):- tol_str = f"{self.rel:.1e}"- elif self.abs is not None:- tol_str = f"{self.abs:.1e}"-- return f"{self.expected} ± {tol_str}"- class ApproxTimedelta(Approx[datetime | timedelta]): """Perform approximate comparisons where the expected value is a@@ -954,6 +1005,8 @@ def approx( __tracebackhide__ = True + _warn_on_inexact_decimal_tolerance(expected, rel, abs)+ if isinstance(expected, Decimal): return ApproxDecimal(expected, rel=rel, abs=abs, nan_ok=nan_ok) # type: ignore[return-value] elif isinstance(expected, Mapping):@@ -971,6 +1024,55 @@ def approx( return ApproxScalar(expected, rel=rel, abs=abs, nan_ok=nan_ok) +def _contains_decimal(expected: object) -> bool:+ """Whether a comparison against ``expected`` will use Decimal arithmetic."""+ if isinstance(expected, Decimal):+ return True+ if isinstance(expected, Mapping):+ return any(isinstance(value, Decimal) for value in expected.values())+ if _is_sequence_like(expected):+ # ``.flat`` also makes a 0-d numpy array iterable, which it is not+ # otherwise, despite being sized and subscriptable.+ values = getattr(expected, "flat", expected)+ return any(isinstance(value, Decimal) for value in values)+ return False+++def _warn_on_inexact_decimal_tolerance(+ expected: object,+ rel: float | Decimal | timedelta | None,+ abs: float | Decimal | timedelta | None,+) -> None:+ """Warn when a float tolerance is used for a Decimal comparison.++ A float tolerance is converted with ``Decimal.from_float()``, which is+ exact and therefore keeps the float's full binary expansion: ``rel=0.01``+ really means a tolerance of ``0.010000000000000000208...``. That is wider+ than the ``0.01`` that was written, which defeats the point of comparing+ Decimals in the first place.++ Only floats that cannot be represented exactly are worth warning about;+ ``0.5`` and friends survive the conversion unchanged, as do ints.+ """+ inexact = [+ (name, value)+ for name, value in (("rel", rel), ("abs", abs))+ if type(value) is float and Decimal.from_float(value) != Decimal(repr(value))+ ]+ if not inexact or not _contains_decimal(expected):+ return+ for name, value in inexact:+ warnings.warn(+ PytestApproxDecimalToleranceWarning(+ f"{name}={value!r} cannot be represented exactly as a Decimal "+ f"and was widened to {Decimal.from_float(value)}.\n"+ f"Pass {name}=Decimal({str(value)!r}) to compare against the "+ f"tolerance you wrote."+ ),+ stacklevel=3,+ )++ def _is_sequence_like(expected: object) -> TypeGuard[Sequence[Any]]: return ( hasattr(expected, "__getitem__")@@ -1007,6 +1109,16 @@ def _as_numpy_array(obj: object) -> ndarray | None: return None +def _unbox_numpy_scalar(value: Any) -> Any:+ """Return the Python object behind a numpy scalar.++ Indexing an object-dtype array yields the stored Python object, which has+ no ``item()`` of its own -- only numpy's own scalar types need unboxing.+ """+ item = getattr(value, "item", None)+ return value if item is None else item()++ def _is_bool(val: Any) -> bool: # Check if `val` is a native bool or numpy bool. if isinstance(val, bool):@@ -1014,3 +1126,17 @@ def _is_bool(val: Any) -> bool: if np := sys.modules.get("numpy"): return isinstance(val, np.bool_) return False+++def _is_nan(val: Any) -> bool:+ # Decimal must not go through float(), which loses exactness and turns+ # out-of-float-range values into infinities (see #15005).+ if isinstance(val, Decimal):+ return val.is_nan()+ return math.isnan(val)+++def _is_inf(val: Any) -> bool:+ if isinstance(val, Decimal):+ return val.is_infinite()+ return math.isinf(val)src/_pytest/warning_types.pyindex 8fd62ff84..c5b339dec 100644@@ -134,6 +134,14 @@ class UnformattedWarning(Generic[_W]): return self.category(self.template.format(**kwargs)) +@final+class PytestApproxDecimalToleranceWarning(PytestWarning):+ """Warning emitted when :func:`pytest.approx` is given a float tolerance+ for a :class:`~decimal.Decimal` comparison."""++ __module__ = "pytest"++ @final class PytestFDWarning(PytestWarning): """When the lsof plugin finds leaked fds."""src/pytest/__init__.pyindex 6ff39f05a..f6ea24f95 100644@@ -79,6 +79,7 @@ from _pytest.subtests import Subtests from _pytest.terminal import TerminalReporter from _pytest.terminal import TestShortLogReport from _pytest.tmpdir import TempPathFactory+from _pytest.warning_types import PytestApproxDecimalToleranceWarning from _pytest.warning_types import PytestAssertRewriteWarning from _pytest.warning_types import PytestCacheWarning from _pytest.warning_types import PytestCollectionWarning@@ -130,6 +131,7 @@ __all__ = [ "OptionGroup", "Package", "Parser",+ "PytestApproxDecimalToleranceWarning", "PytestAssertRewriteWarning", "PytestCacheWarning", "PytestCollectionWarning",testing/python/approx.pyindex b63a1c7a0..e0bfc37f8 100644@@ -15,6 +15,7 @@ from operator import ne import re from _pytest.approx import _recursive_sequence_map+from _pytest.approx import ApproxScalar from _pytest.pytester import Pytester import pytest from pytest import approx@@ -620,6 +621,19 @@ class TestApprox: assert approx(x, rel=0, abs=Decimal("5e-3")) == a assert approx(x, rel=0, abs=Decimal("5e-7")) != a + def test_decimal_outside_float_range(self):+ """Decimals beyond the float range must not be treated as infinite (#15005)."""+ expected = Decimal("1e400")+ actual = Decimal("1.0000001e400")+ rel = Decimal("1e-6")+ assert actual == approx(expected, rel=rel)+ assert actual == approx(expected, abs=rel * expected)+ assert Decimal("2e400") != approx(expected, rel=rel)+ # Actual infinities keep behaving as before.+ assert Decimal("Infinity") == approx(Decimal("Infinity"))+ assert expected != approx(Decimal("Infinity"))+ assert Decimal("Infinity") != approx(expected)+ def test_fraction(self): within_1e6 = [ (1 + Fraction(1, 1000000), Fraction(1)),@@ -835,6 +849,13 @@ class TestApprox: assert op(a, approx(np.array(x))) assert op(np.array(a), approx(np.array(x))) + def test_numpy_actual_not_convertible(self):+ np = pytest.importorskip("numpy")++ with pytest.raises(TypeError, match=r"cannot compare .* to numpy\.ndarray"):+ # A ragged nested sequence has no array representation.+ [[1, 2], [3]] == approx(np.array([[1, 2], [3, 4]]))+ def test_numpy_array_wrong_shape(self): np = pytest.importorskip("numpy") @@ -844,6 +865,22 @@ class TestApprox: assert a12 != approx(a21) assert a21 != approx(a12) + @pytest.mark.parametrize(+ ("values", "offset"),+ (+ pytest.param([Decimal("1.0"), Decimal("2.0")], Decimal(5), id="decimal"),+ pytest.param([1.0, 2.0], 5.0, id="float"),+ pytest.param([Fraction(1), Fraction(2)], Fraction(5), id="fraction"),+ ),+ )+ def test_numpy_object_dtype(self, values, offset) -> None:+ """Object arrays hold plain Python objects, which have no item()."""+ np = pytest.importorskip("numpy")++ expected = np.array(values, dtype=object)+ assert expected == approx(expected)+ assert np.array([v + offset for v in values], dtype=object) != approx(expected)+ def test_numpy_array_implicit_conversion(self): np = pytest.importorskip("numpy") @@ -1064,17 +1101,200 @@ class TestApprox: monkeypatch.setitem(decimal.getcontext().traps, decimal.FloatOperation, True) approx_obj = pytest.approx(decimal.Decimal("2.60")) assert decimal.Decimal("2.600001") == approx_obj+ # Building the repr must not touch a float either (#13530); asserting+ # only the comparison above is what let #15006 slip through.+ assert repr(approx_obj) == "2.60 ± 2.6e-6" def test_decimal_approx_float_rel(self) -> None:- approx_obj = pytest.approx(decimal.Decimal("2.60"), rel=0.01)+ with pytest.warns(pytest.PytestApproxDecimalToleranceWarning):+ approx_obj = pytest.approx(decimal.Decimal("2.60"), rel=0.01) assert decimal.Decimal("2.600001") == approx_obj- assert repr(approx_obj) == "2.60 ± 1.0e-2"+ assert repr(approx_obj) == "2.60 ± 2.6e-2" def test_decimal_approx_float_abs(self) -> None:- approx_obj = pytest.approx(decimal.Decimal("2.60"), abs=0.01)+ with pytest.warns(pytest.PytestApproxDecimalToleranceWarning):+ approx_obj = pytest.approx(decimal.Decimal("2.60"), abs=0.01) assert decimal.Decimal("2.600001") == approx_obj assert repr(approx_obj) == "2.60 ± 1.0e-2" + @pytest.mark.parametrize(+ ("expected", "kwargs"),+ (+ pytest.param(Decimal("2.60"), {"rel": 0.01}, id="scalar-rel"),+ pytest.param(Decimal("2.60"), {"abs": 0.01}, id="scalar-abs"),+ pytest.param([Decimal("2.60")], {"rel": 0.01}, id="sequence"),+ pytest.param({"a": Decimal("2.60")}, {"rel": 0.01}, id="mapping"),+ ),+ )+ def test_inexact_float_tolerance_warns(self, expected, kwargs) -> None:+ """A float tolerance is widened to its exact binary value (#15006)."""+ name = next(iter(kwargs))+ with pytest.warns(+ pytest.PytestApproxDecimalToleranceWarning,+ match=rf"{name}=0\.01 cannot be represented exactly",+ ):+ approx(expected, **kwargs)++ @pytest.mark.parametrize(+ ("expected", "kwargs"),+ (+ pytest.param(2.60, {"rel": 0.01}, id="float-expected"),+ pytest.param([1.0, 2.0], {"rel": 0.01}, id="float-sequence"),+ pytest.param(Decimal("2.60"), {}, id="no-tolerance"),+ pytest.param(Decimal("2.60"), {"rel": Decimal("0.01")}, id="decimal-rel"),+ pytest.param(Decimal("2.60"), {"rel": 0.5}, id="exactly-representable"),+ pytest.param(Decimal("2.60"), {"rel": 1}, id="int-rel"),+ ),+ )+ def test_exact_tolerance_does_not_warn(self, expected, kwargs, recwarn) -> None:+ approx(expected, **kwargs)+ assert not [+ w+ for w in recwarn+ if issubclass(w.category, pytest.PytestApproxDecimalToleranceWarning)+ ]++ def test_inexact_float_tolerance_warns_once_at_the_call_site(+ self, pytester: Pytester+ ) -> None:+ """The warning must point at the user's line, not into approx itself."""+ pytester.makepyfile(+ """+ from decimal import Decimal+ import pytest++ def test_seq():+ values = [Decimal(i) for i in range(20)]+ assert values == pytest.approx(values, rel=0.01)+ """+ )+ result = pytester.runpytest("-Wdefault")+ result.assert_outcomes(passed=1, warnings=1)+ result.stdout.fnmatch_lines(+ ["*:6: PytestApproxDecimalToleranceWarning: rel=0.01 *"]+ )++ @pytest.mark.parametrize(+ ("kwargs", "expected_repr"),+ (+ ({}, "2.60 ± 2.6e-6"),+ ({"rel": Decimal("1e-6")}, "2.60 ± 2.6e-6"),+ ({"rel": Decimal("0.01")}, "2.60 ± 2.6e-2"),+ ({"abs": Decimal("0.01")}, "2.60 ± 1.0e-2"),+ ({"rel": Decimal("0.01"), "abs": Decimal(1)}, "2.60 ± 1.0e+0"),+ ),+ )+ def test_decimal_repr_shows_effective_tolerance(+ self, kwargs, expected_repr+ ) -> None:+ """The ± value is the band actually compared against, not ``rel`` (#15006)."""+ approx_obj = approx(Decimal("2.60"), **kwargs)+ assert isinstance(approx_obj, ApproxScalar)+ assert repr(approx_obj) == expected_repr+ assert Decimal("2.60") + approx_obj.tolerance == approx_obj++ def test_decimal_repr_outside_float_range(self) -> None:+ """Reprs must not go through float() any more than comparisons do (#15005)."""+ assert (+ repr(approx(Decimal("1e400"), rel=Decimal("1e-6"))) == "1E+400 ± 1.0e+394"+ )+ assert repr(approx(Decimal("1e-400"))) == "1E-400 ± 1.0e-12"++ def test_decimal_repr_nan_and_infinity(self) -> None:+ # Infinity is not compared using a tolerance, so none is shown.+ assert repr(approx(Decimal("Infinity"))) == "Infinity"+ # A NaN expected value has no sensible tolerance, as for float("nan").+ assert repr(approx(Decimal("NaN"))) == "NaN ± ???"+ assert repr(approx(nan)) == "nan ± ???"++ @pytest.mark.parametrize(+ ("other", "expected", "wanted"),+ (+ pytest.param(+ [Decimal(1), Decimal(2)],+ [Decimal(1), Decimal(9)],+ ["Max absolute difference: 7", "Max relative difference: 3.5"],+ id="sequence",+ ),+ pytest.param(+ [Decimal(0)],+ [Decimal(1)],+ ["Max absolute difference: 1", "Max relative difference: inf"],+ id="sequence-divide-by-zero",+ ),+ pytest.param(+ {"a": Decimal(1)},+ {"a": Decimal(9)},+ [+ "Max absolute difference: 8",+ "Max relative difference: 0.8888888888888888888888888889",+ ],+ id="mapping",+ ),+ pytest.param(+ {"a": Decimal(9)},+ {"a": Decimal(0)},+ ["Max absolute difference: 9", "Max relative difference: inf"],+ id="mapping-divide-by-zero",+ ),+ ),+ )+ def test_decimal_repr_compare_is_float_free(+ self, monkeypatch, other, expected, wanted+ ) -> None:+ """The failure message must not mix Decimals with float sentinels.++ The sequence variant used to report ``-inf`` here, because+ decimal.FloatOperation subclasses TypeError and was swallowed by the+ handler meant for non-numbers.+ """+ monkeypatch.setitem(decimal.getcontext().traps, decimal.FloatOperation, True)+ explanation = approx(expected)._repr_compare(other)+ assert explanation[1:3] == wanted++ def test_mapping_relative_diff_undefined(self) -> None:+ """A zero expected value that does not compare equal to 0.++ timedelta(0) != 0, so the divide-by-zero guard does not catch it and+ the relative difference stays unknown.+ """+ explanation = approx({"a": datetime.timedelta(0)}, rel=0.01)._repr_compare(+ {"a": datetime.timedelta(seconds=1)}+ )+ assert explanation[1:3] == [+ "Max absolute difference: 0:00:01",+ "Max relative difference: -inf",+ ]++ def test_mixed_decimal_and_float_sequence_does_not_hide_float_operation(+ self, monkeypatch+ ) -> None:+ """Comparing the running maximum can still mix a Decimal with a float.++ decimal.FloatOperation subclasses TypeError, so without the explicit+ re-raise it would be swallowed as a non-number and the reported maximum+ would silently be the smaller of the two differences.+ """+ expected = [Decimal(9), 9.0]+ other = [Decimal(1), 2.0]+ assert approx(expected)._repr_compare(other)[1] == "Max absolute difference: 8"++ monkeypatch.setitem(decimal.getcontext().traps, decimal.FloatOperation, True)+ with pytest.raises(decimal.FloatOperation):+ approx(expected)._repr_compare(other)++ def test_decimal_nan_tolerance_raises_value_error(self) -> None:+ """A Decimal NaN tolerance must not escape as decimal.InvalidOperation."""+ nan_abs = approx(Decimal(1), abs=Decimal("NaN"))+ assert isinstance(nan_abs, ApproxScalar)+ with pytest.raises(ValueError, match="absolute tolerance can't be NaN"):+ _ = nan_abs.tolerance++ nan_rel = approx(Decimal(1), rel=Decimal("NaN"))+ assert isinstance(nan_rel, ApproxScalar)+ with pytest.raises(ValueError, match="relative tolerance can't be NaN"):+ _ = nan_rel.tolerance+ def test_allow_ordered_sequences_only(self) -> None: """pytest.approx() should raise an error on unordered sequences (#9692).""" with pytest.raises(TypeError, match="only supports ordered sequences"):