Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix in ApproxNumpy initialisation, use keywords for arguments to fix #3696

Merged
merged 10 commits into from
Jul 30, 2018
1 change: 1 addition & 0 deletions changelog/3695.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ApproxNumpy initialisation argument mixup. abs rel tolerances were previously flipped
5 changes: 4 additions & 1 deletion src/_pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ def __eq__(self, actual):
the pre-specified tolerance.
"""
if _is_numpy_array(actual):
return ApproxNumpy(actual, self.abs, self.rel, self.nan_ok) == self.expected
return (
ApproxNumpy(actual, rel=self.rel, abs=self.abs, nan_ok=self.nan_ok)
== self.expected
)

# Short-circuit exact equality.
if actual == self.expected:
Expand Down
14 changes: 14 additions & 0 deletions testing/python/approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,20 @@ def test_numpy_array(self):
assert actual == approx(list(expected), rel=5e-7, abs=0)
assert actual != approx(list(expected), rel=5e-8, abs=0)

def test_numpy_tolerance_args(self):
"""
quick check that numpy rel/abs args are handled correctly
for comparison against an np.array
"""
np = pytest.importorskip("numpy")
expected = 100
actual = 99
assert actual != pytest.approx(expected, abs=0.1, rel=0)
assert np.array(actual) != pytest.approx(expected, abs=0.1, rel=0)

assert actual == pytest.approx(expected, abs=0, rel=0.01)
assert np.array(actual) == pytest.approx(expected, abs=0, rel=0.1)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix @abrammer!

What do you think about improving this test so that it is even more complete?
I'm thinking about adding these complementary checks also:

        assert 0.099 == pytest.approx(0.1, abs=0.01, rel=0)
        assert np.array(0.099) == pytest.approx(0.1, abs=0.01, rel=0)
        # ... other variants with np.array in other positions ...

        assert 0.099 != pytest.approx(0.1, abs=0, rel=0.005)
        assert np.array(0.099) != pytest.approx(0.1, abs=0, rel=0.005)
        # ... other variants with np.array in other positions ...

also, perhaps, let's add the equivalent for all tests, but with pytest.approx on the left-side. It would be a total of 32 asserts, but the more tested it is, the better ;)

def test_numpy_array_wrong_shape(self):
np = pytest.importorskip("numpy")

Expand Down