Skip to content

Commit

Permalink
Add fixes to numpy.approx array-scalar comparisons (from PR suggest…
Browse files Browse the repository at this point in the history
…ions)
  • Loading branch information
tadeu committed Mar 15, 2018
1 parent 97f9a8b commit 42c84f4
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions _pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ class ApproxNumpy(ApproxBase):
def __repr__(self):
# It might be nice to rewrite this function to account for the
# shape of the array...
import numpy as np

return "approx({0!r})".format(list(
self._approx_scalar(x) for x in self.expected))
self._approx_scalar(x) for x in np.asarray(self.expected)))

if sys.version_info[0] == 2:
__cmp__ = _cmp_raises_type_error
Expand All @@ -100,9 +102,11 @@ def __eq__(self, actual):
def _yield_comparisons(self, actual):
import numpy as np

# We can be sure that `actual` is a numpy array, because it's
# casted in `__eq__` before being passed to `ApproxBase.__eq__`,
# which is the only method that calls this one.
# For both `actual` and `self.expected`, they can independently be
# either a `numpy.array` or a scalar (but both can't be scalar,
# in this case an `ApproxScalar` is used).
# They are treated in `__eq__` before being passed to
# `ApproxBase.__eq__`, which is the only method that calls this one.

if np.isscalar(self.expected):
for i in np.ndindex(actual.shape):
Expand Down

0 comments on commit 42c84f4

Please sign in to comment.