You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems that pytest.approx repr is optimized for very high or very low tolerances, but is not as easily readable as it could be for values in between.
Some examples:
pytest.approx(42, abs=1) repr is 42 ± 1.0e+00, which could be just 42 ± 1.
pytest.approx(5, rel=0.01) repr is 5 ± 5.0e-02, but could be 5 ± 0.05 or maybe even 5 ± 1%
pytest.approx(24000, abs=500) repr is 24000 ± 5.0e+02 and could be 24000 ± 500
removing accuracy example: pytest.approx(1500, abs=555) repr is 1500 ± 5.6e+02 and could be 1500 ± 555
In these number ranges using the exact number instead of the exponential notation is both more accurate and easier readable for humans. Please adapt pytest.approx to switch it's repr style according to the input value ranges.
I think this change has a regression on line 409. I am catching a FloatOperation exception as this change is comparing a float to a Decimal.
self=<[FloatOperation([<class'decimal.FloatOperation'>]) raisedinrepr()] ApproxDecimalobjectat0xffffa0403e00>def__repr__(self) ->str:
"""Return a string communicating both the expected value and the tolerance for the comparison being made. For example, ``1.0 ± 1e-6``, ``(3+4j) ± 5e-6 ∠ ±180°``. """# Don't show a tolerance for values that aren't compared using# tolerances, i.e. non-numerics and infinities. Need to call abs to# handle complex numbers, e.g. (inf + 1j).if (
isinstance(self.expected, bool)
or (notisinstance(self.expected, (Complex, Decimal)))
ormath.isinf(abs(self.expected) orisinstance(self.expected, bool))
):
returnstr(self.expected)
# If a sensible tolerance can't be calculated, self.tolerance will# raise a ValueError. In this case, display '???'.try:
>if1e-3<=self.tolerance<1e3: # <--- This is the exact line causing the FloatOperation^^^^^^^^^^^^^^^^^^^^^^^^^^^^Edecimal.FloatOperation: [<class'decimal.FloatOperation'>]
/usr/local/lib/python3.13/site-packages/_pytest/python_api.py:405: FloatOperation
Activity
hodevin commentedon Mar 30, 2020
Hey, my team and I would like to look over this issue and resolve it if that's okay.
kalekundert commentedon Mar 30, 2020
Of course, PRs are always welcome! Hopefully this one won't be too hard, either.
Zac-HD commentedon Mar 31, 2020
See https://docs.python.org/3/library/string.html#format-specification-mini-language - you probably want to use the
nformat specifier.henzef commentedon Apr 13, 2022
fazeelghafoor commentedon Jul 28, 2024
@Zac-HD is this issue still open, I would like to work on it
Improve approx repr for better readability (pytest-dev#12665)
rishid commentedon Jun 17, 2025
I think this change has a regression on line 409. I am catching a FloatOperation exception as this change is comparing a float to a Decimal.
chore(deps): update dependency pytest to v9 (#305)