Skip to content

Commit f0db64a

Browse files
author
abrammer
committed
drop the duplicate approx call
update test to include both np.array(actual) and np.array(expected)
1 parent 514ca6f commit f0db64a

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/_pytest/python_api.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,9 @@ def __eq__(self, actual):
211211
the pre-specified tolerance.
212212
"""
213213
if _is_numpy_array(actual):
214-
return (
215-
ApproxNumpy(actual, rel=self.rel, abs=self.abs, nan_ok=self.nan_ok)
216-
== self.expected
217-
)
214+
import numpy as np
215+
216+
return np.all(abs(self.expected - actual) <= self.tolerance)
218217

219218
# Short-circuit exact equality.
220219
if actual == self.expected:

testing/python/approx.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,22 @@ def test_numpy_tolerance_args(self):
346346
"""
347347
quick check that numpy rel/abs args are handled correctly
348348
for comparison against an np.array
349+
- 3.6.4 would approx both actual / expected if np.array
350+
regardless of which value was passed to approx()
351+
Means tolerance could be calculated against bad test return
349352
"""
350353
np = pytest.importorskip("numpy")
351354
expected = 100
352355
actual = 99
353356
assert actual != pytest.approx(expected, abs=0.1, rel=0)
354357
assert np.array(actual) != pytest.approx(expected, abs=0.1, rel=0)
358+
assert actual != pytest.approx(np.array(expected), abs=0.1, rel=0)
359+
assert np.array(actual) != pytest.approx(np.array(expected), abs=0.1, rel=0)
355360

356361
assert actual == pytest.approx(expected, abs=0, rel=0.01)
357-
assert np.array(actual) == pytest.approx(expected, abs=0, rel=0.1)
362+
assert np.array(actual) == pytest.approx(expected, abs=0, rel=0.01)
363+
assert actual == pytest.approx(np.array(expected), abs=0, rel=0.01)
364+
assert np.array(actual) == pytest.approx(np.array(expected), abs=0, rel=0.01)
358365

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

0 commit comments

Comments
 (0)