Skip to content

Commit

Permalink
Add fixes to make numpy.approx array-scalar comparisons work with o…
Browse files Browse the repository at this point in the history
…lder numpy versions
  • Loading branch information
tadeu committed Mar 14, 2018
1 parent 161d4e5 commit 97f9a8b
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions _pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class ApproxBase(object):
or sequences of numbers.
"""

# Tell numpy to use our `__eq__` operator instead of its when left side in a numpy array but right side is
# an instance of ApproxBase
# Tell numpy to use our `__eq__` operator instead of its
__array_ufunc__ = None
__array_priority__ = 100

def __init__(self, expected, rel=None, abs=None, nan_ok=False):
self.expected = expected
Expand Down Expand Up @@ -73,9 +73,6 @@ class ApproxNumpy(ApproxBase):
Perform approximate comparisons for numpy arrays.
"""

# Tell numpy to use our `__eq__` operator instead of its.
__array_priority__ = 100

def __repr__(self):
# It might be nice to rewrite this function to account for the
# shape of the array...
Expand Down Expand Up @@ -109,13 +106,13 @@ def _yield_comparisons(self, actual):

if np.isscalar(self.expected):
for i in np.ndindex(actual.shape):
yield actual[i], self.expected
yield np.asscalar(actual[i]), self.expected
elif np.isscalar(actual):
for i in np.ndindex(self.expected.shape):
yield actual, self.expected[i]
yield actual, np.asscalar(self.expected[i])
else:
for i in np.ndindex(self.expected.shape):
yield actual[i], self.expected[i]
yield np.asscalar(actual[i]), np.asscalar(self.expected[i])


class ApproxMapping(ApproxBase):
Expand Down Expand Up @@ -145,9 +142,6 @@ class ApproxSequence(ApproxBase):
Perform approximate comparisons for sequences of numbers.
"""

# Tell numpy to use our `__eq__` operator instead of its.
__array_priority__ = 100

def __repr__(self):
seq_type = type(self.expected)
if seq_type not in (tuple, list, set):
Expand Down

0 comments on commit 97f9a8b

Please sign in to comment.