@@ -210,7 +210,7 @@ from typing import (
210
210
# library include `typing_extensions` stubs:
211
211
# https://github.com/python/typeshed/blob/main/stdlib/typing_extensions.pyi
212
212
from _typeshed import StrOrBytesPath , SupportsFlush , SupportsLenAndGetItem , SupportsWrite
213
- from typing_extensions import CapsuleType , Generic , LiteralString , Protocol , Self , TypeVar , overload
213
+ from typing_extensions import CapsuleType , Generic , LiteralString , Protocol , Self , TypeVar , deprecated , overload
214
214
215
215
from numpy import (
216
216
core ,
@@ -1377,6 +1377,10 @@ _SortSide: TypeAlias = L["left", "right"]
1377
1377
1378
1378
@type_check_only
1379
1379
class _ArrayOrScalarCommon :
1380
+ @property
1381
+ def real (self , / ) -> Any : ...
1382
+ @property
1383
+ def imag (self , / ) -> Any : ...
1380
1384
@property
1381
1385
def T (self ) -> Self : ...
1382
1386
@property
@@ -1391,17 +1395,18 @@ class _ArrayOrScalarCommon:
1391
1395
def nbytes (self ) -> int : ...
1392
1396
@property
1393
1397
def device (self ) -> L ["cpu" ]: ...
1394
- def __bool__ ( self ) -> builtins . bool : ...
1395
- def __bytes__ (self ) -> bytes : ...
1396
- def __str__ (self ) -> str : ...
1397
- def __repr__ (self ) -> str : ...
1398
+
1399
+ def __bool__ (self , / ) -> builtins . bool : ...
1400
+ def __int__ (self , / ) -> int : ...
1401
+ def __float__ (self , / ) -> float : ...
1398
1402
def __copy__ (self ) -> Self : ...
1399
1403
def __deepcopy__ (self , memo : None | dict [int , Any ], / ) -> Self : ...
1400
1404
1401
1405
# TODO: How to deal with the non-commutative nature of `==` and `!=`?
1402
1406
# xref numpy/numpy#17368
1403
1407
def __eq__ (self , other : Any , / ) -> Any : ...
1404
1408
def __ne__ (self , other : Any , / ) -> Any : ...
1409
+
1405
1410
def copy (self , order : _OrderKACF = ...) -> Self : ...
1406
1411
def dump (self , file : StrOrBytesPath | SupportsWrite [bytes ]) -> None : ...
1407
1412
def dumps (self ) -> bytes : ...
@@ -1418,7 +1423,7 @@ class _ArrayOrScalarCommon:
1418
1423
@property
1419
1424
def __array_priority__ (self ) -> float : ...
1420
1425
@property
1421
- def __array_struct__ (self ) -> Any : ... # builtins.PyCapsule
1426
+ def __array_struct__ (self ) -> CapsuleType : ... # builtins.PyCapsule
1422
1427
def __array_namespace__ (self , / , * , api_version : _ArrayAPIVersion | None = None ) -> ModuleType : ...
1423
1428
def __setstate__ (self , state : tuple [
1424
1429
SupportsIndex , # version
@@ -2230,8 +2235,8 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
2230
2235
) -> NDArray [Any ]: ...
2231
2236
2232
2237
def __index__ (self : NDArray [np .integer [Any ]], / ) -> int : ...
2233
- def __int__ (self : NDArray [number [Any ] | np .bool | object_ ], / ) -> int : ...
2234
- def __float__ (self : NDArray [number [Any ] | np .bool | object_ ], / ) -> float : ...
2238
+ def __int__ (self : NDArray [number [Any ] | np .timedelta64 | np . bool | object_ ], / ) -> int : ...
2239
+ def __float__ (self : NDArray [number [Any ] | np .timedelta64 | np . bool | object_ ], / ) -> float : ...
2235
2240
def __complex__ (self : NDArray [number [Any ] | np .bool | object_ ], / ) -> complex : ...
2236
2241
2237
2242
def __len__ (self ) -> int : ...
@@ -3254,14 +3259,7 @@ class generic(_ArrayOrScalarCommon):
3254
3259
def dtype (self ) -> _dtype [Self ]: ...
3255
3260
3256
3261
class number (generic , Generic [_NBit1 ]): # type: ignore
3257
- @property
3258
- def real (self ) -> Self : ...
3259
- @property
3260
- def imag (self ) -> Self : ...
3261
3262
def __class_getitem__ (cls , item : Any , / ) -> GenericAlias : ...
3262
- def __int__ (self ) -> int : ...
3263
- def __float__ (self ) -> float : ...
3264
- def __complex__ (self ) -> complex : ...
3265
3263
def __neg__ (self ) -> Self : ...
3266
3264
def __pos__ (self ) -> Self : ...
3267
3265
def __abs__ (self ) -> Self : ...
@@ -3283,19 +3281,19 @@ class number(generic, Generic[_NBit1]): # type: ignore
3283
3281
__gt__ : _ComparisonOpGT [_NumberLike_co , _ArrayLikeNumber_co ]
3284
3282
__ge__ : _ComparisonOpGE [_NumberLike_co , _ArrayLikeNumber_co ]
3285
3283
3286
- class bool (generic ):
3287
- def __init__ (self , value : object = ..., / ) -> None : ...
3288
- def item (
3289
- self , args : L [0 ] | tuple [()] | tuple [L [0 ]] = ..., / ,
3290
- ) -> builtins .bool : ...
3291
- def tolist (self ) -> builtins .bool : ...
3284
+ @type_check_only
3285
+ class _RealMixin :
3292
3286
@property
3293
3287
def real (self ) -> Self : ...
3294
3288
@property
3295
3289
def imag (self ) -> Self : ...
3296
- def __int__ (self ) -> int : ...
3297
- def __float__ (self ) -> float : ...
3298
- def __complex__ (self ) -> complex : ...
3290
+
3291
+ class bool (_RealMixin , generic ):
3292
+ def __init__ (self , value : object = ..., / ) -> None : ...
3293
+ def item (self , args : L [0 ] | tuple [()] | tuple [L [0 ]] = ..., / ) -> builtins .bool : ...
3294
+ def tolist (self ) -> builtins .bool : ...
3295
+ @deprecated ("In future, it will be an error for 'np.bool' scalars to be interpreted as an index" )
3296
+ def __index__ (self , / ) -> int : ...
3299
3297
def __abs__ (self ) -> Self : ...
3300
3298
__add__ : _BoolOp [np .bool ]
3301
3299
__radd__ : _BoolOp [np .bool ]
@@ -3332,13 +3330,12 @@ class bool(generic):
3332
3330
bool_ : TypeAlias = bool
3333
3331
3334
3332
_StringType = TypeVar ("_StringType" , bound = str | bytes )
3335
- _ShapeType = TypeVar ("_ShapeType" , bound = _Shape )
3336
3333
_ObjectType = TypeVar ("_ObjectType" , bound = object )
3337
3334
3338
3335
# The `object_` constructor returns the passed object, so instances with type
3339
3336
# `object_` cannot exists (at runtime).
3340
3337
@final
3341
- class object_ (generic ):
3338
+ class object_ (_RealMixin , generic ):
3342
3339
@overload
3343
3340
def __new__ (cls , nothing_to_see_here : None = ..., / ) -> None : ...
3344
3341
@overload
@@ -3353,16 +3350,6 @@ class object_(generic):
3353
3350
@overload
3354
3351
def __new__ (cls , value : Any = ..., / ) -> object | NDArray [object_ ]: ...
3355
3352
3356
- @property
3357
- def real (self ) -> Self : ...
3358
- @property
3359
- def imag (self ) -> Self : ...
3360
- # The 3 protocols below may or may not raise,
3361
- # depending on the underlying object
3362
- def __int__ (self ) -> int : ...
3363
- def __float__ (self ) -> float : ...
3364
- def __complex__ (self ) -> complex : ...
3365
-
3366
3353
if sys .version_info >= (3 , 12 ):
3367
3354
def __release_buffer__ (self , buffer : memoryview , / ) -> None : ...
3368
3355
@@ -3379,7 +3366,7 @@ class _DatetimeScalar(Protocol):
3379
3366
3380
3367
# TODO: `item`/`tolist` returns either `dt.date`, `dt.datetime` or `int`
3381
3368
# depending on the unit
3382
- class datetime64 (generic ):
3369
+ class datetime64 (_RealMixin , generic ):
3383
3370
@overload
3384
3371
def __init__ (
3385
3372
self ,
@@ -3417,25 +3404,29 @@ _ComplexValue: TypeAlias = (
3417
3404
| complex # `complex` is not a subtype of `SupportsComplex`
3418
3405
)
3419
3406
3420
- class integer (number [_NBit1 ]): # type: ignore
3407
+ @type_check_only
3408
+ class _RoundMixin :
3409
+ @overload
3410
+ def __round__ (self , / , ndigits : None = None ) -> int : ...
3411
+ @overload
3412
+ def __round__ (self , / , ndigits : SupportsIndex ) -> Self : ...
3413
+
3414
+ @type_check_only
3415
+ class _IntegralMixin (_RealMixin ):
3421
3416
@property
3422
3417
def numerator (self ) -> Self : ...
3423
3418
@property
3424
3419
def denominator (self ) -> L [1 ]: ...
3425
- @overload
3426
- def __round__ (self , ndigits : None = ..., / ) -> int : ...
3427
- @overload
3428
- def __round__ (self , ndigits : SupportsIndex , / ) -> Self : ...
3429
3420
3430
- # NOTE: `__index__` is technically defined in the bottom-most
3431
- # sub-classes (`int64`, `uint32`, etc)
3432
- def item (
3433
- self , args : L [0 ] | tuple [()] | tuple [L [0 ]] = ..., / ,
3434
- ) -> int : ...
3421
+ class integer (_IntegralMixin , _RoundMixin , number [_NBit1 ]): # type: ignore
3422
+ def is_integer (self , / ) -> L [True ]: ...
3423
+ def item (self , args : L [0 ] | tuple [()] | tuple [L [0 ]] = ..., / ) -> int : ...
3435
3424
def tolist (self ) -> int : ...
3436
- def is_integer (self ) -> L [True ]: ...
3437
- def bit_count (self ) -> int : ...
3438
- def __index__ (self ) -> int : ...
3425
+
3426
+ # NOTE: `bit_count` and `__index__` are technically defined in the concrete subtypes
3427
+ def bit_count (self , / ) -> int : ...
3428
+ def __index__ (self , / ) -> int : ...
3429
+
3439
3430
__truediv__ : _IntTrueDiv [_NBit1 ]
3440
3431
__rtruediv__ : _IntTrueDiv [_NBit1 ]
3441
3432
def __mod__ (self , value : _IntLike_co , / ) -> integer [Any ]: ...
@@ -3495,23 +3486,16 @@ longlong = signedinteger[_NBitLongLong]
3495
3486
3496
3487
# TODO: `item`/`tolist` returns either `dt.timedelta` or `int`
3497
3488
# depending on the unit
3498
- class timedelta64 (generic ):
3489
+ class timedelta64 (_IntegralMixin , generic ):
3499
3490
def __init__ (
3500
3491
self ,
3501
3492
value : None | int | _CharLike_co | dt .timedelta | timedelta64 = ...,
3502
3493
format : _CharLike_co | tuple [_CharLike_co , _IntLike_co ] = ...,
3503
3494
/ ,
3504
3495
) -> None : ...
3505
- @property
3506
- def numerator (self ) -> Self : ...
3507
- @property
3508
- def denominator (self ) -> L [1 ]: ...
3509
3496
3510
3497
# NOTE: Only a limited number of units support conversion
3511
3498
# to builtin scalar types: `Y`, `M`, `ns`, `ps`, `fs`, `as`
3512
- def __int__ (self ) -> int : ...
3513
- def __float__ (self ) -> float : ...
3514
- def __complex__ (self ) -> complex : ...
3515
3499
def __neg__ (self ) -> Self : ...
3516
3500
def __pos__ (self ) -> Self : ...
3517
3501
def __abs__ (self ) -> Self : ...
@@ -3577,18 +3561,15 @@ ulonglong: TypeAlias = unsignedinteger[_NBitLongLong]
3577
3561
3578
3562
class inexact (number [_NBit1 ]): ... # type: ignore[misc]
3579
3563
3580
- _IntType = TypeVar ("_IntType" , bound = integer [Any ])
3581
-
3582
- class floating (inexact [_NBit1 ]):
3564
+ class floating (_RealMixin , _RoundMixin , inexact [_NBit1 ]):
3583
3565
def __init__ (self , value : _FloatValue = ..., / ) -> None : ...
3584
3566
def item (self , args : L [0 ] | tuple [()] | tuple [L [0 ]] = ..., / ) -> float : ...
3585
3567
def tolist (self ) -> float : ...
3586
- def is_integer (self ) -> builtins .bool : ...
3587
- def as_integer_ratio (self ) -> tuple [int , int ]: ...
3588
- @overload
3589
- def __round__ (self , ndigits : None = ..., / ) -> int : ...
3590
- @overload
3591
- def __round__ (self , ndigits : SupportsIndex , / ) -> Self : ...
3568
+
3569
+ # NOTE: `is_integer` and `as_integer_ratio` are technically defined in the concrete subtypes
3570
+ def is_integer (self , / ) -> builtins .bool : ...
3571
+ def as_integer_ratio (self , / ) -> tuple [int , int ]: ...
3572
+
3592
3573
__add__ : _FloatOp [_NBit1 ]
3593
3574
__radd__ : _FloatOp [_NBit1 ]
3594
3575
__sub__ : _FloatOp [_NBit1 ]
@@ -3617,7 +3598,7 @@ class float64(floating[_64Bit], float): # type: ignore[misc]
3617
3598
def __getformat__ (self , typestr : L ["double" , "float" ], / ) -> str : ...
3618
3599
def __getnewargs__ (self , / ) -> tuple [float ]: ...
3619
3600
3620
- # overrides for `floating` and `builtins.float` compatibility
3601
+ # overrides for `floating` and `builtins.float` compatibility (`_RealMixin` doesn't work)
3621
3602
@property
3622
3603
def real (self ) -> Self : ...
3623
3604
@property
@@ -3754,9 +3735,16 @@ class complexfloating(inexact[_NBit1], Generic[_NBit1, _NBit2]):
3754
3735
def real (self ) -> floating [_NBit1 ]: ... # type: ignore[override]
3755
3736
@property
3756
3737
def imag (self ) -> floating [_NBit2 ]: ... # type: ignore[override]
3757
- def __abs__ (self ) -> floating [_NBit1 | _NBit2 ]: ... # type: ignore[override]
3758
- # NOTE: Deprecated
3759
- # def __round__(self, ndigits=...): ...
3738
+
3739
+ # NOTE: `__complex__` is technically defined in the concrete subtypes
3740
+ def __complex__ (self , / ) -> complex : ...
3741
+ def __abs__ (self , / ) -> floating [_NBit1 | _NBit2 ]: ... # type: ignore[override]
3742
+ @deprecated (
3743
+ "The Python built-in `round` is deprecated for complex scalars, and will raise a `TypeError` in a future release. "
3744
+ "Use `np.round` or `scalar.round` instead."
3745
+ )
3746
+ def __round__ (self , / , ndigits : SupportsIndex | None = None ) -> Self : ...
3747
+
3760
3748
@overload
3761
3749
def __add__ (self , other : _Complex64_co , / ) -> complexfloating [_NBit1 , _NBit2 ]: ...
3762
3750
@overload
@@ -3871,7 +3859,7 @@ csingle: TypeAlias = complexfloating[_NBitSingle, _NBitSingle]
3871
3859
cdouble : TypeAlias = complexfloating [_NBitDouble , _NBitDouble ]
3872
3860
clongdouble : TypeAlias = complexfloating [_NBitLongDouble , _NBitLongDouble ]
3873
3861
3874
- class flexible (generic ): ... # type: ignore
3862
+ class flexible (_RealMixin , generic ): ... # type: ignore
3875
3863
3876
3864
# TODO: `item`/`tolist` returns either `bytes` or `tuple`
3877
3865
# depending on whether or not it's used as an opaque bytes sequence
@@ -3881,13 +3869,7 @@ class void(flexible):
3881
3869
def __init__ (self , value : _IntLike_co | bytes , / , dtype : None = ...) -> None : ...
3882
3870
@overload
3883
3871
def __init__ (self , value : Any , / , dtype : _DTypeLikeVoid ) -> None : ...
3884
- @property
3885
- def real (self ) -> Self : ...
3886
- @property
3887
- def imag (self ) -> Self : ...
3888
- def setfield (
3889
- self , val : ArrayLike , dtype : DTypeLike , offset : int = ...
3890
- ) -> None : ...
3872
+ def setfield (self , val : ArrayLike , dtype : DTypeLike , offset : int = ...) -> None : ...
3891
3873
@overload
3892
3874
def __getitem__ (self , key : str | SupportsIndex , / ) -> Any : ...
3893
3875
@overload
@@ -3899,9 +3881,7 @@ class void(flexible):
3899
3881
/ ,
3900
3882
) -> None : ...
3901
3883
3902
- class character (flexible ): # type: ignore
3903
- def __int__ (self ) -> int : ...
3904
- def __float__ (self ) -> float : ...
3884
+ class character (flexible ): ... # type: ignore
3905
3885
3906
3886
# NOTE: Most `np.bytes_` / `np.str_` methods return their
3907
3887
# builtin `bytes` / `str` counterpart
@@ -3913,6 +3893,7 @@ class bytes_(character, bytes):
3913
3893
def __init__ (
3914
3894
self , value : str , / , encoding : str = ..., errors : str = ...
3915
3895
) -> None : ...
3896
+ def __bytes__ (self , / ) -> bytes : ...
3916
3897
def item (
3917
3898
self , args : L [0 ] | tuple [()] | tuple [L [0 ]] = ..., / ,
3918
3899
) -> bytes : ...
0 commit comments