1+ from __future__ import annotations
2+
13import numpy
24import pytest
35
46import dpnp as cupy
5- from dpnp .tests .helper import has_support_aspect64
7+ from dpnp .tests .helper import has_support_aspect64 , numpy_version
68from dpnp .tests .third_party .cupy import testing
79
810
@@ -155,10 +157,7 @@ def test_external_clip4(self, dtype):
155157 # (min or max) as a keyword argument according to Python Array API.
156158 # In older versions of numpy, both arguments must be positional;
157159 # passing only one raises a TypeError.
158- if (
159- xp is numpy
160- and numpy .lib .NumpyVersion (numpy .__version__ ) < "2.1.0"
161- ):
160+ if xp is numpy and numpy_version () < "2.1.0" :
162161 with pytest .raises (TypeError ):
163162 xp .clip (a , 3 )
164163 else :
@@ -257,9 +256,10 @@ def test_nan_to_num_inf(self):
257256 def test_nan_to_num_nan (self ):
258257 self .check_unary_nan ("nan_to_num" )
259258
260- @testing .numpy_cupy_allclose (atol = 1e-5 , type_check = has_support_aspect64 ())
259+ @pytest .mark .skip ("no scalar support" )
260+ @testing .numpy_cupy_allclose (atol = 1e-5 )
261261 def test_nan_to_num_scalar_nan (self , xp ):
262- return xp .nan_to_num (xp .array ( xp . nan ) )
262+ return xp .nan_to_num (xp .nan )
263263
264264 @pytest .mark .filterwarnings ("ignore::RuntimeWarning" )
265265 def test_nan_to_num_inf_nan (self ):
@@ -286,14 +286,44 @@ def test_nan_to_num_inplace(self, xp):
286286 return y
287287
288288 @pytest .mark .parametrize ("kwarg" , ["nan" , "posinf" , "neginf" ])
289- def test_nan_to_num_broadcast (self , kwarg ):
289+ @testing .numpy_cupy_array_equal ()
290+ def test_nan_to_num_broadcast_same_shapes (self , xp , kwarg ):
291+ x = xp .asarray (
292+ [[0 , 1 , xp .nan , 4 ], [11 , xp .inf , 12 , 13 ]],
293+ dtype = cupy .default_float_type (),
294+ )
295+ y = xp .zeros ((2 , 4 ), dtype = x .dtype )
296+ return xp .nan_to_num (x , ** {kwarg : y })
297+
298+ @pytest .mark .parametrize ("kwarg" , ["nan" , "posinf" , "neginf" ])
299+ @testing .numpy_cupy_array_equal ()
300+ def test_nan_to_num_broadcast_different_columns (self , xp , kwarg ):
301+ x = xp .asarray (
302+ [[0 , 1 , xp .nan , 4 ], [11 , xp .inf , 12 , 13 ]],
303+ dtype = cupy .default_float_type (),
304+ )
305+ y = xp .zeros ((2 , 1 ), dtype = x .dtype )
306+ return xp .nan_to_num (x , ** {kwarg : y })
307+
308+ @pytest .mark .parametrize ("kwarg" , ["nan" , "posinf" , "neginf" ])
309+ @testing .numpy_cupy_array_equal ()
310+ def test_nan_to_num_broadcast_different_rows (self , xp , kwarg ):
311+ x = xp .asarray (
312+ [[0 , 1 , xp .nan , 4 ], [11 , - xp .inf , 12 , 13 ]],
313+ dtype = cupy .default_float_type (),
314+ )
315+ y = xp .zeros ((1 , 4 ), dtype = x .dtype )
316+ return xp .nan_to_num (x , ** {kwarg : y })
317+
318+ @pytest .mark .parametrize ("kwarg" , ["nan" , "posinf" , "neginf" ])
319+ def test_nan_to_num_broadcast_invalid_shapes (self , kwarg ):
290320 for xp in (numpy , cupy ):
291321 x = xp .asarray ([0 , 1 , xp .nan , 4 ], dtype = cupy .default_float_type ())
292- y = xp .zeros ((2 , 4 ), dtype = cupy . default_float_type () )
293- with pytest .raises (( ValueError , TypeError ) ):
322+ y = xp .zeros ((2 , 4 ), dtype = x . dtype )
323+ with pytest .raises (ValueError ):
294324 xp .nan_to_num (x , ** {kwarg : y })
295- with pytest .raises (( ValueError , TypeError ) ):
296- xp .nan_to_num (0.0 , ** {kwarg : y })
325+ with pytest .raises (ValueError ):
326+ xp .nan_to_num (xp . array ( 0.0 ) , ** {kwarg : y })
297327
298328 @testing .for_all_dtypes (no_bool = True , no_complex = True )
299329 @testing .numpy_cupy_array_equal ()
0 commit comments