From 42c84f4f30725e42b4ba3e0aa7480549666e2f01 Mon Sep 17 00:00:00 2001 From: Tadeu Manoel Date: Thu, 15 Mar 2018 13:41:58 -0300 Subject: [PATCH] Add fixes to `numpy.approx` array-scalar comparisons (from PR suggestions) --- _pytest/python_api.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/_pytest/python_api.py b/_pytest/python_api.py index aa847d649..e2c83aeab 100644 --- a/_pytest/python_api.py +++ b/_pytest/python_api.py @@ -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 @@ -100,9 +102,11 @@ class ApproxNumpy(ApproxBase): 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):