#15010·pytest

pytest.approx Decimal repr omits or misstates the effective tolerance

Author: torjan0Created Sep 13, 2026Updated Sep 13, 2026

For Decimal values, pytest.approx can display the raw relative factor instead of the effective tolerance, ignore a larger absolute tolerance, or show ??? with valid defaults.

python
from decimal import Decimal as D

import pytest

for options in (
    {"rel": D("0.01")},
    {"rel": D("0.01"), "abs": D("2")},
    {},
):
    obj = pytest.approx(D("100"), **options)
    print(repr(obj), obj.tolerance, sep=" | ")

Output:

100 ± 1.0e-2 | 1.00
100 ± 1.0e-2 | 2
100 ± ??? | 0.000100

The comparison windows are [99, 101], [98, 102], and [99.9999, 100.0001]: both endpoints compare equal, while 101.1, 102.1, and 100.00011, respectively, compare unequal. The comparisons are correct in these examples; the display does not communicate their tolerance.

Expected: the values after ± should represent offsets of 1, 2, and 0.0001. The float equivalent already displays 100.0 ± 1 for pytest.approx(100.0, rel=0.01).

The existing test_decimal_approx_float_rel explicitly expects the raw relative factor. The cases above additionally demonstrate the omitted dominating absolute tolerance and unknown default. Related #13530/#13543 address a FloatOperation exception during repr.

Reproduced on pytest 9.1.1 and development commit 3fd8675d6d798507c06cf9c60753be6d9d7b0e17, with CPython 3.12.3 on Linux 6.8.0-100-generic x86-64.

Installed packages (uv pip list --offline; this environment has no pip module):

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

GPT-6 Astra assisted; I manually reviewed this report.