@@ -44,6 +44,7 @@ from numpy._typing import (
44
44
_DTypeLikeVoid ,
45
45
_VoidDTypeLike ,
46
46
# Shapes
47
+ _AnyShape ,
47
48
_Shape ,
48
49
_ShapeLike ,
49
50
# Scalars
@@ -794,7 +795,7 @@ _RealArrayT = TypeVar("_RealArrayT", bound=NDArray[floating | integer | timedelt
794
795
_NumericArrayT = TypeVar ("_NumericArrayT" , bound = NDArray [number | timedelta64 | object_ ])
795
796
796
797
_ShapeT = TypeVar ("_ShapeT" , bound = _Shape )
797
- _ShapeT_co = TypeVar ("_ShapeT_co" , bound = _Shape , default = _Shape , covariant = True )
798
+ _ShapeT_co = TypeVar ("_ShapeT_co" , bound = _Shape , default = _AnyShape , covariant = True )
798
799
_1DShapeT = TypeVar ("_1DShapeT" , bound = _1D )
799
800
_2DShapeT_co = TypeVar ("_2DShapeT_co" , bound = _2D , default = _2D , covariant = True )
800
801
_1NShapeT = TypeVar ("_1NShapeT" , bound = tuple [L [1 ], * tuple [L [1 ], ...]]) # (1,) | (1, 1) | (1, 1, 1) | ...
@@ -1580,11 +1581,11 @@ class dtype(Generic[_ScalarT_co], metaclass=_DTypeMeta):
1580
1581
@property
1581
1582
def num (self ) -> _DTypeNum : ...
1582
1583
@property
1583
- def shape (self ) -> tuple [()] | _Shape : ...
1584
+ def shape (self ) -> _AnyShape : ...
1584
1585
@property
1585
1586
def ndim (self ) -> int : ...
1586
1587
@property
1587
- def subdtype (self ) -> tuple [dtype , _Shape ] | None : ...
1588
+ def subdtype (self ) -> tuple [dtype , _AnyShape ] | None : ...
1588
1589
def newbyteorder (self , new_order : _ByteOrder = ..., / ) -> Self : ...
1589
1590
@property
1590
1591
def str (self ) -> LiteralString : ...
@@ -1628,9 +1629,9 @@ class flatiter(Generic[_ArrayT_co]):
1628
1629
@overload
1629
1630
def __array__ (self : flatiter [ndarray [_1DShapeT , Any ]], dtype : _DTypeT , / ) -> ndarray [_1DShapeT , _DTypeT ]: ...
1630
1631
@overload
1631
- def __array__ (self : flatiter [ndarray [_Shape , _DTypeT ]], dtype : None = ..., / ) -> ndarray [_Shape , _DTypeT ]: ...
1632
+ def __array__ (self : flatiter [ndarray [Any , _DTypeT ]], dtype : None = ..., / ) -> ndarray [_AnyShape , _DTypeT ]: ...
1632
1633
@overload
1633
- def __array__ (self , dtype : _DTypeT , / ) -> ndarray [_Shape , _DTypeT ]: ...
1634
+ def __array__ (self , dtype : _DTypeT , / ) -> ndarray [_AnyShape , _DTypeT ]: ...
1634
1635
1635
1636
@type_check_only
1636
1637
class _ArrayOrScalarCommon :
@@ -2085,11 +2086,11 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2085
2086
) -> ndarray [_ShapeT , _DTypeT ]: ...
2086
2087
2087
2088
@overload
2088
- def __getitem__ (self , key : _ArrayInt_co | tuple [_ArrayInt_co , ...], / ) -> ndarray [_Shape , _DTypeT_co ]: ...
2089
+ def __getitem__ (self , key : _ArrayInt_co | tuple [_ArrayInt_co , ...], / ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2089
2090
@overload
2090
2091
def __getitem__ (self , key : SupportsIndex | tuple [SupportsIndex , ...], / ) -> Any : ...
2091
2092
@overload
2092
- def __getitem__ (self , key : _ToIndices , / ) -> ndarray [_Shape , _DTypeT_co ]: ...
2093
+ def __getitem__ (self , key : _ToIndices , / ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2093
2094
@overload
2094
2095
def __getitem__ (self : NDArray [void ], key : str , / ) -> ndarray [_ShapeT_co , np .dtype ]: ...
2095
2096
@overload
@@ -2167,6 +2168,8 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2167
2168
* args : SupportsIndex ,
2168
2169
) -> str : ...
2169
2170
2171
+ @overload # this first overload prevents mypy from over-eagerly selecting `tuple[()]` in case of `_AnyShape`
2172
+ def tolist (self : ndarray [tuple [Never ], dtype [generic [_T ]]], / ) -> Any : ...
2170
2173
@overload
2171
2174
def tolist (self : ndarray [tuple [()], dtype [generic [_T ]]], / ) -> _T : ...
2172
2175
@overload
@@ -2188,13 +2191,13 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2188
2191
def squeeze (
2189
2192
self ,
2190
2193
axis : SupportsIndex | tuple [SupportsIndex , ...] | None = ...,
2191
- ) -> ndarray [_Shape , _DTypeT_co ]: ...
2194
+ ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2192
2195
2193
2196
def swapaxes (
2194
2197
self ,
2195
2198
axis1 : SupportsIndex ,
2196
2199
axis2 : SupportsIndex ,
2197
- ) -> ndarray [_Shape , _DTypeT_co ]: ...
2200
+ ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2198
2201
2199
2202
@overload
2200
2203
def transpose (self , axes : _ShapeLike | None , / ) -> Self : ...
@@ -2321,7 +2324,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2321
2324
offset : SupportsIndex = ...,
2322
2325
axis1 : SupportsIndex = ...,
2323
2326
axis2 : SupportsIndex = ...,
2324
- ) -> ndarray [_Shape , _DTypeT_co ]: ...
2327
+ ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2325
2328
2326
2329
# 1D + 1D returns a scalar;
2327
2330
# all other with at least 1 non-0D array return an ndarray.
@@ -2397,7 +2400,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2397
2400
axis : SupportsIndex | None = ...,
2398
2401
out : None = ...,
2399
2402
mode : _ModeKind = ...,
2400
- ) -> ndarray [_Shape , _DTypeT_co ]: ...
2403
+ ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2401
2404
@overload
2402
2405
def take (
2403
2406
self ,
@@ -2418,7 +2421,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2418
2421
self ,
2419
2422
repeats : _ArrayLikeInt_co ,
2420
2423
axis : SupportsIndex ,
2421
- ) -> ndarray [_Shape , _DTypeT_co ]: ...
2424
+ ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2422
2425
2423
2426
def flatten (self , / , order : _OrderKACF = "C" ) -> ndarray [tuple [int ], _DTypeT_co ]: ...
2424
2427
def ravel (self , / , order : _OrderKACF = "C" ) -> ndarray [tuple [int ], _DTypeT_co ]: ...
@@ -2494,7 +2497,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2494
2497
* shape : SupportsIndex ,
2495
2498
order : _OrderACF = "C" ,
2496
2499
copy : builtins .bool | None = None ,
2497
- ) -> ndarray [_Shape , _DTypeT_co ]: ...
2500
+ ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2498
2501
@overload # (sequence[index])
2499
2502
def reshape (
2500
2503
self ,
@@ -2503,7 +2506,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2503
2506
* ,
2504
2507
order : _OrderACF = "C" ,
2505
2508
copy : builtins .bool | None = None ,
2506
- ) -> ndarray [_Shape , _DTypeT_co ]: ...
2509
+ ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2507
2510
2508
2511
@overload
2509
2512
def astype (
@@ -4986,7 +4989,7 @@ class broadcast:
4986
4989
@property
4987
4990
def numiter (self ) -> int : ...
4988
4991
@property
4989
- def shape (self ) -> _Shape : ...
4992
+ def shape (self ) -> _AnyShape : ...
4990
4993
@property
4991
4994
def size (self ) -> int : ...
4992
4995
def __next__ (self ) -> tuple [Any , ...]: ...
@@ -5443,8 +5446,8 @@ class matrix(ndarray[_2DShapeT_co, _DTypeT_co]):
5443
5446
def A (self ) -> ndarray [_2DShapeT_co , _DTypeT_co ]: ...
5444
5447
def getA (self ) -> ndarray [_2DShapeT_co , _DTypeT_co ]: ...
5445
5448
@property
5446
- def A1 (self ) -> ndarray [_Shape , _DTypeT_co ]: ...
5447
- def getA1 (self ) -> ndarray [_Shape , _DTypeT_co ]: ...
5449
+ def A1 (self ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
5450
+ def getA1 (self ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
5448
5451
@property
5449
5452
def H (self ) -> matrix [_2D , _DTypeT_co ]: ...
5450
5453
def getH (self ) -> matrix [_2D , _DTypeT_co ]: ...
0 commit comments