Skip to content

Commit 6a737c4

Browse files
Handling warnings in pytest (#1845)
* Enable loading of warning plugin in pytest * Fix DeprecationWarning in test_histogram.py * Ignore DeprecationWarning for pkg_resources * Fix SyntaxWarning in test_ndarray_math.py * Deprecate numpy_cupy_array_list_equal * Fix DeprecationWarning in test_mathematical.py * Avoid FutureWarning for rcond parameter of numpy.linalg.lstsq * Fix DeprecationWarning in cupy test_elementwise.py * Skip test_msort_zero_dim - not implemented * Ignore RuntimeWarning for numpy.arccosh * Fix DeprecationWarning for numpy.fromstring * Add test_digitize_inf to TestDigitize * Fix DeprecationWarning: Converting np.integer to a dtype is deprecated * Fix ComplexWarning in 2 ways * Fix RuntimeWarning by reducing shape in TestNansumNanprodLong * Handle DeprecationWarning in test_dparray.py * Skip test_lexsort_one_dim/_two_dim - not implemented * Handle RuntimeWarning in test_linspace_float_underflow * Fix DeprecationWarning in test_round_halfway_uint * Update test_linspace to avoid DeprecationWarning * Use pytest.mark.usefixtures('suppress_complex_warning') * Handle RuntimeWarning: divide by zero in test_reciprocal * Fix DeprecationWarning in test_mathematical.py * Handle RuntimeWarning in test_from_dlpack * Add the fixture to test_from_dlpack_with_dpt insted of test_from_dlpack
1 parent d553611 commit 6a737c4

16 files changed

+83
-45
lines changed

setup.cfg

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ ignore = E201
66
# By default, tests marked as slow will be deselected.
77
# To run all tests, use -m "slow and not slow".
88
# To run only slow tests, use -m "slow".
9-
addopts = -m "not slow" -p no:warnings --tb=short --strict-markers
9+
addopts = -m "not slow" --tb=short --strict-markers
1010
norecursedirs = tests_perf
1111
testpaths = tests
1212
markers =
1313
slow: marks tests as slow (deselect with '-m "not slow"')
1414
multi_gpu: marks tests that require a specified number of GPUs
15-
# Added due to -p no:warnings to avoid errors with --strict-markers
16-
filterwarnings: mark to filter warnings during tests
15+
filterwarnings =
16+
# pkg_resources
17+
ignore:pkg_resources is deprecated as an API:DeprecationWarning
18+
# NumPy arccosh
19+
# Undefined behavior depends on the backend:
20+
# NumPy with OpenBLAS for np.array[1.0] does not raise a warning
21+
# while numpy with OneMKL raises RuntimeWarning
22+
ignore:invalid value encountered in arccosh:RuntimeWarning
1723

1824
[versioneer]
1925
VCS = git

tests/test_arraycreation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ def test_vander_seq(sequence):
516516
assert_allclose(vander_func(numpy, sequence), vander_func(dpnp, sequence))
517517

518518

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

533534

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

716716

717717
@pytest.mark.parametrize(

tests/test_arraymanipulation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ def test_asfarray(dtype, data):
846846
assert_array_equal(result, expected)
847847

848848

849+
@pytest.mark.usefixtures("suppress_complex_warning")
849850
@pytest.mark.parametrize("dtype", get_all_dtypes())
850851
@pytest.mark.parametrize("data", [[1.0, 2.0, 3.0]], ids=["[1., 2., 3.]"])
851852
@pytest.mark.parametrize("data_dtype", get_all_dtypes(no_none=True))

tests/test_dparray.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ def test_print_dpnp_zero_shape():
220220
assert result == expected
221221

222222

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

233236

237+
# Numpy will raise an error when converting a.ndim > 0 to a scalar
238+
# TODO: Discuss dpnp behavior according to these future changes
239+
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
234240
@pytest.mark.parametrize(
235241
"method", ["__bool__", "__float__", "__int__", "__complex__"]
236242
)

tests/test_histogram.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ class TestDigitize:
3838
numpy.array([1, 2, 3, 4, 5, 6, 7, 8, 9]),
3939
numpy.array([1, 4, 6, 7]),
4040
),
41-
# Infinity values
42-
(
43-
numpy.array([-numpy.inf, -1, 0, 1, numpy.inf]),
44-
numpy.array([-2, -1, 0, 1, 2]),
45-
),
4641
# Repeated elements
4742
(numpy.array([1, 2, 2, 3, 3, 3, 4, 5]), numpy.array([1, 2, 3, 4])),
4843
],
@@ -57,6 +52,18 @@ def test_digitize(self, x, bins, dtype, right):
5752
expected = numpy.digitize(x, bins, right=right)
5853
assert_dtype_allclose(result, expected)
5954

55+
@pytest.mark.parametrize("dtype", get_float_dtypes())
56+
@pytest.mark.parametrize("right", [True, False])
57+
def test_digitize_inf(self, dtype, right):
58+
x = numpy.array([-numpy.inf, -1, 0, 1, numpy.inf], dtype=dtype)
59+
bins = numpy.array([-2, -1, 0, 1, 2], dtype=dtype)
60+
x_dp = dpnp.array(x)
61+
bins_dp = dpnp.array(bins)
62+
63+
result = dpnp.digitize(x_dp, bins_dp, right=right)
64+
expected = numpy.digitize(x, bins, right=right)
65+
assert_dtype_allclose(result, expected)
66+
6067
@pytest.mark.parametrize(
6168
"dtype_x", get_all_dtypes(no_bool=True, no_complex=True)
6269
)
@@ -386,7 +393,8 @@ def test_infinite_edge(self, xp, inf_val):
386393

387394
# both first and last ranges must be finite
388395
with assert_raises_regex(
389-
ValueError, f"autodetected range of \[{min}, {max}\] is not finite"
396+
ValueError,
397+
f"autodetected range of \\[{min}, {max}\\] is not finite",
390398
):
391399
xp.histogram(v)
392400

tests/test_linalg.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,9 @@ def test_lstsq(self, a_shape, b_shape, dtype):
780780
b_dp = inp.array(b_np)
781781

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

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

797-
expected = numpy.linalg.lstsq(a_np, b_np)
799+
# if rcond is not set, FutureWarning is given.
800+
# By default Numpy uses None for calculations
801+
expected = numpy.linalg.lstsq(a_np, b_np, rcond=None)
798802
result = inp.linalg.lstsq(a_dp, b_dp)
799803

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

815819
result = inp.linalg.lstsq(a_dp, b_dp)
816-
expected = numpy.linalg.lstsq(a_np, b_np)
820+
# if rcond is not set, FutureWarning is given.
821+
# By default Numpy uses None for calculations
822+
expected = numpy.linalg.lstsq(a_np, b_np, rcond=None)
817823

818824
for param_dp, param_np in zip(result, expected):
819825
assert_dtype_allclose(param_dp, param_np)

tests/test_mathematical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_mode(self):
9191
d = dpnp.ones(100)
9292
k = dpnp.ones(3)
9393
default_mode = dpnp.convolve(d, k, mode="full")
94-
full_mode = dpnp.convolve(d, k, mode="f")
94+
full_mode = dpnp.convolve(d, k, mode="full")
9595
assert_array_equal(full_mode, default_mode)
9696
# integer mode
9797
with assert_raises(ValueError):

tests/test_random_state.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ def test_fallback(self, loc, scale):
239239
[
240240
dpnp.float16,
241241
float,
242-
dpnp.integer,
243242
dpnp.int64,
244243
dpnp.int32,
245244
dpnp.int,
@@ -253,7 +252,6 @@ def test_fallback(self, loc, scale):
253252
ids=[
254253
"dpnp.float16",
255254
"float",
256-
"dpnp.integer",
257255
"dpnp.int64",
258256
"dpnp.int32",
259257
"dpnp.int",
@@ -366,8 +364,8 @@ def test_wrong_dims(self):
366364
class TestRandInt:
367365
@pytest.mark.parametrize(
368366
"dtype",
369-
[int, dpnp.int32, dpnp.int, dpnp.integer],
370-
ids=["int", "dpnp.int32", "dpnp.int", "dpnp.integer"],
367+
[int, dpnp.int32, dpnp.int],
368+
ids=["int", "dpnp.int32", "dpnp.int"],
371369
)
372370
@pytest.mark.parametrize(
373371
"usm_type",
@@ -379,7 +377,7 @@ def test_distr(self, dtype, usm_type):
379377
low = 1
380378
high = 10
381379

382-
if dtype in (dpnp.int, dpnp.integer) and dtype != dpnp.dtype("int32"):
380+
if dtype == dpnp.int and dtype != dpnp.dtype("int32"):
383381
pytest.skip(
384382
"dtype isn't alias on dpnp.int32 on the target OS, so there will be a fallback"
385383
)
@@ -566,11 +564,10 @@ def test_bounds_fallback(self, low, high):
566564
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
567565
@pytest.mark.parametrize(
568566
"dtype",
569-
[dpnp.int64, dpnp.int, dpnp.integer, dpnp.bool, dpnp.bool_, bool],
567+
[dpnp.int64, dpnp.int, dpnp.bool, dpnp.bool_, bool],
570568
ids=[
571569
"dpnp.int64",
572570
"dpnp.int",
573-
"dpnp.integer",
574571
"dpnp.bool",
575572
"dpnp.bool_",
576573
"bool",
@@ -582,7 +579,7 @@ def test_dtype_fallback(self, dtype):
582579
high = 37 if not dtype in {dpnp.bool_, bool} else 2
583580
size = (3, 2, 5)
584581

585-
if dtype in (dpnp.int, dpnp.integer) and dtype == dpnp.dtype("int32"):
582+
if dtype == dpnp.int and dtype == dpnp.dtype("int32"):
586583
pytest.skip(
587584
"dtype is alias on dpnp.int32 on the target OS, so no fallback here"
588585
)
@@ -1157,7 +1154,6 @@ def test_fallback(self, low, high):
11571154
[
11581155
dpnp.float16,
11591156
float,
1160-
dpnp.integer,
11611157
dpnp.int64,
11621158
dpnp.int,
11631159
int,
@@ -1170,7 +1166,6 @@ def test_fallback(self, low, high):
11701166
ids=[
11711167
"dpnp.float16",
11721168
"float",
1173-
"dpnp.integer",
11741169
"dpnp.int64",
11751170
"dpnp.int",
11761171
"int",
@@ -1182,7 +1177,7 @@ def test_fallback(self, low, high):
11821177
],
11831178
)
11841179
def test_invalid_dtype(self, dtype):
1185-
if dtype in (dpnp.int, dpnp.integer) and dtype == dpnp.dtype("int32"):
1180+
if dtype == dpnp.int and dtype == dpnp.dtype("int32"):
11861181
pytest.skip(
11871182
"dtype is alias on dpnp.int32 on the target OS, so no error here"
11881183
)

tests/test_sycl_queue.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def vvsort(val, vec, size, xp):
103103
{"dtype": dpnp.int32},
104104
),
105105
pytest.param("fromiter", [[1, 2, 3, 4]], {"dtype": dpnp.int64}),
106-
pytest.param("fromstring", ["1, 2"], {"dtype": int, "sep": " "}),
106+
pytest.param("fromstring", ["1 2"], {"dtype": int, "sep": " "}),
107107
pytest.param("full", [(2, 2)], {"fill_value": 5}),
108108
pytest.param("eye", [4, 2], {}),
109109
pytest.param("geomspace", [1, 4, 8], {}),
@@ -1686,6 +1686,7 @@ def test_from_dlpack(arr_dtype, shape, device):
16861686
assert V.strides == W.strides
16871687

16881688

1689+
@pytest.mark.usefixtures("suppress_invalid_numpy_warnings")
16891690
@pytest.mark.parametrize(
16901691
"device",
16911692
valid_devices,
@@ -2112,7 +2113,9 @@ def test_lstsq(m, n, nrhs, device):
21122113
b_dp = dpnp.array(b_np, device=device)
21132114

21142115
result_dp = dpnp.linalg.lstsq(a_dp, b_dp)
2115-
result = numpy.linalg.lstsq(a_np, b_np)
2116+
# if rcond is not set, FutureWarning is given.
2117+
# By default Numpy uses None for calculations
2118+
result = numpy.linalg.lstsq(a_np, b_np, rcond=None)
21162119

21172120
for param_dp, param_np in zip(result_dp, result):
21182121
assert_dtype_allclose(param_dp, param_np)

tests/test_umath.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ def test_invalid_out(self, out):
398398

399399

400400
class TestReciprocal:
401+
@pytest.mark.usefixtures("suppress_divide_numpy_warnings")
401402
@pytest.mark.parametrize("dtype", get_float_complex_dtypes())
402403
def test_reciprocal(self, dtype):
403404
np_array, expected = _get_numpy_arrays_1in_1out(

0 commit comments

Comments
 (0)