pytest-dev / pytest-dev/pytest
pytest.approx Decimal repr omits or misstates the effective tolerance
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
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.
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with testing/python/approx.py and the existing test_decimal_approx_float_rel test, then run the Decimal examples from the report to inspect repr and tolerance values. Add focused coverage for the three Decimal cases and verify that the displayed offsets match the effective comparison windows while existing tests continue to pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100