`pytest.approx` fails with `TypeError` when testing approximate value of a `decimal.Decimal`
See original GitHub issuedescription
When testing the approximate value of a decimal.Decimal
the following TypeError
occurrs
TypeError: unsupported operand type(s) for -: 'float' and 'decimal.Decimal'
I would expect pytest.approx
to test for approximate equality with a floating point value without having to either convert the decimal to a float or testing explicitly against another decimal.
pip list
Package Version
------------ -------
atomicwrites 1.4.0
attrs 20.3.0
colorama 0.4.4
iniconfig 1.1.1
packaging 20.9
pip 21.0.1
pluggy 0.13.1
py 1.10.0
pyparsing 2.4.7
pytest 6.2.2
setuptools 54.2.0
toml 0.10.2
pytest and os versions
pytest: 6.2.2 os: windows 10
minimal example
import decimal
import pytest
import math
3.14159 == pytest.approx(math.pi) # works
decimal.Decimal('3.14159') == pytest.approx(math.pi) # raises TypeError
Issue Analytics
- State:
- Created 2 years ago
- Comments:26 (19 by maintainers)
Top Results From Across the Web
pytest: assert almost equal - python - Stack Overflow
I noticed that this question specifically asked about pytest. pytest 3.0 includes an approx() function (well, really class) that is very ...
Read more >approx on Decimal value fails with default tolerance arguments
When using default tolerance on a Decimal, approx fails: >>> from pytest import approx >>> from decimal import Decimal ...
Read more >_pytest.python_api — pytest documentation
This is only a concern if the expected value is a sequence. ... the approx() function ensures that the expected value has #...
Read more >_pytest.python_api — pytest documentation - API Manual
__tracebackhide__ = True raise TypeError( "Comparison operators other than == and ... Perform approximate comparisons where the expected value is a decimal.
Read more >_pytest.python_api module — pytest API documentation
Perform approximate comparisons where the expected value is a decimal. ... This problem is commonly encountered when writing tests, e.g. when making sure ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, the tolerance is added to/subtracted from the expected value. So I agree that you shouldn’t be able to use a Decimal tolerance with a float expected value, or vice versa. But only comparisons are made between the actual value and the tolerance-adjusted expected value, so there’s no reason why the example given in this issue shouldn’t work:
This is more-or-less equivalent to:
It’s worth mentioning again that Decimal explicitly allows comparisons with float:
Since
approx()
only uses comparisons, it does make sense that this operation should succeed.