8000 MAINT: pytest cleanups · Pull Request #10294 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: pytest cleanups #10294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion numpy/core/tests/test_mem_overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_overlapping_assignments():
srcidx = tuple([a[0] for a in ind])
dstidx = tuple([a[1] for a in ind])

yield _check_assignment, srcidx, dstidx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the parameterize decorator here instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried that; doesn't work with nose. I can drop the second commit if you want, but a similar PR was merged previously.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I guess this is fine. Deprecating this style of test feels like a step backwards to me, but that's beyond our control.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added limited support for parameters on nose in nose_tools just for some of these cases. It doesn't have the full capabilities of the pytest version, however.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. parametrize doesn't even replace yield because it must be static on collection. I looked at other examples and the same thing was done as here. Complain to the pytest folks if you want.

_check_assignment(srcidx, dstidx)


@dec.slow
Expand Down
12 changes: 6 additions & 6 deletions numpy/core/tests/test_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_float_types():

"""
for t in [np.float32, np.double, np.longdouble]:
yield check_float_type, t
check_float_type(t)

def check_nan_inf_float(tp):
for x in [np.inf, -np.inf, np.nan]:
Expand All @@ -56,7 +56,7 @@ def test_nan_inf_float():

"""
for t in [np.float32, np.double, np.longdouble]:
yield check_nan_inf_float, t
check_nan_inf_float(t)

def check_complex_type(tp):
for x in [0, 1, -1, 1e20]:
Expand Down Expand Up @@ -84,7 +84,7 @@ def test_complex_types():

"""
for t in [np.complex64, np.cdouble, np.clongdouble]:
yield check_complex_type, t
check_complex_type(t)

def test_complex_inf_nan():
"""Check inf/nan formatting of complex types."""
Expand All @@ -108,7 +108,7 @@ def test_complex_inf_nan():
}
for tp in [np.complex64, np.cdouble, np.clongdouble]:
for c, s in TESTS.items():
yield _check_complex_inf_nan, c, s, tp
_check_complex_inf_nan(c, s, tp)

def _check_complex_inf_nan(c, s, dtype):
assert_equal(str(dtype(c)), s)
Expand Down Expand Up @@ -164,12 +164,12 @@ def check_complex_type_print(tp):
def test_float_type_print():
"""Check formatting when using print """
for t in [np.float32, np.double, np.longdouble]:
yield check_float_type_print, t
check_float_type_print(t)

def test_complex_type_print():
"""Check formatting when using print """
for t in [np.complex64, np.cdouble, np.clongdouble]:
yield check_complex_type_print, t
check_complex_type_print(t)

def test_scalar_format():
"""Test the str.format method with NumPy scalar types"""
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/tests/test_scalarmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def test_float_repr(self):
# long double test cannot work, because eval goes through a python
# float
for t in [np.float32, np.float64]:
yield self._test_type_repr, t
self._test_type_repr(t)


if not IS_PYPY:
Expand Down
70 changes: 35 additions & 35 deletions numpy/core/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -2335,53 +2335,53 @@ def test_precisions_consistent(self):

def test_branch_cuts(self):
# check branch cuts and continuity on them
yield _check_branch_cut, np.log, -0.5, 1j, 1, -1, True
yield _check_branch_cut, np.log2, -0.5, 1j, 1, -1, True
yield _check_branch_cut, np.log10, -0.5, 1j, 1, -1, True
yield _check_branch_cut, np.log1p, -1.5, 1j, 1, -1, True
yield _check_branch_cut, np.sqrt, -0.5, 1j, 1, -1, True
_check_branch_cut(np.log, -0.5, 1j, 1, -1, True)
_check_branch_cut(np.log2, -0.5, 1j, 1, -1, True)
_check_branch_cut(np.log10, -0.5, 1j, 1, -1, True)
_check_branch_cut(np.log1p, -1.5, 1j, 1, -1, True)
_check_branch_cut(np.sqrt, -0.5, 1j, 1, -1, True)

yield _check_branch_cut, np.arcsin, [ -2, 2], [1j, 1j], 1, -1, True
yield _check_branch_cut, np.arccos, [ -2, 2], [1j, 1j], 1, -1, True
yield _check_branch_cut, np.arctan, [0-2j, 2j], [1, 1], -1, 1, True
_check_branch_cut(np.arcsin, [ -2, 2], [1j, 1j], 1, -1, True)
_check_branch_cut(np.arccos, [ -2, 2], [1j, 1j], 1, -1, True)
_check_branch_cut(np.arctan, [0-2j, 2j], [1, 1], -1, 1, True)

yield _check_branch_cut, np.arcsinh, [0-2j, 2j], [1, 1], -1, 1, True
yield _check_branch_cut, np.arccosh, [ -1, 0.5], [1j, 1j], 1, -1, True
yield _check_branch_cut, np.arctanh, [ -2, 2], [1j, 1j], 1, -1, True
_check_branch_cut(np.arcsinh, [0-2j, 2j], [1, 1], -1, 1, True)
_check_branch_cut(np.arccosh, [ -1, 0.5], [1j, 1j], 1, -1, True)
_check_branch_cut(np.arctanh, [ -2, 2], [1j, 1j], 1, -1, True)

# check against bogus branch cuts: assert continuity between quadrants
yield _check_branch_cut, np.arcsin, [0-2j, 2j], [ 1, 1], 1, 1
yield _check_branch_cut, np.arccos, [0-2j, 2j], [ 1, 1], 1, 1
yield _check_branch_cut, np.arctan, [ -2, 2], [1j, 1j], 1, 1
_check_branch_cut(np.arcsin, [0-2j, 2j], [ 1, 1], 1, 1)
_check_branch_cut(np.arccos, [0-2j, 2j], [ 1, 1], 1, 1)
_check_branch_cut(np.arctan, [ -2, 2], [1j, 1j], 1, 1)

yield _check_branch_cut, np.arcsinh, [ -2, 2, 0], [1j, 1j, 1], 1, 1
yield _check_branch_cut, np.arccosh, [0-2j, 2j, 2], [1, 1, 1j], 1, 1
yield _check_branch_cut, np.arctanh, [0-2j, 2j, 0], [1, 1, 1j], 1, 1
_check_branch_cut(np.arcsinh, [ -2, 2, 0], [1j, 1j, 1], 1, 1)
_check_branch_cut(np.arccosh, [0-2j, 2j, 2], [1, 1, 1j], 1, 1)
_check_branch_cut(np.arctanh, [0-2j, 2j, 0], [1, 1, 1j], 1, 1)

def test_branch_cuts_complex64(self):
# check branch cuts and continuity on them
yield _check_branch_cut, np.log, -0.5, 1j, 1, -1, True, np.complex64
yield _check_branch_cut, np.log2, -0.5, 1j, 1, -1, True, np.complex64
yield _check_branch_cut, np.log10, -0.5, 1j, 1, -1, True, np.complex64
yield _check_branch_cut, np.log1p, -1.5, 1j, 1, -1, True, np.complex64
yield _check_branch_cut, np.sqrt, -0.5, 1j, 1, -1, True, np.complex64
_check_branch_cut(np.log, -0.5, 1j, 1, -1, True, np.complex64)
_check_branch_cut(np.log2, -0.5, 1j, 1, -1, True, np.complex64)
_check_branch_cut(np.log10, -0.5, 1j, 1, -1, True, np.complex64)
_check_branch_cut(np.log1p, -1.5, 1j, 1, -1, True, np.complex64)
_check_branch_cut(np.sqrt, -0.5, 1j, 1, -1, True, np.complex64)

yield _check_branch_cut, np.arcsin, [ -2, 2], [1j, 1j], 1, -1, True, np.complex64
yield _check_branch_cut, np.arccos, [ -2, 2], [1j, 1j], 1, -1, True, np.complex64
yield _check_branch_cut, np.arctan, [0-2j, 2j], [1, 1], -1, 1, True, np.complex64
_check_branch_cut(np.arcsin, [ -2, 2], [1j, 1j], 1, -1, True, np.complex64)
_check_branch_cut(np.arccos, [ -2, 2], [1j, 1j], 1, -1, True, np.complex64)
_check_branch_cut(np.arctan, [0-2j, 2j], [1, 1], -1, 1, True, np.complex64)

yield _check_branch_cut, np.arcsinh, [0-2j, 2j], [1, 1], -1, 1, True, np.complex64
yield _check_branch_cut, np.arccosh, [ -1, 0.5], [1j, 1j], 1, -1, True, np.complex64
yield _check_branch_cut, np.arctanh, [ -2, 2], [1j, 1j], 1, -1, True, np.complex64
_check_branch_cut(np.arcsinh, [0-2j, 2j], [1, 1], -1, 1, True, np.complex64)
_check_branch_cut(np.arccosh, [ -1, 0.5], [1j, 1j], 1, -1, True, np.complex64)
_check_branch_cut(np.arctanh, [ -2, 2], [1j, 1j], 1, -1, True, np.complex64)

# check against bogus branch cuts: assert continuity between quadrants
yield _check_branch_cut, np.arcsin, [0-2j, 2j], [ 1, 1], 1, 1, False, np.complex64
yield _check_branch_cut, np.arccos, [0-2j, 2j], [ 1, 1], 1, 1, False, np.complex64
yield _check_branch_cut, np.arctan, [ -2, 2], [1j, 1j], 1, 1, False, np.complex64
_check_branch_cut(np.arcsin, [0-2j, 2j], [ 1, 1], 1, 1, False, np.complex64)
_check_branch_cut(np.arccos, [0-2j, 2j], [ 1, 1], 1, 1, False, np.complex64)
_check_branch_cut(np.arctan, [ -2, 2], [1j, 1j], 1, 1, False, np.complex64)

yield _check_branch_cut, np.arcsinh, [ -2, 2, 0], [1j, 1j, 1], 1, 1, False, np.complex64
yield _check_branch_cut, np.arccosh, [0-2j, 2j, 2], [1, 1, 1j], 1, 1, False, np.complex64
yield _check_branch_cut, np.arctanh, [0-2j, 2j, 0], [1, 1, 1j], 1, 1, False, np.complex64
_check_branch_cut(np.arcsinh, [ -2, 2, 0], [1j, 1j, 1], 1, 1, False, np.complex64)
_check_branch_cut(np.arccosh, [0-2j, 2j, 2], [1, 1, 1j], 1, 1, False, np.complex64)
_check_branch_cut(np.arctanh, [0-2j, 2j, 0], [1, 1, 1j], 1, 1, False, np.complex64)

def test_against_cmath(self):
import cmath
Expand Down Expand Up F438 @@ -2489,7 +2489,7 @@ def check(func, z0, d=1):

def test_loss_of_precision(self):
for dtype in [np.complex64, np.complex_]:
yield self.check_loss_of_precision, dtype
self.check_loss_of_precision(dtype)

@dec.knownfailureif(is_longdouble_finfo_bogus(), "Bogus long double finfo")
def test_loss_of_precision_longcomplex(self):
Expand Down
12 changes: 6 additions & 6 deletions numpy/lib/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,20 +454,20 @@ def assert_equal_(o1, o2):
def test_roundtrip():
for arr in basic_arrays + record_arrays:
arr2 = roundtrip(arr)
yield assert_array_equal, arr, arr2
assert_array_equal(arr, arr2)


def test_roundtrip_randsize():
for arr in basic_arrays + record_arrays:
if arr.dtype != object:
arr2 = roundtrip_randsize(arr)
yield assert_array_equal, arr, arr2
assert_array_equal(arr, arr2)


def test_roundtrip_truncated():
for arr in basic_arrays:
if arr.dtype != object:
yield assert_raises, ValueError, roundtrip_truncated, arr
assert_raises(ValueError, roundtrip_truncated, arr)


def test_long_str():
Expand Down Expand Up @@ -508,7 +508,7 @@ def test_memmap_roundtrip():
fp = open(mfn, 'rb')
memmap_bytes = fp.read()
fp.close()
yield assert_equal_, normal_bytes, memmap_bytes
assert_equal_(normal_bytes, memmap_bytes)

# Check that reading the file using memmap works.
ma = format.open_memmap(nfn, mode='r')
Expand Down Expand Up @@ -728,13 +728,13 @@ def test_read_magic():
def test_read_magic_bad_magic():
for magic in malformed_magic:
f = BytesIO(magic)
yield raises(ValueError)(format.read_magic), f
assert_raises(ValueError, format.read_array, f)


def test_read_version_1_0_bad_magic():
for magic in bad_version_magic + malformed_magic:
f = BytesIO(magic)
yield raises(ValueError)(format.read_array), f
assert_raises(ValueError, format.read_array, f)


def test_bad_magic_args():
Expand Down
A7B7
11 changes: 2 additions & 9 deletions numpy/testing/pytest_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,16 +1219,9 @@ def assert_raises(exception_class, fn=None, *args, **kwargs):
if fn is not None:
pytest.raises(exception_class, fn, *args,**kwargs)
else:
@contextlib.contextmanager
def assert_raises_context():
try:
yield
except BaseException as raised_exception:
assert isinstance(raised_exception, exception_class)
else:
raise ValueError('Function did not raise an exception')
assert not kwargs

return assert_raises_context()
return pytest.raises(exception_class)


def assert_raises_regex(exception_class, expected_regexp, *args, **kwargs):
Expand Down
0