Skip to content
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

Avoid pytest.warns(None) #8718

Merged
merged 5 commits into from Mar 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 25 additions & 32 deletions dask/array/tests/test_array_core.py
Expand Up @@ -862,21 +862,18 @@ def test_block_tuple():


def test_broadcast_shapes():
with warnings.catch_warnings(record=True) as record:
assert () == broadcast_shapes()
assert (2, 5) == broadcast_shapes((2, 5))
assert (0, 5) == broadcast_shapes((0, 1), (1, 5))
assert np.allclose(
(2, np.nan), broadcast_shapes((1, np.nan), (2, 1)), equal_nan=True
)
assert np.allclose(
(2, np.nan), broadcast_shapes((2, 1), (1, np.nan)), equal_nan=True
)
assert (3, 4, 5) == broadcast_shapes((3, 4, 5), (4, 1), ())
assert (3, 4) == broadcast_shapes((3, 1), (1, 4), (4,))
assert (5, 6, 7, 3, 4) == broadcast_shapes((3, 1), (), (5, 6, 7, 1, 4))

assert not record
assert () == broadcast_shapes()
assert (2, 5) == broadcast_shapes((2, 5))
assert (0, 5) == broadcast_shapes((0, 1), (1, 5))
assert np.allclose(
(2, np.nan), broadcast_shapes((1, np.nan), (2, 1)), equal_nan=True
)
assert np.allclose(
(2, np.nan), broadcast_shapes((2, 1), (1, np.nan)), equal_nan=True
)
assert (3, 4, 5) == broadcast_shapes((3, 4, 5), (4, 1), ())
assert (3, 4) == broadcast_shapes((3, 1), (1, 4), (4,))
assert (5, 6, 7, 3, 4) == broadcast_shapes((3, 1), (), (5, 6, 7, 1, 4))

pytest.raises(ValueError, lambda: broadcast_shapes((3,), (3, 4)))
pytest.raises(ValueError, lambda: broadcast_shapes((2, 3), (2, 3, 1)))
Expand Down Expand Up @@ -958,11 +955,11 @@ def test_operators():
assert_eq(c, x + x.reshape((10, 1)))

expr = (3 / a * b) ** 2 > 5
with pytest.warns(None): # ZeroDivisionWarning
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning) # divide by zero
assert_eq(expr, (3 / x * y) ** 2 > 5)

with pytest.warns(None): # OverflowWarning
c = da.exp(a)
c = da.exp(a)
assert_eq(c, np.exp(x))

assert_eq(abs(-a), a)
Expand Down Expand Up @@ -2354,13 +2351,11 @@ def test_arithmetic():

assert_eq(da.logaddexp(a, b), np.logaddexp(x, y))
assert_eq(da.logaddexp2(a, b), np.logaddexp2(x, y))
with pytest.warns(None): # Overflow warning
assert_eq(da.exp(b), np.exp(y))
assert_eq(da.exp(b), np.exp(y))
assert_eq(da.log(a), np.log(x))
assert_eq(da.log10(a), np.log10(x))
assert_eq(da.log1p(a), np.log1p(x))
with pytest.warns(None): # Overflow warning
assert_eq(da.expm1(b), np.expm1(y))
assert_eq(da.expm1(b), np.expm1(y))
assert_eq(da.sqrt(a), np.sqrt(x))
assert_eq(da.square(a), np.square(x))

Expand All @@ -2373,8 +2368,7 @@ def test_arithmetic():
assert_eq(da.arctan2(b * 10, a), np.arctan2(y * 10, x))
assert_eq(da.hypot(b, a), np.hypot(y, x))
assert_eq(da.sinh(a), np.sinh(x))
with pytest.warns(None): # Overflow warning
assert_eq(da.cosh(b), np.cosh(y))
assert_eq(da.cosh(b), np.cosh(y))
assert_eq(da.tanh(a), np.tanh(x))
assert_eq(da.arcsinh(b * 10), np.arcsinh(y * 10))
assert_eq(da.arccosh(b * 10), np.arccosh(y * 10))
Expand All @@ -2399,8 +2393,7 @@ def test_arithmetic():
assert_eq(da.signbit(a - 3), np.signbit(x - 3))
assert_eq(da.copysign(a - 3, b), np.copysign(x - 3, y))
assert_eq(da.nextafter(a - 3, b), np.nextafter(x - 3, y))
with pytest.warns(None): # overflow warning
assert_eq(da.ldexp(c, c), np.ldexp(z, z))
assert_eq(da.ldexp(c, c), np.ldexp(z, z))
assert_eq(da.fmod(a * 12, b), np.fmod(x * 12, y))
assert_eq(da.floor(a * 0.5), np.floor(x * 0.5))
assert_eq(da.ceil(a), np.ceil(x))
Expand Down Expand Up @@ -3761,7 +3754,8 @@ def test_no_chunks_2d():
x = da.from_array(X, chunks=(2, 2))
x._chunks = ((np.nan, np.nan), (np.nan, np.nan, np.nan))

with pytest.warns(None): # zero division warning
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning) # divide by zero
assert_eq(da.log(x), np.log(X))
assert_eq(x.T, X.T)
assert_eq(x.sum(axis=0, keepdims=True), X.sum(axis=0, keepdims=True))
Expand Down Expand Up @@ -4887,10 +4881,9 @@ def test_scipy_sparse_concatenate(axis):


def test_3851():
with warnings.catch_warnings() as record:
with warnings.catch_warnings(record=True) as record:
Y = da.random.random((10, 10), chunks="auto")
da.argmax(Y, axis=0).compute()

assert not record


Expand Down Expand Up @@ -4987,17 +4980,17 @@ def test_auto_chunks_h5py():


def test_no_warnings_from_blockwise():
with pytest.warns(None) as record:
with warnings.catch_warnings(record=True) as record:
x = da.ones((3, 10, 10), chunks=(3, 2, 2))
da.map_blocks(lambda y: np.mean(y, axis=0), x, dtype=x.dtype, drop_axis=0)
assert not record

with pytest.warns(None) as record:
with warnings.catch_warnings(record=True) as record:
x = da.ones((15, 15), chunks=(5, 5))
(x.dot(x.T + 1) - x.mean(axis=0)).std()
assert not record

with pytest.warns(None) as record:
with warnings.catch_warnings(record=True) as record:
x = da.ones((1,), chunks=(1,))
1 / x[0]
assert not record
Expand Down
4 changes: 3 additions & 1 deletion dask/array/tests/test_atop.py
Expand Up @@ -728,7 +728,9 @@ def test_dont_merge_before_reductions():

def test_atop_legacy():
x = da.ones(10, chunks=(5,))
with pytest.warns(None):
with pytest.warns(
UserWarning, match="The da.atop function has moved to da.blockwise"
):
y = da.atop(inc, "i", x, "i", dtype=x.dtype)
z = da.blockwise(inc, "i", x, "i", dtype=x.dtype)
assert_eq(y, z)
Expand Down
18 changes: 10 additions & 8 deletions dask/array/tests/test_cupy_reductions.py
@@ -1,3 +1,5 @@
import warnings

import numpy as np
import pytest

Expand Down Expand Up @@ -55,16 +57,16 @@ def test_nanarg_reductions(dfunc, func):
a = da.from_array(x, chunks=(3, 4, 5))
assert_eq(dfunc(a), func(x))
assert_eq(dfunc(a, 0), func(x, 0))
with pytest.raises(ValueError):
with pytest.warns(None): # All NaN axis

with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning) # All-NaN slice encountered
with pytest.raises(ValueError):
dfunc(a, 1).compute()

with pytest.raises(ValueError):
with pytest.warns(None): # All NaN axis
with pytest.raises(ValueError):
dfunc(a, 2).compute()

x[:] = cupy.nan
a = da.from_array(x, chunks=(3, 4, 5))
with pytest.raises(ValueError):
with pytest.warns(None): # All NaN axis
x[:] = cupy.nan
a = da.from_array(x, chunks=(3, 4, 5))
with pytest.raises(ValueError):
dfunc(a).compute()
140 changes: 60 additions & 80 deletions dask/array/tests/test_reductions.py
Expand Up @@ -130,56 +130,54 @@ def test_reductions_1D(dtype):


def reduction_2d_test(da_func, darr, np_func, narr, use_dtype=True, split_every=True):
with warnings.catch_warnings():
warnings.simplefilter("ignore") # overflow
assert_eq(da_func(darr), np_func(narr))
assert_eq(da_func(darr, keepdims=True), np_func(narr, keepdims=True))
assert_eq(da_func(darr, axis=()), np_func(narr, axis=()))
assert_eq(da_func(darr, axis=0), np_func(narr, axis=0))
assert_eq(da_func(darr, axis=1), np_func(narr, axis=1))
assert_eq(da_func(darr, axis=-1), np_func(narr, axis=-1))
assert_eq(da_func(darr, axis=-2), np_func(narr, axis=-2))
assert_eq(da_func(darr), np_func(narr))
assert_eq(da_func(darr, keepdims=True), np_func(narr, keepdims=True))
assert_eq(da_func(darr, axis=()), np_func(narr, axis=()))
assert_eq(da_func(darr, axis=0), np_func(narr, axis=0))
assert_eq(da_func(darr, axis=1), np_func(narr, axis=1))
assert_eq(da_func(darr, axis=-1), np_func(narr, axis=-1))
assert_eq(da_func(darr, axis=-2), np_func(narr, axis=-2))
assert_eq(
da_func(darr, axis=1, keepdims=True), np_func(narr, axis=1, keepdims=True)
)
assert_eq(
da_func(darr, axis=(), keepdims=True), np_func(narr, axis=(), keepdims=True)
)
assert_eq(da_func(darr, axis=(1, 0)), np_func(narr, axis=(1, 0)))

assert same_keys(da_func(darr, axis=()), da_func(darr, axis=()))
assert same_keys(da_func(darr, axis=1), da_func(darr, axis=1))
assert same_keys(da_func(darr, axis=(1, 0)), da_func(darr, axis=(1, 0)))

if use_dtype:
assert_eq(da_func(darr, dtype="f8"), np_func(narr, dtype="f8"))
assert_eq(da_func(darr, dtype="i8"), np_func(narr, dtype="i8"))

if split_every:
a1 = da_func(darr, split_every=4)
a2 = da_func(darr, split_every={0: 2, 1: 2})
assert same_keys(a1, a2)
assert_eq(a1, np_func(narr))
assert_eq(a2, np_func(narr))
assert_eq(
da_func(darr, axis=1, keepdims=True), np_func(narr, axis=1, keepdims=True)
da_func(darr, keepdims=True, split_every=4),
np_func(narr, keepdims=True),
)
assert_eq(da_func(darr, axis=(), split_every=2), np_func(narr, axis=()))
assert_eq(da_func(darr, axis=0, split_every=2), np_func(narr, axis=0))
assert_eq(
da_func(darr, axis=(), keepdims=True), np_func(narr, axis=(), keepdims=True)
da_func(darr, axis=(), keepdims=True, split_every=2),
np_func(narr, axis=(), keepdims=True),
)
assert_eq(
da_func(darr, axis=0, keepdims=True, split_every=2),
np_func(narr, axis=0, keepdims=True),
)
assert_eq(da_func(darr, axis=1, split_every=2), np_func(narr, axis=1))
assert_eq(
da_func(darr, axis=1, keepdims=True, split_every=2),
np_func(narr, axis=1, keepdims=True),
)
assert_eq(da_func(darr, axis=(1, 0)), np_func(narr, axis=(1, 0)))

assert same_keys(da_func(darr, axis=()), da_func(darr, axis=()))
assert same_keys(da_func(darr, axis=1), da_func(darr, axis=1))
assert same_keys(da_func(darr, axis=(1, 0)), da_func(darr, axis=(1, 0)))

if use_dtype:
assert_eq(da_func(darr, dtype="f8"), np_func(narr, dtype="f8"))
assert_eq(da_func(darr, dtype="i8"), np_func(narr, dtype="i8"))

if split_every:
a1 = da_func(darr, split_every=4)
a2 = da_func(darr, split_every={0: 2, 1: 2})
assert same_keys(a1, a2)
assert_eq(a1, np_func(narr))
assert_eq(a2, np_func(narr))
assert_eq(
da_func(darr, keepdims=True, split_every=4),
np_func(narr, keepdims=True),
)
assert_eq(da_func(darr, axis=(), split_every=2), np_func(narr, axis=()))
assert_eq(da_func(darr, axis=0, split_every=2), np_func(narr, axis=0))
assert_eq(
da_func(darr, axis=(), keepdims=True, split_every=2),
np_func(narr, axis=(), keepdims=True),
)
assert_eq(
da_func(darr, axis=0, keepdims=True, split_every=2),
np_func(narr, axis=0, keepdims=True),
)
assert_eq(da_func(darr, axis=1, split_every=2), np_func(narr, axis=1))
assert_eq(
da_func(darr, axis=1, keepdims=True, split_every=2),
np_func(narr, axis=1, keepdims=True),
)


def test_reduction_errors():
Expand All @@ -191,6 +189,7 @@ def test_reduction_errors():


@pytest.mark.slow
@pytest.mark.filterwarnings("ignore:overflow encountered in reduce:RuntimeWarning")
@pytest.mark.parametrize("dtype", ["f4", "i4"])
def test_reductions_2D(dtype):
x = np.arange(1, 122).reshape((11, 11)).astype(dtype)
Expand Down Expand Up @@ -261,18 +260,17 @@ def test_nanarg_reductions(dfunc, func):
a = da.from_array(x, chunks=(3, 4, 5))
assert_eq(dfunc(a), func(x))
assert_eq(dfunc(a, 0), func(x, 0))
with pytest.raises(ValueError):
with pytest.warns(None): # All NaN axis
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning) # All-NaN slice encountered
with pytest.raises(ValueError):
dfunc(a, 1).compute()

with pytest.raises(ValueError):
with pytest.warns(None): # All NaN axis
with pytest.raises(ValueError):
dfunc(a, 2).compute()

x[:] = np.nan
a = da.from_array(x, chunks=(3, 4, 5))
with pytest.raises(ValueError):
with pytest.warns(None): # All NaN axis
x[:] = np.nan
a = da.from_array(x, chunks=(3, 4, 5))
with pytest.raises(ValueError):
dfunc(a).compute()


Expand Down Expand Up @@ -327,47 +325,28 @@ def test_reductions_2D_nans():

reduction_2d_test(da.nansum, a, np.nansum, x, False, False)
reduction_2d_test(da.nanprod, a, np.nanprod, x, False, False)
reduction_2d_test(da.nanmean, a, np.nanmean, x, False, False)
with pytest.warns(None): # division by 0 warning

with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning)
reduction_2d_test(da.nanmean, a, np.nanmean, x, False, False)
reduction_2d_test(da.nanvar, a, np.nanvar, x, False, False)
with pytest.warns(None): # division by 0 warning
reduction_2d_test(da.nanstd, a, np.nanstd, x, False, False)
with pytest.warns(None): # all NaN axis warning
reduction_2d_test(da.nanmin, a, np.nanmin, x, False, False)
with pytest.warns(None): # all NaN axis warning
reduction_2d_test(da.nanmax, a, np.nanmax, x, False, False)

with warnings.catch_warnings():
# RuntimeWarning: invalid value encountered in reduce
warnings.simplefilter("ignore", RuntimeWarning)
assert_eq(da.argmax(a), np.argmax(x))
assert_eq(da.argmin(a), np.argmin(x))

with pytest.warns(None): # all NaN axis warning
assert_eq(da.nanargmax(a), np.nanargmax(x))
with pytest.warns(None): # all NaN axis warning
assert_eq(da.nanargmin(a), np.nanargmin(x))

with warnings.catch_warnings():
# RuntimeWarning: invalid value encountered in reduce
warnings.simplefilter("ignore", RuntimeWarning)
assert_eq(da.argmax(a, axis=0), np.argmax(x, axis=0))
assert_eq(da.argmin(a, axis=0), np.argmin(x, axis=0))

with pytest.warns(None): # all NaN axis warning
assert_eq(da.nanargmax(a, axis=0), np.nanargmax(x, axis=0))
with pytest.warns(None): # all NaN axis warning
assert_eq(da.nanargmin(a, axis=0), np.nanargmin(x, axis=0))

with warnings.catch_warnings():
# RuntimeWarning: invalid value encountered in reduce
warnings.simplefilter("ignore", RuntimeWarning)
assert_eq(da.argmax(a, axis=1), np.argmax(x, axis=1))
assert_eq(da.argmin(a, axis=1), np.argmin(x, axis=1))

with pytest.warns(None): # all NaN axis warning
assert_eq(da.nanargmax(a, axis=1), np.nanargmax(x, axis=1))
with pytest.warns(None): # all NaN axis warning
assert_eq(da.nanargmin(a, axis=1), np.nanargmin(x, axis=1))


Expand Down Expand Up @@ -477,7 +456,8 @@ def test_reductions_with_empty_array():
x2 = dx2.compute()

for dx, x in [(dx1, x1), (dx2, x2)]:
with pytest.warns(None): # empty slice warning
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning) # Mean of empty slice
assert_eq(dx.mean(), x.mean())
assert_eq(dx.mean(axis=()), x.mean(axis=()))
assert_eq(dx.mean(axis=0), x.mean(axis=0))
Expand Down Expand Up @@ -794,7 +774,7 @@ def test_mean_func_does_not_warn():
xr = pytest.importorskip("xarray")
a = xr.DataArray(da.from_array(np.full((10, 10), np.nan)))

with pytest.warns(None) as rec:
with warnings.catch_warnings(record=True) as rec:
a.mean().compute()
assert not rec # did not warn

Expand All @@ -806,7 +786,7 @@ def test_nan_func_does_not_warn(func):
x[0] = 1
x[1] = 2
d = da.from_array(x, chunks=2)
with pytest.warns(None) as rec:
with warnings.catch_warnings(record=True) as rec:
getattr(da, func)(d).compute()
assert not rec # did not warn

Expand Down