Prevent approx from being used without a comparison (#9061)
Some of the top search-engine hits for pytest.approx use the function without actually comparing it to anything. This PR will cause these tests to fail by implementing approx.__bool__() to raise an AssertionError that briefly explains how to correctly use approx.
This commit is contained in:
@@ -100,6 +100,11 @@ class ApproxBase:
|
||||
a == self._approx_scalar(x) for a, x in self._yield_comparisons(actual)
|
||||
)
|
||||
|
||||
def __bool__(self):
|
||||
raise AssertionError(
|
||||
"approx() is not supported in a boolean context.\nDid you mean: `assert a == approx(b)`?"
|
||||
)
|
||||
|
||||
# Ignore type because of https://github.com/python/mypy/issues/4266.
|
||||
__hash__ = None # type: ignore
|
||||
|
||||
|
||||
Reference in New Issue
Block a user