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:
Kale Kundert
2021-08-30 14:19:31 -04:00
committed by GitHub
parent 771c4b9313
commit af42e7154a
3 changed files with 26 additions and 0 deletions

View File

@@ -319,6 +319,12 @@ class TestApprox:
np_array = np.array(value)
assert repr(approx(np_array)) == expected_repr_string
def test_bool(self):
with pytest.raises(AssertionError) as err:
assert approx(1)
assert err.match(r"approx\(\) is not supported in a boolean context")
def test_operator_overloading(self):
assert 1 == approx(1, rel=1e-6, abs=1e-12)
assert not (1 != approx(1, rel=1e-6, abs=1e-12))