24
24
_integer_dtypes ,
25
25
_integer_or_boolean_dtypes ,
26
26
_floating_dtypes ,
27
+ _complex_floating_dtypes ,
27
28
_numeric_dtypes ,
28
29
_result_type ,
29
30
_dtype_categories ,
@@ -139,7 +140,7 @@ def _check_allowed_dtypes(self, other: bool | int | float | Array, dtype_categor
139
140
140
141
if self .dtype not in _dtype_categories [dtype_category ]:
141
142
raise TypeError (f"Only { dtype_category } dtypes are allowed in { op } " )
142
- if isinstance (other , (int , float , bool )):
143
+ if isinstance (other , (int , complex , float , bool )):
143
144
other = self ._promote_scalar (other )
144
145
elif isinstance (other , Array ):
145
146
if other .dtype not in _dtype_categories [dtype_category ]:
@@ -189,11 +190,23 @@ def _promote_scalar(self, scalar):
189
190
raise TypeError (
190
191
"Python int scalars cannot be promoted with bool arrays"
191
192
)
193
+ if self .dtype in _integer_dtypes :
194
+ info = np .iinfo (self .dtype )
195
+ if not (info .min <= scalar <= info .max ):
196
+ raise OverflowError (
197
+ "Python int scalars must be within the bounds of the dtype for integer arrays"
198
+ )
199
+ # int + array(floating) is allowed
192
200
elif isinstance (scalar , float ):
193
201
if self .dtype not in _floating_dtypes :
194
202
raise TypeError (
195
203
"Python float scalars can only be promoted with floating-point arrays."
196
204
)
205
+ elif isinstance (scalar , complex ):
206
+ if self .dtype not in _complex_floating_dtypes :
207
+ raise TypeError (
208
+ "Python complex scalars can only be promoted with complex floating-point arrays."
209
+ )
197
210
else :
198
211
raise TypeError ("'scalar' must be a Python scalar" )
199
212
@@ -454,11 +467,19 @@ def __bool__(self: Array, /) -> bool:
454
467
# Note: This is an error here.
455
468
if self ._array .ndim != 0 :
456
469
raise TypeError ("bool is only allowed on arrays with 0 dimensions" )
457
- if self .dtype not in _boolean_dtypes :
458
- raise ValueError ("bool is only allowed on boolean arrays" )
459
470
res = self ._array .__bool__ ()
460
471
return res
461
472
473
+ def __complex__ (self : Array , / ) -> complex :
474
+ """
475
+ Performs the operation __complex__.
476
+ """
477
+ # Note: This is an error here.
478
+ if self ._array .ndim != 0 :
479
+ raise TypeError ("complex is only allowed on arrays with 0 dimensions" )
480
+ res = self ._array .__complex__ ()
481
+ return res
482
+
462
483
def __dlpack__ (self : Array , / , * , stream : None = None ) -> PyCapsule :
463
484
"""
464
485
Performs the operation __dlpack__.
@@ -492,16 +513,16 @@ def __float__(self: Array, /) -> float:
492
513
# Note: This is an error here.
493
514
if self ._array .ndim != 0 :
494
515
raise TypeError ("float is only allowed on arrays with 0 dimensions" )
495
- if self .dtype not in _floating_dtypes :
496
- raise ValueError ("float is only allowed on floating-point arrays" )
516
+ if self .dtype in _complex_floating_dtypes :
517
+ raise TypeError ("float is not allowed on complex floating-point arrays" )
497
518
res = self ._array .__float__ ()
498
519
return res
499
520
500
521
def __floordiv__ (self : Array , other : Union [int , float , Array ], / ) -> Array :
501
522
"""
502
523
Performs the operation __floordiv__.
503
524
"""
504
- other = self ._check_allowed_dtypes (other , "numeric" , "__floordiv__" )
525
+ other = self ._check_allowed_dtypes (other , "real numeric" , "__floordiv__" )
505
526
if other is NotImplemented :
506
527
return other
507
528
self , other = self ._normalize_two_args (self , other )
@@ -512,7 +533,7 @@ def __ge__(self: Array, other: Union[int, float, Array], /) -> Array:
512
533
"""
513
534
Performs the operation __ge__.
514
535
"""
515
- other = self ._check_allowed_dtypes (other , "numeric" , "__ge__" )
536
+ other = self ._check_allowed_dtypes (other , "real numeric" , "__ge__" )
516
537
if other is NotImplemented :
517
538
return other
518
539
self , other = self ._normalize_two_args (self , other )
@@ -542,7 +563,7 @@ def __gt__(self: Array, other: Union[int, float, Array], /) -> Array:
542
563
"""
543
564
Performs the operation __gt__.
544
565
"""
545
- other = self ._check_allowed_dtypes (other , "numeric" , "__gt__" )
566
+ other = self ._check_allowed_dtypes (other , "real numeric" , "__gt__" )
546
567
if other is NotImplemented :
547
568
return other
548
569
self , other = self ._normalize_two_args (self , other )
@@ -556,8 +577,8 @@ def __int__(self: Array, /) -> int:
556
577
# Note: This is an error here.
557
578
if self ._array .ndim != 0 :
558
579
raise TypeError ("int is only allowed on arrays with 0 dimensions" )
559
- if self .dtype not in _integer_dtypes :
560
- raise ValueError ("int is only allowed on integer arrays" )
580
+ if self .dtype in _complex_floating_dtypes :
581
+ raise TypeError ("int is not allowed on complex floating-point arrays" )
561
582
res = self ._array .__int__ ()
562
583
return res
563
584
@@ -581,7 +602,7 @@ def __le__(self: Array, other: Union[int, float, Array], /) -> Array:
581
602
"""
582
603
Performs the operation __le__.
583
604
"""
584
- other = self ._check_allowed_dtypes (other , "numeric" , "__le__" )
605
+ other = self ._check_allowed_dtypes (other , "real numeric" , "__le__" )
585
606
if other is NotImplemented :
586
607
return other
587
608
self , other = self ._normalize_two_args (self , other )
@@ -603,7 +624,7 @@ def __lt__(self: Array, other: Union[int, float, Array], /) -> Array:
603
624
"""
604
625
Performs the operation __lt__.
605
626
"""
606
- other = self ._check_allowed_dtypes (other , "numeric" , "__lt__" )
627
+ other = self ._check_allowed_dtypes (other , "real numeric" , "__lt__" )
607
628
if other is NotImplemented :
608
629
return other
609
630
self , other = self ._normalize_two_args (self , other )
@@ -626,7 +647,7 @@ def __mod__(self: Array, other: Union[int, float, Array], /) -> Array:
626
647
"""
627
648
Performs the operation __mod__.
628
649
"""
629
- other = self ._check_allowed_dtypes (other , "numeric" , "__mod__" )
650
+ other = self ._check_allowed_dtypes (other , "real numeric" , "__mod__" )
630
651
if other is NotImplemented :
631
652
return other
632
653
self , other = self ._normalize_two_args (self , other )
@@ -808,7 +829,7 @@ def __ifloordiv__(self: Array, other: Union[int, float, Array], /) -> Array:
808
829
"""
809
830
Performs the operation __ifloordiv__.
810
831
"""
811
- other = self ._check_allowed_dtypes (other , "numeric" , "__ifloordiv__" )
832
+ other = self ._check_allowed_dtypes (other , "real numeric" , "__ifloordiv__" )
812
833
if other is NotImplemented :
813
834
return other
814
835
self ._array .__ifloordiv__ (other ._array )
@@ -818,7 +839,7 @@ def __rfloordiv__(self: Array, other: Union[int, float, Array], /) -> Array:
818
839
"""
819
840
Performs the operation __rfloordiv__.
820
841
"""
821
- other = self ._check_allowed_dtypes (other , "numeric" , "__rfloordiv__" )
842
+ other = self ._check_allowed_dtypes (other , "real numeric" , "__rfloordiv__" )
822
843
if other is NotImplemented :
823
844
return other
824
845
self , other = self ._normalize_two_args (self , other )
@@ -874,7 +895,7 @@ def __imod__(self: Array, other: Union[int, float, Array], /) -> Array:
874
895
"""
875
896
Performs the operation __imod__.
876
897
"""
877
- other = self ._check_allowed_dtypes (other , "numeric" , "__imod__" )
898
+ other = self ._check_allowed_dtypes (other , "real numeric" , "__imod__" )
878
899
if other is NotImplemented :
879
900
return other
880
901
self ._array .__imod__ (other ._array )
@@ -884,7 +905,7 @@ def __rmod__(self: Array, other: Union[int, float, Array], /) -> Array:
884
905
"""
885
906
Performs the operation __rmod__.
886
907
"""
887
- other = self ._check_allowed_dtypes (other , "numeric" , "__rmod__" )
908
+ other = self ._check_allowed_dtypes (other , "real numeric" , "__rmod__" )
888
909
if other is NotImplemented :
889
910
return other
890
911
self , other = self ._normalize_two_args (self , other )
0 commit comments