@@ -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) | ...
@@ -1579,11 +1580,11 @@ class dtype(Generic[_ScalarT_co], metaclass=_DTypeMeta):
1579
1580
@property
1580
1581
def num (self ) -> _DTypeNum : ...
1581
1582
@property
1582
- def shape (self ) -> tuple [()] | _Shape : ...
1583
+ def shape (self ) -> _AnyShape : ...
1583
1584
@property
1584
1585
def ndim (self ) -> int : ...
1585
1586
@property
1586
- def subdtype (self ) -> tuple [dtype , _Shape ] | None : ...
1587
+ def subdtype (self ) -> tuple [dtype , _AnyShape ] | None : ...
1587
1588
def newbyteorder (self , new_order : _ByteOrder = ..., / ) -> Self : ...
1588
1589
@property
1589
1590
def str (self ) -> LiteralString : ...
@@ -1627,9 +1628,9 @@ class flatiter(Generic[_ArrayT_co]):
1627
1628
@overload
1628
1629
def __array__ (self : flatiter [ndarray [_1DShapeT , Any ]], dtype : _DTypeT , / ) -> ndarray [_1DShapeT , _DTypeT ]: ...
1629
1630
@overload
1630
- def __array__ (self : flatiter [ndarray [_Shape , _DTypeT ]], dtype : None = ..., / ) -> ndarray [_Shape , _DTypeT ]: ...
1631
+ def __array__ (self : flatiter [ndarray [Any , _DTypeT ]], dtype : None = ..., / ) -> ndarray [_AnyShape , _DTypeT ]: ...
1631
1632
@overload
1632
- def __array__ (self , dtype : _DTypeT , / ) -> ndarray [_Shape , _DTypeT ]: ...
1633
+ def __array__ (self , dtype : _DTypeT , / ) -> ndarray [_AnyShape , _DTypeT ]: ...
1633
1634
1634
1635
@type_check_only
1635
1636
class _ArrayOrScalarCommon :
@@ -2084,11 +2085,11 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2084
2085
) -> ndarray [_ShapeT , _DTypeT ]: ...
2085
2086
2086
2087
@overload
2087
- def __getitem__ (self , key : _ArrayInt_co | tuple [_ArrayInt_co , ...], / ) -> ndarray [_Shape , _DTypeT_co ]: ...
2088
+ def __getitem__ (self , key : _ArrayInt_co | tuple [_ArrayInt_co , ...], / ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2088
2089
@overload
2089
2090
def __getitem__ (self , key : SupportsIndex | tuple [SupportsIndex , ...], / ) -> Any : ...
2090
2091
@overload
2091
- def __getitem__ (self , key : _ToIndices , / ) -> ndarray [_Shape , _DTypeT_co ]: ...
2092
+ def __getitem__ (self , key : _ToIndices , / ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2092
2093
@overload
2093
2094
def __getitem__ (self : NDArray [void ], key : str , / ) -> ndarray [_ShapeT_co , np .dtype ]: ...
2094
2095
@overload
@@ -2166,6 +2167,8 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2166
2167
* args : SupportsIndex ,
2167
2168
) -> str : ...
2168
2169
2170
+ @overload # this first overload prevents mypy from over-eagerly selecting `tuple[()]` in case of `_AnyShape`
2171
+ def tolist (self : ndarray [tuple [Never ], dtype [generic [_T ]]], / ) -> Any : ...
2169
2172
@overload
2170
2173
def tolist (self : ndarray [tuple [()], dtype [generic [_T ]]], / ) -> _T : ...
2171
2174
@overload
@@ -2187,13 +2190,13 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2187
2190
def squeeze (
2188
2191
self ,
2189
2192
axis : SupportsIndex | tuple [SupportsIndex , ...] | None = ...,
2190
- ) -> ndarray [_Shape , _DTypeT_co ]: ...
2193
+ ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2191
2194
2192
2195
def swapaxes (
2193
2196
self ,
2194
2197
axis1 : SupportsIndex ,
2195
2198
axis2 : SupportsIndex ,
2196
- ) -> ndarray [_Shape , _DTypeT_co ]: ...
2199
+ ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2197
2200
2198
2201
@overload
2199
2202
def transpose (self , axes : _ShapeLike | None , / ) -> Self : ...
@@ -2320,7 +2323,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2320
2323
offset : SupportsIndex = ...,
2321
2324
axis1 : SupportsIndex = ...,
2322
2325
axis2 : SupportsIndex = ...,
2323
- ) -> ndarray [_Shape , _DTypeT_co ]: ...
2326
+ ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2324
2327
2325
2328
# 1D + 1D returns a scalar;
2326
2329
# all other with at least 1 non-0D array return an ndarray.
@@ -2396,7 +2399,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2396
2399
axis : SupportsIndex | None = ...,
2397
2400
out : None = ...,
2398
2401
mode : _ModeKind = ...,
2399
- ) -> ndarray [_Shape , _DTypeT_co ]: ...
2402
+ ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2400
2403
@overload
2401
2404
def take (
2402
2405
self ,
@@ -2417,7 +2420,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2417
2420
self ,
2418
2421
repeats : _ArrayLikeInt_co ,
2419
2422
axis : SupportsIndex ,
2420
- ) -> ndarray [_Shape , _DTypeT_co ]: ...
2423
+ ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2421
2424
2422
2425
def flatten (self , / , order : _OrderKACF = "C" ) -> ndarray [tuple [int ], _DTypeT_co ]: ...
2423
2426
def ravel (self , / , order : _OrderKACF = "C" ) -> ndarray [tuple [int ], _DTypeT_co ]: ...
@@ -2493,7 +2496,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2493
2496
* shape : SupportsIndex ,
2494
2497
order : _OrderACF = "C" ,
2495
2498
copy : builtins .bool | None = None ,
2496
- ) -> ndarray [_Shape , _DTypeT_co ]: ...
2499
+ ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2497
2500
@overload # (sequence[index])
2498
2501
def reshape (
2499
2502
self ,
@@ -2502,7 +2505,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
2502
2505
* ,
2503
2506
order : _OrderACF = "C" ,
2504
2507
copy : builtins .bool | None = None ,
2505
- ) -> ndarray [_Shape , _DTypeT_co ]: ...
2508
+ ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
2506
2509
2507
2510
@overload
2508
2511
def astype (
@@ -4941,7 +4944,7 @@ class broadcast:
4941
4944
@property
4942
4945
def numiter (self ) -> int : ...
4943
4946
@property
4944
- def shape (self ) -> _Shape : ...
4947
+ def shape (self ) -> _AnyShape : ...
4945
4948
@property
4946
4949
def size (self ) -> int : ...
4947
4950
def __next__ (self ) -> tuple [Any , ...]: ...
@@ -5398,8 +5401,8 @@ class matrix(ndarray[_2DShapeT_co, _DTypeT_co]):
5398
5401
def A (self ) -> ndarray [_2DShapeT_co , _DTypeT_co ]: ...
5399
5402
def getA (self ) -> ndarray [_2DShapeT_co , _DTypeT_co ]: ...
5400
5403
@property
5401
- def A1 (self ) -> ndarray [_Shape , _DTypeT_co ]: ...
5402
- def getA1 (self ) -> ndarray [_Shape , _DTypeT_co ]: ...
5404
+ def A1 (self ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
5405
+ def getA1 (self ) -> ndarray [_AnyShape , _DTypeT_co ]: ...
5403
5406
@property
5404
5407
def H (self ) -> matrix [_2D , _DTypeT_co ]: ...
5405
5408
def getH (self ) -> matrix [_2D , _DTypeT_co ]: ...
0 commit comments