Skip to content

Commit

Permalink
Bug fix #3593 - approx method in a single element of numpy array
Browse files Browse the repository at this point in the history
If the user pass as a expected value a numpy array created like
numpy.array(5); it will creates an array with one element without shape,
when used with approx it will raise an error
'TypeError: iteration over a 0-d array'
  • Loading branch information
marcelotrevisani committed Jun 25, 2018
1 parent 42b3125 commit 0030ceb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Maik Figura
Mandeep Bhutani
Manuel Krebber
Marc Schlaich
Marcelo Duarte Trevisani
Marcin Bachry
Mark Abramowitz
Markus Unterwaditzer
Expand Down
5 changes: 5 additions & 0 deletions changelog/3593.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
If the user pass as a expected value a numpy array created like
numpy.array(5); it will creates an array with one element without shape,
when used with approx it will raise an error for the `repr`
'TypeError: iteration over a 0-d array'. With this PR pytest will iterate
properly in the numpy array even with 0 dimension.
8 changes: 5 additions & 3 deletions src/_pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ def __repr__(self):
# shape of the array...
import numpy as np

return "approx({!r})".format(
list(self._approx_scalar(x) for x in np.asarray(self.expected))
)
list_scalars = []
for x in np.ndindex(self.expected.shape):
list_scalars.append(self._approx_scalar(np.asscalar(self.expected[x])))

return "approx({!r})".format(list_scalars)

if sys.version_info[0] == 2:
__cmp__ = _cmp_raises_type_error
Expand Down
19 changes: 17 additions & 2 deletions testing/python/approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ def report_failure(self, out, test, example, got):

class TestApprox(object):

def test_repr_string(self):
plus_minus = u"\u00b1" if sys.version_info[0] > 2 else u"+-"
@pytest.fixture
def plus_minus(self):
return u"\u00b1" if sys.version_info[0] > 2 else u"+-"

def test_repr_string(self, plus_minus):
tol1, tol2, infr = "1.0e-06", "2.0e-06", "inf"
assert repr(approx(1.0)) == "1.0 {pm} {tol1}".format(pm=plus_minus, tol1=tol1)
assert (
Expand Down Expand Up @@ -61,6 +64,18 @@ def test_repr_string(self):
),
)

def test_repr_0d_array(self, plus_minus):
np = pytest.importorskip("numpy")
np_array = np.array(5.)
assert approx(np_array) == 5.0
string_expected = "approx([5.0 {} 5.0e-06])".format(plus_minus)

assert repr(approx(np_array)) == string_expected

np_array = np.array([5.])
assert approx(np_array) == 5.0
assert repr(approx(np_array)) == string_expected

def test_operator_overloading(self):
assert 1 == approx(1, rel=1e-6, abs=1e-12)
assert not (1 != approx(1, rel=1e-6, abs=1e-12))
Expand Down

0 comments on commit 0030ceb

Please sign in to comment.