@@ -38,6 +38,25 @@ def test_func(self, *args, **kw):
3838 return decorator
3939
4040
41+ def skip_inplace_numpy_uint64_casting_error (xp , x_type , y_type ):
42+ """
43+ An inplace operator does not work with NumPy when an array has a signed
44+ integer dtype and dtype of the other array is uint64. NumPy will raise
45+ a casting error in that case:
46+ numpy._core._exceptions._UFuncOutputCastingError: Cannot cast ufunc 'add'
47+ output from dtype('float64') to dtype('int64') with casting rule
48+ 'same_kind'
49+
50+ This is considering as a bug in NumPy, because
51+ numpy.can_cast(numpy.uint64, numpy.int16, casting="same_kind") is True
52+
53+ """
54+ x_dt = xp .dtype (x_type )
55+ y_dt = xp .dtype (y_type )
56+ if xp .issubdtype (x_dt , xp .signedinteger ) and y_dt == xp .uint64 :
57+ pytest .skip ("numpy.uint64 casting error" )
58+
59+
4160class TestArrayElementwiseOp :
4261
4362 @testing .for_all_dtypes_combination (names = ["x_type" , "y_type" ])
@@ -50,6 +69,7 @@ def check_array_scalar_op(
5069 x_type ,
5170 y_type ,
5271 swap = False ,
72+ inplace = False ,
5373 no_bool = False ,
5474 no_complex = False ,
5575 ):
@@ -63,6 +83,9 @@ def check_array_scalar_op(
6383 if swap :
6484 return op (y_type (3 ), a )
6585 else :
86+ if inplace :
87+ skip_inplace_numpy_uint64_casting_error (xp , x_type , y_type )
88+
6689 return op (a , y_type (3 ))
6790
6891 @testing .with_requires ("numpy>=1.25" )
@@ -74,7 +97,7 @@ def test_radd_scalar(self):
7497 self .check_array_scalar_op (operator .add , swap = True )
7598
7699 def test_iadd_scalar (self ):
77- self .check_array_scalar_op (operator .iadd )
100+ self .check_array_scalar_op (operator .iadd , inplace = True )
78101
79102 @testing .with_requires ("numpy>=1.25" )
80103 def test_sub_scalar (self ):
@@ -85,7 +108,7 @@ def test_rsub_scalar(self):
85108 self .check_array_scalar_op (operator .sub , swap = True , no_bool = True )
86109
87110 def test_isub_scalar (self ):
88- self .check_array_scalar_op (operator .isub , no_bool = True )
111+ self .check_array_scalar_op (operator .isub , no_bool = True , inplace = True )
89112
90113 @testing .with_requires ("numpy>=1.25" )
91114 def test_mul_scalar (self ):
@@ -96,7 +119,7 @@ def test_rmul_scalar(self):
96119 self .check_array_scalar_op (operator .mul , swap = True )
97120
98121 def test_imul_scalar (self ):
99- self .check_array_scalar_op (operator .imul )
122+ self .check_array_scalar_op (operator .imul , inplace = True )
100123
101124 @testing .with_requires ("numpy>=1.25" )
102125 def test_truediv_scalar (self ):
@@ -124,7 +147,9 @@ def test_rfloordiv_scalar(self):
124147
125148 def test_ifloordiv_scalar (self ):
126149 with numpy .errstate (divide = "ignore" ):
127- self .check_array_scalar_op (operator .ifloordiv , no_complex = True )
150+ self .check_array_scalar_op (
151+ operator .ifloordiv , inplace = True , no_complex = True
152+ )
128153
129154 @testing .with_requires ("numpy>=1.25" )
130155 def test_pow_scalar (self ):
@@ -138,6 +163,8 @@ def test_rpow_scalar(self):
138163 @testing .numpy_cupy_allclose (atol = 1.0 , accept_error = TypeError )
139164 @cast_exception_type ()
140165 def check_ipow_scalar (self , xp , x_type , y_type ):
166+ skip_inplace_numpy_uint64_casting_error (xp , x_type , y_type )
167+
141168 a = xp .array ([[1 , 2 , 3 ], [4 , 5 , 6 ]], x_type )
142169 return operator .ipow (a , y_type (3 ))
143170
@@ -190,8 +217,18 @@ def test_ne_scalar(self):
190217 @testing .numpy_cupy_allclose (accept_error = TypeError )
191218 @cast_exception_type ()
192219 def check_array_array_op (
193- self , op , xp , x_type , y_type , no_bool = False , no_complex = False
220+ self ,
221+ op ,
222+ xp ,
223+ x_type ,
224+ y_type ,
225+ inplace = False ,
226+ no_bool = False ,
227+ no_complex = False ,
194228 ):
229+ if inplace :
230+ skip_inplace_numpy_uint64_casting_error (xp , x_type , y_type )
231+
195232 x_dtype = numpy .dtype (x_type )
196233 y_dtype = numpy .dtype (y_type )
197234 if no_bool and x_dtype == "?" and y_dtype == "?" :
@@ -206,19 +243,19 @@ def test_add_array(self):
206243 self .check_array_array_op (operator .add )
207244
208245 def test_iadd_array (self ):
209- self .check_array_array_op (operator .iadd )
246+ self .check_array_array_op (operator .iadd , inplace = True )
210247
211248 def test_sub_array (self ):
212249 self .check_array_array_op (operator .sub , no_bool = True )
213250
214251 def test_isub_array (self ):
215- self .check_array_array_op (operator .isub , no_bool = True )
252+ self .check_array_array_op (operator .isub , inplace = True , no_bool = True )
216253
217254 def test_mul_array (self ):
218255 self .check_array_array_op (operator .mul )
219256
220257 def test_imul_array (self ):
221- self .check_array_array_op (operator .imul )
258+ self .check_array_array_op (operator .imul , inplace = True )
222259
223260 def test_truediv_array (self ):
224261 with numpy .errstate (divide = "ignore" ):
@@ -236,7 +273,9 @@ def test_ifloordiv_array(self):
236273 if "1.16.1" <= numpy .lib .NumpyVersion (numpy .__version__ ) < "1.18.0" :
237274 self .skipTest ("NumPy Issue #12927" )
238275 with numpy .errstate (divide = "ignore" ):
239- self .check_array_array_op (operator .ifloordiv , no_complex = True )
276+ self .check_array_array_op (
277+ operator .ifloordiv , inplace = True , no_complex = True
278+ )
240279
241280 @testing .for_all_dtypes_combination (names = ["x_type" , "y_type" ])
242281 @testing .numpy_cupy_allclose (atol = 1e-5 , rtol = 1e-6 , accept_error = TypeError )
@@ -257,6 +296,8 @@ def test_pow_array(self):
257296 @testing .numpy_cupy_allclose (atol = 1.0 , accept_error = TypeError )
258297 @cast_exception_type ()
259298 def check_ipow_array (self , xp , x_type , y_type ):
299+ skip_inplace_numpy_uint64_casting_error (xp , x_type , y_type )
300+
260301 a = xp .array ([[1 , 2 , 3 ], [4 , 5 , 6 ]], x_type )
261302 b = xp .array ([[6 , 5 , 4 ], [3 , 2 , 1 ]], y_type )
262303 return operator .ipow (a , b )
@@ -294,8 +335,18 @@ def test_ne_array(self):
294335 @testing .numpy_cupy_allclose (accept_error = TypeError )
295336 @cast_exception_type ()
296337 def check_array_broadcasted_op (
297- self , op , xp , x_type , y_type , no_bool = False , no_complex = False
338+ self ,
339+ op ,
340+ xp ,
341+ x_type ,
342+ y_type ,
343+ inplace = False ,
344+ no_bool = False ,
345+ no_complex = False ,
298346 ):
347+ if inplace :
348+ skip_inplace_numpy_uint64_casting_error (xp , x_type , y_type )
349+
299350 x_dtype = numpy .dtype (x_type )
300351 y_dtype = numpy .dtype (y_type )
301352 if no_bool and x_dtype == "?" and y_dtype == "?" :
@@ -310,21 +361,23 @@ def test_broadcasted_add(self):
310361 self .check_array_broadcasted_op (operator .add )
311362
312363 def test_broadcasted_iadd (self ):
313- self .check_array_broadcasted_op (operator .iadd )
364+ self .check_array_broadcasted_op (operator .iadd , inplace = True )
314365
315366 def test_broadcasted_sub (self ):
316367 # TODO(unno): sub for boolean array is deprecated in numpy>=1.13
317368 self .check_array_broadcasted_op (operator .sub , no_bool = True )
318369
319370 def test_broadcasted_isub (self ):
320371 # TODO(unno): sub for boolean array is deprecated in numpy>=1.13
321- self .check_array_broadcasted_op (operator .isub , no_bool = True )
372+ self .check_array_broadcasted_op (
373+ operator .isub , inplace = True , no_bool = True
374+ )
322375
323376 def test_broadcasted_mul (self ):
324377 self .check_array_broadcasted_op (operator .mul )
325378
326379 def test_broadcasted_imul (self ):
327- self .check_array_broadcasted_op (operator .imul )
380+ self .check_array_broadcasted_op (operator .imul , inplace = True )
328381
329382 def test_broadcasted_truediv (self ):
330383 with numpy .errstate (divide = "ignore" ):
@@ -342,7 +395,9 @@ def test_broadcasted_ifloordiv(self):
342395 if "1.16.1" <= numpy .lib .NumpyVersion (numpy .__version__ ) < "1.18.0" :
343396 self .skipTest ("NumPy Issue #12927" )
344397 with numpy .errstate (divide = "ignore" ):
345- self .check_array_broadcasted_op (operator .ifloordiv , no_complex = True )
398+ self .check_array_broadcasted_op (
399+ operator .ifloordiv , inplace = True , no_complex = True
400+ )
346401
347402 @testing .for_all_dtypes_combination (names = ["x_type" , "y_type" ])
348403 @testing .numpy_cupy_allclose (atol = 1e-5 , rtol = 1e-6 , accept_error = TypeError )
@@ -363,6 +418,8 @@ def test_broadcasted_pow(self):
363418 @testing .numpy_cupy_allclose (atol = 1.0 , accept_error = TypeError )
364419 @cast_exception_type ()
365420 def check_broadcasted_ipow (self , xp , x_type , y_type ):
421+ skip_inplace_numpy_uint64_casting_error (xp , x_type , y_type )
422+
366423 a = xp .array ([[1 , 2 , 3 ], [4 , 5 , 6 ]], x_type )
367424 b = xp .array ([[1 ], [2 ]], y_type )
368425 return operator .ipow (a , b )
@@ -659,7 +716,10 @@ def test_rmod_scalarzero(self):
659716 @testing .for_all_dtypes_combination (names = ["x_type" , "y_type" ])
660717 @testing .numpy_cupy_allclose (accept_error = TypeError )
661718 @cast_exception_type ()
662- def check_array_array_op (self , op , xp , x_type , y_type ):
719+ def check_array_array_op (self , op , xp , x_type , y_type , inplace = False ):
720+ if inplace :
721+ skip_inplace_numpy_uint64_casting_error (xp , x_type , y_type )
722+
663723 a = xp .array ([[0 , 1 , 2 ], [1 , 0 , 2 ]], dtype = x_type )
664724 b = xp .array ([[0 , 0 , 1 ], [0 , 1 , 2 ]], dtype = y_type )
665725 return op (a , b )
@@ -668,44 +728,47 @@ def test_lshift_array(self):
668728 self .check_array_array_op (operator .lshift )
669729
670730 def test_ilshift_array (self ):
671- self .check_array_array_op (operator .ilshift )
731+ self .check_array_array_op (operator .ilshift , inplace = True )
672732
673733 def test_rshift_array (self ):
674734 self .check_array_array_op (operator .rshift )
675735
676736 def test_irshift_array (self ):
677- self .check_array_array_op (operator .irshift )
737+ self .check_array_array_op (operator .irshift , inplace = True )
678738
679739 def test_and_array (self ):
680740 self .check_array_array_op (operator .and_ )
681741
682742 def test_iand_array (self ):
683- self .check_array_array_op (operator .iand )
743+ self .check_array_array_op (operator .iand , inplace = True )
684744
685745 def test_or_array (self ):
686746 self .check_array_array_op (operator .or_ )
687747
688748 def test_ior_array (self ):
689- self .check_array_array_op (operator .ior )
749+ self .check_array_array_op (operator .ior , inplace = True )
690750
691751 def test_xor_array (self ):
692752 self .check_array_array_op (operator .xor )
693753
694754 def test_ixor_array (self ):
695- self .check_array_array_op (operator .ixor )
755+ self .check_array_array_op (operator .ixor , inplace = True )
696756
697757 def test_mod_array (self ):
698758 with numpy .errstate (divide = "ignore" , invalid = "ignore" ):
699759 self .check_array_array_op (operator .mod )
700760
701761 def test_imod_array (self ):
702762 with numpy .errstate (divide = "ignore" , invalid = "ignore" ):
703- self .check_array_array_op (operator .imod )
763+ self .check_array_array_op (operator .imod , inplace = True )
704764
705765 @testing .for_all_dtypes_combination (names = ["x_type" , "y_type" ])
706766 @testing .numpy_cupy_allclose (accept_error = TypeError )
707767 @cast_exception_type ()
708- def check_array_broadcasted_op (self , op , xp , x_type , y_type ):
768+ def check_array_broadcasted_op (self , op , xp , x_type , y_type , inplace = False ):
769+ if inplace :
770+ skip_inplace_numpy_uint64_casting_error (xp , x_type , y_type )
771+
709772 a = xp .array ([[0 , 1 , 2 ], [1 , 0 , 2 ], [2 , 1 , 0 ]], dtype = x_type )
710773 b = xp .array ([[0 , 0 , 1 ]], dtype = y_type )
711774 return op (a , b )
@@ -714,39 +777,39 @@ def test_broadcasted_lshift(self):
714777 self .check_array_broadcasted_op (operator .lshift )
715778
716779 def test_broadcasted_ilshift (self ):
717- self .check_array_broadcasted_op (operator .ilshift )
780+ self .check_array_broadcasted_op (operator .ilshift , inplace = True )
718781
719782 def test_broadcasted_rshift (self ):
720783 self .check_array_broadcasted_op (operator .rshift )
721784
722785 def test_broadcasted_irshift (self ):
723- self .check_array_broadcasted_op (operator .irshift )
786+ self .check_array_broadcasted_op (operator .irshift , inplace = True )
724787
725788 def test_broadcasted_and (self ):
726789 self .check_array_broadcasted_op (operator .and_ )
727790
728791 def test_broadcasted_iand (self ):
729- self .check_array_broadcasted_op (operator .iand )
792+ self .check_array_broadcasted_op (operator .iand , inplace = True )
730793
731794 def test_broadcasted_or (self ):
732795 self .check_array_broadcasted_op (operator .or_ )
733796
734797 def test_broadcasted_ior (self ):
735- self .check_array_broadcasted_op (operator .ior )
798+ self .check_array_broadcasted_op (operator .ior , inplace = True )
736799
737800 def test_broadcasted_xor (self ):
738801 self .check_array_broadcasted_op (operator .xor )
739802
740803 def test_broadcasted_ixor (self ):
741- self .check_array_broadcasted_op (operator .ixor )
804+ self .check_array_broadcasted_op (operator .ixor , inplace = True )
742805
743806 def test_broadcasted_mod (self ):
744807 with numpy .errstate (divide = "ignore" , invalid = "ignore" ):
745808 self .check_array_broadcasted_op (operator .mod )
746809
747810 def test_broadcasted_imod (self ):
748811 with numpy .errstate (divide = "ignore" , invalid = "ignore" ):
749- self .check_array_broadcasted_op (operator .imod )
812+ self .check_array_broadcasted_op (operator .imod , inplace = True )
750813
751814 @testing .for_all_dtypes_combination (names = ["x_type" , "y_type" ])
752815 @testing .numpy_cupy_allclose (accept_error = TypeError )
0 commit comments