Skip to content

Handling warnings in pytest #1845

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 33 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4f72371
Enable loading of warning plugin in pytest
vlad-perevezentsev May 22, 2024
11a6554
Merge master into handle_pytest_warnings
vlad-perevezentsev May 24, 2024
005c366
Fix DeprecationWarning in test_histogram.py
vlad-perevezentsev May 24, 2024
ffbb9ce
Ignore DeprecationWarning for pkg_resources
vlad-perevezentsev May 24, 2024
d0733b5
Fix SyntaxWarning in test_ndarray_math.py
vlad-perevezentsev May 24, 2024
dada23b
Deprecate numpy_cupy_array_list_equal
vlad-perevezentsev May 24, 2024
1d460b2
Fix DeprecationWarning in test_mathematical.py
vlad-perevezentsev May 24, 2024
948f293
Avoid FutureWarning for rcond parameter of numpy.linalg.lstsq
vlad-perevezentsev May 24, 2024
42d8cfa
Merge master into handle_pytest_warnings
vlad-perevezentsev May 27, 2024
e194073
Fix DeprecationWarning in cupy test_elementwise.py
vlad-perevezentsev May 27, 2024
91f0319
Skip test_msort_zero_dim - not implemented
vlad-perevezentsev May 27, 2024
dd22f37
Ignore RuntimeWarning for numpy.arccosh
vlad-perevezentsev May 27, 2024
6f08579
Fix DeprecationWarning for numpy.fromstring
vlad-perevezentsev May 27, 2024
c37a014
Add test_digitize_inf to TestDigitize
vlad-perevezentsev May 27, 2024
064be72
Fix DeprecationWarning: Converting np.integer to a dtype is deprecated
vlad-perevezentsev May 27, 2024
98e8e83
Fix ComplexWarning in 2 ways
vlad-perevezentsev May 28, 2024
4f42ceb
Fix RuntimeWarning by reducing shape in TestNansumNanprodLong
vlad-perevezentsev May 28, 2024
fc31079
Merge master into handle_pytest_warnings
vlad-perevezentsev May 28, 2024
391ec3b
Handle DeprecationWarning in test_dparray.py
vlad-perevezentsev May 29, 2024
80234e6
Skip test_lexsort_one_dim/_two_dim - not implemented
vlad-perevezentsev May 29, 2024
668243a
Merge master into handle_pytest_warnings
vlad-perevezentsev May 29, 2024
527bd83
Handle RuntimeWarning in test_linspace_float_underflow
vlad-perevezentsev May 29, 2024
7f163cb
Fix DeprecationWarning in test_round_halfway_uint
vlad-perevezentsev May 29, 2024
e129cbb
Update test_linspace to avoid DeprecationWarning
vlad-perevezentsev May 29, 2024
548490f
Use pytest.mark.usefixtures('suppress_complex_warning')
vlad-perevezentsev May 29, 2024
16c8b43
Merge master into handle_pytest_warnings
vlad-perevezentsev May 29, 2024
28d92c6
Handle RuntimeWarning: divide by zero in test_reciprocal
vlad-perevezentsev May 31, 2024
91feb27
Merge master into handle_pytest_warnings
vlad-perevezentsev Jun 5, 2024
e605555
Fix DeprecationWarning in test_mathematical.py
vlad-perevezentsev Jun 5, 2024
36bb145
Handle RuntimeWarning in test_from_dlpack
vlad-perevezentsev Jun 5, 2024
2632dd4
Merge master into handle_pytest_warnings
vlad-perevezentsev Jun 12, 2024
d71ecf5
Add the fixture to test_from_dlpack_with_dpt insted of test_from_dlpack
vlad-perevezentsev Jun 12, 2024
dd09984
Merge master into handle_pytest_warnings
vlad-perevezentsev Jun 14, 2024
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
12 changes: 9 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ ignore = E201
# By default, tests marked as slow will be deselected.
# To run all tests, use -m "slow and not slow".
# To run only slow tests, use -m "slow".
addopts = -m "not slow" -p no:warnings --tb=short --strict-markers
addopts = -m "not slow" --tb=short --strict-markers
norecursedirs = tests_perf
testpaths = tests
markers =
slow: marks tests as slow (deselect with '-m "not slow"')
multi_gpu: marks tests that require a specified number of GPUs
# Added due to -p no:warnings to avoid errors with --strict-markers
filterwarnings: mark to filter warnings during tests
filterwarnings =
# pkg_resources
ignore:pkg_resources is deprecated as an API:DeprecationWarning
# NumPy arccosh
# Undefined behavior depends on the backend:
# NumPy with OpenBLAS for np.array[1.0] does not raise a warning
# while numpy with OneMKL raises RuntimeWarning
ignore:invalid value encountered in arccosh:RuntimeWarning

[versioneer]
VCS = git
Expand Down
6 changes: 3 additions & 3 deletions tests/test_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ def test_vander_seq(sequence):
assert_allclose(vander_func(numpy, sequence), vander_func(dpnp, sequence))


@pytest.mark.usefixtures("suppress_complex_warning")
@pytest.mark.parametrize(
"shape",
[(), 0, (0,), (2, 0, 3), (3, 2)],
Expand All @@ -531,6 +532,7 @@ def test_full(shape, fill_value, dtype, order):
assert_array_equal(func(numpy), func(dpnp))


@pytest.mark.usefixtures("suppress_complex_warning")
@pytest.mark.parametrize(
"array",
[[], 0, [1, 2, 3], [[1, 2], [3, 4]]],
Expand Down Expand Up @@ -709,9 +711,7 @@ def test_linspace(start, stop, num, dtype, retstep):
if numpy.issubdtype(dtype, dpnp.integer):
assert_allclose(res_np, res_dp, rtol=1)
else:
if dtype is None and not has_support_aspect64():
dtype = dpnp.float32
assert_allclose(res_np, res_dp, rtol=1e-06, atol=dpnp.finfo(dtype).eps)
assert_dtype_allclose(res_dp, res_np)


@pytest.mark.parametrize(
Expand Down
1 change: 1 addition & 0 deletions tests/test_arraymanipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ def test_asfarray(dtype, data):
assert_array_equal(result, expected)


@pytest.mark.usefixtures("suppress_complex_warning")
@pytest.mark.parametrize("dtype", get_all_dtypes())
@pytest.mark.parametrize("data", [[1.0, 2.0, 3.0]], ids=["[1., 2., 3.]"])
@pytest.mark.parametrize("data_dtype", get_all_dtypes(no_none=True))
Expand Down
6 changes: 6 additions & 0 deletions tests/test_dparray.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ def test_print_dpnp_zero_shape():
assert result == expected


# Numpy will raise an error when converting a.ndim > 0 to a scalar
# TODO: Discuss dpnp behavior according to these future changes
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
@pytest.mark.parametrize("func", [bool, float, int, complex])
@pytest.mark.parametrize("shape", [tuple(), (1,), (1, 1), (1, 1, 1)])
@pytest.mark.parametrize(
Expand All @@ -231,6 +234,9 @@ def test_scalar_type_casting(func, shape, dtype):
assert func(numpy_array) == func(dpnp_array)


# Numpy will raise an error when converting a.ndim > 0 to a scalar
# TODO: Discuss dpnp behavior according to these future changes
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
@pytest.mark.parametrize(
"method", ["__bool__", "__float__", "__int__", "__complex__"]
)
Expand Down
20 changes: 14 additions & 6 deletions tests/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ class TestDigitize:
numpy.array([1, 2, 3, 4, 5, 6, 7, 8, 9]),
numpy.array([1, 4, 6, 7]),
),
# Infinity values
(
numpy.array([-numpy.inf, -1, 0, 1, numpy.inf]),
numpy.array([-2, -1, 0, 1, 2]),
),
# Repeated elements
(numpy.array([1, 2, 2, 3, 3, 3, 4, 5]), numpy.array([1, 2, 3, 4])),
],
Expand All @@ -57,6 +52,18 @@ def test_digitize(self, x, bins, dtype, right):
expected = numpy.digitize(x, bins, right=right)
assert_dtype_allclose(result, expected)

@pytest.mark.parametrize("dtype", get_float_dtypes())
@pytest.mark.parametrize("right", [True, False])
def test_digitize_inf(self, dtype, right):
x = numpy.array([-numpy.inf, -1, 0, 1, numpy.inf], dtype=dtype)
bins = numpy.array([-2, -1, 0, 1, 2], dtype=dtype)
x_dp = dpnp.array(x)
bins_dp = dpnp.array(bins)

result = dpnp.digitize(x_dp, bins_dp, right=right)
expected = numpy.digitize(x, bins, right=right)
assert_dtype_allclose(result, expected)

@pytest.mark.parametrize(
"dtype_x", get_all_dtypes(no_bool=True, no_complex=True)
)
Expand Down Expand Up @@ -386,7 +393,8 @@ def test_infinite_edge(self, xp, inf_val):

# both first and last ranges must be finite
with assert_raises_regex(
ValueError, f"autodetected range of \[{min}, {max}\] is not finite"
ValueError,
f"autodetected range of \\[{min}, {max}\\] is not finite",
):
xp.histogram(v)

Expand Down
12 changes: 9 additions & 3 deletions tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,9 @@ def test_lstsq(self, a_shape, b_shape, dtype):
b_dp = inp.array(b_np)

result = inp.linalg.lstsq(a_dp, b_dp)
expected = numpy.linalg.lstsq(a_np, b_np)
# if rcond is not set, FutureWarning is given.
# By default Numpy uses None for calculations
expected = numpy.linalg.lstsq(a_np, b_np, rcond=None)

for param_dp, param_np in zip(result, expected):
assert_dtype_allclose(param_dp, param_np)
Expand All @@ -794,7 +796,9 @@ def test_lstsq_diff_type(self, a_dtype, b_dtype):
a_dp = inp.array(a_np)
b_dp = inp.array(b_np)

expected = numpy.linalg.lstsq(a_np, b_np)
# if rcond is not set, FutureWarning is given.
# By default Numpy uses None for calculations
expected = numpy.linalg.lstsq(a_np, b_np, rcond=None)
result = inp.linalg.lstsq(a_dp, b_dp)

for param_dp, param_np in zip(result, expected):
Expand All @@ -813,7 +817,9 @@ def test_lstsq_empty(self, m, n, nrhs, dtype):
b_dp = inp.array(b_np)

result = inp.linalg.lstsq(a_dp, b_dp)
expected = numpy.linalg.lstsq(a_np, b_np)
# if rcond is not set, FutureWarning is given.
# By default Numpy uses None for calculations
expected = numpy.linalg.lstsq(a_np, b_np, rcond=None)

for param_dp, param_np in zip(result, expected):
assert_dtype_allclose(param_dp, param_np)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_mode(self):
d = dpnp.ones(100)
k = dpnp.ones(3)
default_mode = dpnp.convolve(d, k, mode="full")
full_mode = dpnp.convolve(d, k, mode="f")
full_mode = dpnp.convolve(d, k, mode="full")
assert_array_equal(full_mode, default_mode)
# integer mode
with assert_raises(ValueError):
Expand Down
17 changes: 6 additions & 11 deletions tests/test_random_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ def test_fallback(self, loc, scale):
[
dpnp.float16,
float,
dpnp.integer,
dpnp.int64,
dpnp.int32,
dpnp.int,
Expand All @@ -253,7 +252,6 @@ def test_fallback(self, loc, scale):
ids=[
"dpnp.float16",
"float",
"dpnp.integer",
"dpnp.int64",
"dpnp.int32",
"dpnp.int",
Expand Down Expand Up @@ -366,8 +364,8 @@ def test_wrong_dims(self):
class TestRandInt:
@pytest.mark.parametrize(
"dtype",
[int, dpnp.int32, dpnp.int, dpnp.integer],
ids=["int", "dpnp.int32", "dpnp.int", "dpnp.integer"],
[int, dpnp.int32, dpnp.int],
ids=["int", "dpnp.int32", "dpnp.int"],
)
@pytest.mark.parametrize(
"usm_type",
Expand All @@ -379,7 +377,7 @@ def test_distr(self, dtype, usm_type):
low = 1
high = 10

if dtype in (dpnp.int, dpnp.integer) and dtype != dpnp.dtype("int32"):
if dtype == dpnp.int and dtype != dpnp.dtype("int32"):
pytest.skip(
"dtype isn't alias on dpnp.int32 on the target OS, so there will be a fallback"
)
Expand Down Expand Up @@ -566,11 +564,10 @@ def test_bounds_fallback(self, low, high):
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
@pytest.mark.parametrize(
"dtype",
[dpnp.int64, dpnp.int, dpnp.integer, dpnp.bool, dpnp.bool_, bool],
[dpnp.int64, dpnp.int, dpnp.bool, dpnp.bool_, bool],
ids=[
"dpnp.int64",
"dpnp.int",
"dpnp.integer",
"dpnp.bool",
"dpnp.bool_",
"bool",
Expand All @@ -582,7 +579,7 @@ def test_dtype_fallback(self, dtype):
high = 37 if not dtype in {dpnp.bool_, bool} else 2
size = (3, 2, 5)

if dtype in (dpnp.int, dpnp.integer) and dtype == dpnp.dtype("int32"):
if dtype == dpnp.int and dtype == dpnp.dtype("int32"):
pytest.skip(
"dtype is alias on dpnp.int32 on the target OS, so no fallback here"
)
Expand Down Expand Up @@ -1157,7 +1154,6 @@ def test_fallback(self, low, high):
[
dpnp.float16,
float,
dpnp.integer,
dpnp.int64,
dpnp.int,
int,
Expand All @@ -1170,7 +1166,6 @@ def test_fallback(self, low, high):
ids=[
"dpnp.float16",
"float",
"dpnp.integer",
"dpnp.int64",
"dpnp.int",
"int",
Expand All @@ -1182,7 +1177,7 @@ def test_fallback(self, low, high):
],
)
def test_invalid_dtype(self, dtype):
if dtype in (dpnp.int, dpnp.integer) and dtype == dpnp.dtype("int32"):
if dtype == dpnp.int and dtype == dpnp.dtype("int32"):
pytest.skip(
"dtype is alias on dpnp.int32 on the target OS, so no error here"
)
Expand Down
7 changes: 5 additions & 2 deletions tests/test_sycl_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def vvsort(val, vec, size, xp):
{"dtype": dpnp.int32},
),
pytest.param("fromiter", [[1, 2, 3, 4]], {"dtype": dpnp.int64}),
pytest.param("fromstring", ["1, 2"], {"dtype": int, "sep": " "}),
pytest.param("fromstring", ["1 2"], {"dtype": int, "sep": " "}),
pytest.param("full", [(2, 2)], {"fill_value": 5}),
pytest.param("eye", [4, 2], {}),
pytest.param("geomspace", [1, 4, 8], {}),
Expand Down Expand Up @@ -1686,6 +1686,7 @@ def test_from_dlpack(arr_dtype, shape, device):
assert V.strides == W.strides


@pytest.mark.usefixtures("suppress_invalid_numpy_warnings")
@pytest.mark.parametrize(
"device",
valid_devices,
Expand Down Expand Up @@ -2112,7 +2113,9 @@ def test_lstsq(m, n, nrhs, device):
b_dp = dpnp.array(b_np, device=device)

result_dp = dpnp.linalg.lstsq(a_dp, b_dp)
result = numpy.linalg.lstsq(a_np, b_np)
# if rcond is not set, FutureWarning is given.
# By default Numpy uses None for calculations
result = numpy.linalg.lstsq(a_np, b_np, rcond=None)

for param_dp, param_np in zip(result_dp, result):
assert_dtype_allclose(param_dp, param_np)
Expand Down
1 change: 1 addition & 0 deletions tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ def test_invalid_out(self, out):


class TestReciprocal:
@pytest.mark.usefixtures("suppress_divide_numpy_warnings")
@pytest.mark.parametrize("dtype", get_float_complex_dtypes())
def test_reciprocal(self, dtype):
np_array, expected = _get_numpy_arrays_1in_1out(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_usm_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def test_array_creation_from_2d_array(func, args, usm_type_x, usm_type_y):
"fromfunction", [(lambda i, j: i + j), (3, 3)], {"dtype": dp.int32}
),
pytest.param("fromiter", [[1, 2, 3, 4]], {"dtype": dp.int64}),
pytest.param("fromstring", ["1, 2"], {"dtype": int, "sep": " "}),
pytest.param("fromstring", ["1 2"], {"dtype": int, "sep": " "}),
pytest.param("full", [(2, 2)], {"fill_value": 5}),
pytest.param("eye", [4, 2], {}),
pytest.param("geomspace", [1, 4, 8], {}),
Expand Down
6 changes: 3 additions & 3 deletions tests/third_party/cupy/binary_tests/test_elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ class TestElementwise(unittest.TestCase):
@testing.for_int_dtypes()
@testing.numpy_cupy_array_equal()
def check_unary_int(self, name, xp, dtype):
a = xp.array([-3, -2, -1, 0, 1, 2, 3], dtype=dtype)
a = xp.array([-3, -2, -1, 0, 1, 2, 3]).astype(dtype)
return getattr(xp, name)(a)

@testing.for_int_dtypes()
@testing.numpy_cupy_array_equal()
def check_binary_int(self, name, xp, dtype):
a = xp.array([-3, -2, -1, 0, 1, 2, 3], dtype=dtype)
b = xp.array([0, 1, 2, 3, 4, 5, 6], dtype=dtype)
a = xp.array([-3, -2, -1, 0, 1, 2, 3]).astype(dtype)
b = xp.array([0, 1, 2, 3, 4, 5, 6]).astype(dtype)
return getattr(xp, name)(a, b)

def test_bitwise_and(self):
Expand Down
8 changes: 4 additions & 4 deletions tests/third_party/cupy/core_tests/test_ndarray_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class TestRoundHalfway(unittest.TestCase):
@testing.for_float_dtypes()
@testing.numpy_cupy_allclose(atol=1e-5)
def test_round_halfway_float(self, xp, dtype):
if self.decimals is -3 and dtype == numpy.float32:
if self.decimals == -3 and dtype == numpy.float32:
pytest.skip(
"Case with decimals=-3 and dtype float32 has divide error less than 1e-5"
)
Expand All @@ -78,7 +78,7 @@ def test_round_halfway_float(self, xp, dtype):
@testing.numpy_cupy_array_equal()
def test_round_halfway_int(self, xp, dtype):
# generate [..., -1.5, -0.5, 0.5, 1.5, ...] * 10^{-decimals}
if self.decimals is -3 and not has_support_aspect64():
if self.decimals == -3 and not has_support_aspect64():
pytest.skip(
"Case with decimals=-3 and dtype float32 has divide error less than 1e-5"
)
Expand All @@ -96,7 +96,7 @@ def test_round_halfway_int(self, xp, dtype):
@testing.numpy_cupy_array_equal()
def test_round_halfway_uint(self, xp, dtype):
# generate [0.5, 1.5, ...] * 10^{-decimals}
if self.decimals is -3 and not has_support_aspect64():
if self.decimals == -3 and not has_support_aspect64():
pytest.skip(
"Case with decimals=-3 and dtype float32 has divide error less than 1e-5"
)
Expand All @@ -105,7 +105,7 @@ def test_round_halfway_uint(self, xp, dtype):
a -= 1
scale = 10 ** abs(self.decimals)
if self.decimals < 0:
a *= xp.array(scale, dtype=dtype)
a *= xp.array(scale).astype(dtype)
a >>= 1

return a.round(self.decimals)
Expand Down
6 changes: 3 additions & 3 deletions tests/third_party/cupy/creation_tests/test_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ def test_linspace_float_overflow(self, xp):
def test_linspace_float_underflow(self, xp):
# find minimum subnormal number
dtype = cupy.default_float_type()
x = xp.finfo(dtype).min
while x / 2 > 0:
x /= 2
# use .tiny instead of .min and while to get
# minimum subnormal number directly and avoid RuntimeWarning
x = xp.finfo(dtype).tiny
return xp.linspace(0.0, x, 10, dtype=dtype)

@testing.with_requires("numpy>=1.16")
Expand Down
8 changes: 4 additions & 4 deletions tests/third_party/cupy/logic_tests/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,28 @@ class TestComparisonOperator(unittest.TestCase):
]

@testing.for_all_dtypes(no_complex=True)
@testing.numpy_cupy_array_list_equal()
@testing.numpy_cupy_array_equal()
def test_binary_npscalar_array(self, xp, dtype):
a = numpy.int16(3)
b = testing.shaped_arange((2, 3), xp, dtype)
return [op(a, b) for op in self.operators]

@testing.for_all_dtypes(no_complex=True)
@testing.numpy_cupy_array_list_equal()
@testing.numpy_cupy_array_equal()
def test_binary_pyscalar_array(self, xp, dtype):
a = 3.0
b = testing.shaped_arange((2, 3), xp, dtype)
return [op(a, b) for op in self.operators]

@testing.for_all_dtypes(no_complex=True)
@testing.numpy_cupy_array_list_equal()
@testing.numpy_cupy_array_equal()
def test_binary_array_npscalar(self, xp, dtype):
a = testing.shaped_arange((2, 3), xp, dtype)
b = numpy.float32(3.0)
return [op(a, b) for op in self.operators]

@testing.for_all_dtypes(no_complex=True)
@testing.numpy_cupy_array_list_equal()
@testing.numpy_cupy_array_equal()
def test_binary_array_pyscalar(self, xp, dtype):
a = testing.shaped_arange((2, 3), xp, dtype)
b = 3
Expand Down
Loading
Loading