8000 TYP: Stop using `Any` as shape-type default · numpy/numpy@11606e9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 11606e9

Browse files
committed
TYP: Stop using Any as shape-type default
1 parent eb6ac83 commit 11606e9

18 files changed

+195
-180
lines changed

numpy/__init__.pyi

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,9 +1044,9 @@ class flatiter(Generic[_NdArraySubClass_co]):
10441044
@overload
10451045
def __array__(self: flatiter[ndarray[_FlatShapeType, Any]], dtype: _DType, /) -> ndarray[_FlatShapeType, _DType]: ...
10461046
@overload
1047-
def __array__(self: flatiter[ndarray[Any, _DType]], dtype: None = ..., /) -> ndarray[Any, _DType]: ...
1047+
def __array__(self: flatiter[ndarray[_Shape, _DType]], dtype: None = ..., /) -> ndarray[_Shape, _DType]: ...
10481048
@overload
1049-
def __array__(self, dtype: _DType, /) -> ndarray[Any, _DType]: ...
1049+
def __array__(self, dtype: _DType, /) -> ndarray[_Shape, _DType]: ...
10501050

10511051
_OrderKACF: TypeAlias = L[None, "K", "A", "C", "F"]
10521052
_OrderACF: TypeAlias = L[None, "A", "C", "F"]
@@ -1499,11 +1499,15 @@ _DType = TypeVar("_DType", bound=dtype[Any])
14991499
_DType_co = TypeVar("_DType_co", covariant=True, bound=dtype[Any])
15001500
_FlexDType = TypeVar("_FlexDType", bound=dtype[flexible])
15011501

1502-
_ShapeType_co = TypeVar("_ShapeType_co", covariant=True, bound=tuple[int, ...])
1503-
_ShapeType2 = TypeVar("_ShapeType2", bound=tuple[int, ...])
1504-
_Shape2DType_co = TypeVar("_Shape2DType_co", covariant=True, bound=tuple[int, int])
1502+
_Shape1D: TypeAlias = tuple[int]
1503+
_Shape2D: TypeAlias = tuple[int, int]
1504+
1505+
_ShapeType_co = TypeVar("_ShapeType_co", covariant=True, bound=_Shape)
1506+
_ShapeType2 = TypeVar("_ShapeType2", bound=_Shape)
1507+
_Shape2DType_co = TypeVar("_Shape2DType_co", covariant=True, bound=_Shape2D)
15051508
_NumberType = TypeVar("_NumberType", bound=number[Any])
15061509

1510+
15071511
if sys.version_info >= (3, 12):
15081512
from collections.abc import Buffer as _SupportsBuffer
15091513
else:
@@ -1629,7 +1633,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
16291633
NDArray[integer[Any]]
16301634
| NDArray[np.bool]
16311635
| tuple[NDArray[integer[Any]] | NDArray[np.bool], ...]
1632-
)) -> ndarray[Any, _DType_co]: ...
1636+
)) -> ndarray[_Shape, _DType_co]: ...
16331637
@overload
16341638
def __getitem__(self, key: SupportsIndex | tuple[SupportsIndex, ...]) -> Any: ...
16351639
@overload
@@ -1640,7 +1644,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
16401644
| SupportsIndex
16411645
| _ArrayLikeInt_co
16421646
| tuple[None | slice | EllipsisType | _ArrayLikeInt_co | SupportsIndex, ...]
1643-
)) -> ndarray[Any, _DType_co]: ...
1647+
)) -> ndarray[_Shape, _DType_co]: ...
16441648
@overload
16451649
def __getitem__(self: NDArray[void], key: str) -> NDArray[Any]: ...
16461650
@overload
@@ -1686,13 +1690,13 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
16861690
def squeeze(
16871691
self,
16881692
axis: None | SupportsIndex | tuple[SupportsIndex, ...] = ...,
1689-
) -> ndarray[Any, _DType_co]: ...
1693+
) -> ndarray[_Shape, _DType_co]: ...
16901694

16911695
def swapaxes(
16921696
self,
16931697
axis1: SupportsIndex,
16941698
axis2: SupportsIndex,
1695-
) -> ndarray[Any, _DType_co]: ...
1699+
) -> ndarray[_Shape, _DType_co]: ...
16961700

16971701
@overload
16981702
def transpose(self: _ArraySelf, axes: None | _ShapeLike, /) -> _ArraySelf: ...
@@ -1712,7 +1716,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
17121716
offset: SupportsIndex = ...,
17131717
axis1: SupportsIndex = ...,
17141718
axis2: SupportsIndex = ...,
1715-
) -> ndarray[Any, _DType_co]: ...
1719+
) -> ndarray[_Shape, _DType_co]: ...
17161720

17171721
# 1D + 1D returns a scalar;
17181722
# all other with at least 1 non-0D array return an ndarray.
@@ -1808,7 +1812,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
18081812
axis: None | SupportsIndex = ...,
18091813
out: None = ...,
18101814
mode: _ModeKind = ...,
1811-
) -> ndarray[Any, _DType_co]: ...
1815+
) -> ndarray[_Shape, _DType_co]: ...
18121816
@overload
18131817
def take(
18141818
self,
@@ -1822,19 +1826,19 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
18221826
self,
18231827
repeats: _ArrayLikeInt_co,
18241828
axis: None | SupportsIndex = ...,
1825-
) -> ndarray[Any, _DType_co]: ...
1829+
) -> ndarray[_Shape, _DType_co]: ...
18261830

18271831
# TODO: use `tuple[int]` as shape type once covariant (#26081)
18281832
def flatten(
18291833
self,
18301834
order: _OrderKACF = ...,
1831-
) -> ndarray[Any, _DType_co]: ...
1835+
) -> ndarray[_Shape, _DType_co]: ...
18321836

18331837
# TODO: use `tuple[int]` as shape type once covariant (#26081)
18341838
def ravel(
18351839
self,
18361840
order: _OrderKACF = ...,
1837-
) -> ndarray[Any, _DType_co]: ...
1841+
) -> ndarray[_Shape, _DType_co]: ...
18381842

18391843
@overload
18401844
def reshape(
@@ -1844,14 +1848,14 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
18441848
*,
18451849
order: _OrderACF = ...,
18461850
copy: None | bool = ...,
1847-
) -> ndarray[Any, _DType_co]: ...
1851+
) -> ndarray[_Shape, _DType_co]: ...
18481852
@overload
18491853
def reshape(
18501854
self,
18511855
*shape: SupportsIndex,
18521856
order: _OrderACF = ...,
18531857
copy: None | bool = ...,
1854-
) -> ndarray[Any, _DType_co]: ...
1858+
) -> ndarray[_Shape, _DType_co]: ...
18551859

18561860
@overload
18571861
def astype(
@@ -2737,7 +2741,7 @@ class generic(_ArrayOrScalarCommon):
27372741
@overload
27382742
def __array__(self: _ScalarType, dtype: None = ..., /) -> NDArray[_ScalarType]: ...
27392743
@overload
2740-
def __array__(self, dtype: _DType, /) -> ndarray[Any, _DType]: ...
2744+
def __array__(self, dtype: _DType, /) -> ndarray[_Shape, _DType]: ...
27412745
def __hash__(self) -> int: ...
27422746
@property
27432747
def base(self) -> None: ...
@@ -3886,7 +3890,7 @@ class poly1d:
38863890
@overload
38873891
def __array__(self, t: None = ..., copy: None | bool = ...) -> NDArray[Any]: ...
38883892
@overload
3889-
def __array__(self, t: _DType, copy: None | bool = ...) -> ndarray[Any, _DType]: ...
3893+
def __array__(self, t: _DType, copy: None | bool = ...) -> ndarray[_Shape, _DType]: ...
38903894

38913895
@overload
38923896
def __call__(self, val: _ScalarLike_co) -> Any: ...
@@ -3926,15 +3930,14 @@ class poly1d:
39263930
) -> poly1d: ...
39273931

39283932

3929-
39303933
class matrix(ndarray[_Shape2DType_co, _DType_co]):
39313934
__array_priority__: ClassVar[float]
39323935
def __new__(
39333936
subtype,
39343937
data: ArrayLike,
39353938
dtype: DTypeLike = ...,
39363939
copy: builtins.bool = ...,
3937-
) -> matrix[Any, Any]: ...
3940+
) -> matrix[_Shape2D, Any]: ...
39383941
def __array_finalize__(self, obj: object) -> None: ...
39393942

39403943
@overload
@@ -3959,122 +3962,122 @@ class matrix(ndarray[_Shape2DType_co, _DType_co]):
39593962
| tuple[None | slice | EllipsisType | _ArrayLikeInt_co | SupportsIndex, ...]
39603963
),
39613964
/,
3962-
) -> matrix[Any, _DType_co]: ...
3965+
) -> matrix[_Shape2D, _DType_co]: ...
39633966
@overload
3964-
def __getitem__(self: NDArray[void], key: str, /) -> matrix[Any, dtype[Any]]: ...
3967+
def __getitem__(self: NDArray[void], key: str, /) -> matrix[_Shape2D, dtype[Any]]: ...
39653968
@overload
39663969
def __getitem__(self: NDArray[void], key: list[str], /) -> matrix[_Shape2DType_co, dtype[void]]: ...
39673970

3968-
def __mul__(self, other: ArrayLike, /) -> matrix[Any, Any]: ...
3969-
def __rmul__(self, other: ArrayLike, /) -> matrix[Any, Any]: ...
3971+
def __mul__(self, other: ArrayLike, /) -> matrix[_Shape2D, Any]: ...
3972+
def __rmul__(self, other: ArrayLike, /) -> matrix[_Shape2D, Any]: ...
39703973
def __imul__(self, other: ArrayLike, /) -> matrix[_Shape2DType_co, _DType_co]: ...
3971-
def __pow__(self, other: ArrayLike, /) -> matrix[Any, Any]: ...
3974+
def __pow__(self, other: ArrayLike, /) -> matrix[_Shape2D, Any]: ...
39723975
def __ipow__(self, other: ArrayLike, /) -> matrix[_Shape2DType_co, _DType_co]: ...
39733976

39743977
@overload
39753978
def sum(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
39763979
@overload
3977-
def sum(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
3980+
def sum(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[_Shape2D, Any]: ...
39783981
@overload
39793982
def sum(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
39803983

39813984
@overload
39823985
def mean(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
39833986
@overload
3984-
def mean(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
3987+
def mean(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[_Shape2D, Any]: ...
39853988
@overload
39863989
def mean(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
39873990

39883991
@overload
39893992
def std(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> Any: ...
39903993
@overload
3991-
def std(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> matrix[Any, Any]: ...
3994+
def std(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> matrix[_Shape2D, Any]: ...
39923995
@overload
39933996
def std(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ..., ddof: float = ...) -> _NdArraySubClass: ...
39943997

39953998
@overload
39963999
def var(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> Any: ...
39974000
@overload
3998-
def var(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> matrix[Any, Any]: ...
4001+
def var(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> matrix[_Shape2D, Any]: ...
39994002
@overload
40004003
def var(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ..., ddof: float = ...) -> _NdArraySubClass: ...
40014004

40024005
@overload
40034006
def prod(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
40044007
@overload
4005-
def prod(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
4008+
def prod(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[_Shape2D, Any]: ...
40064009
@overload
40074010
def prod(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
40084011

40094012
@overload
40104013
def any(self, axis: None = ..., out: None = ...) -> np.bool: ...
40114014
@overload
4012-
def any(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[np.bool]]: ...
4015+
def any(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, dtype[np.bool]]: ...
40134016
@overload
40144017
def any(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
40154018

40164019
@overload
40174020
def all(self, axis: None = ..., out: None = ...) -> np.bool: ...
40184021
@overload
4019-
def all(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[np.bool]]: ...
4022+
def all(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, dtype[np.bool]]: ...
40204023
@overload
40214024
def all(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
40224025

40234026
@overload
40244027
def max(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ...
40254028
@overload
4026-
def max(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ...
4029+
def max(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, _DType_co]: ...
40274030
@overload
40284031
def max(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
40294032

40304033
@overload
40314034
def min(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ...
40324035
@overload
4033-
def min(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ...
4036+
def min(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, _DType_co]: ...
40344037
@overload
40354038
def min(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
40364039

40374040
@overload
40384041
def argmax(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> intp: ...
40394042
@overload
4040-
def argmax(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[intp]]: ...
4043+
def argmax(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, dtype[intp]]: ...
40414044
@overload
40424045
def argmax(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
40434046

40444047
@overload
40454048
def argmin(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> intp: ...
40464049
@overload
4047-
def argmin(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[intp]]: ...
4050+
def argmin(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, dtype[intp]]: ...
40484051
@overload
40494052
def argmin(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
40504053

40514054
@overload
40524055
def ptp(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ...
40534056
@overload
4054-
def ptp(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ...
4057+
def ptp(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, _DType_co]: ...
40554058
@overload
40564059
def ptp(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
40574060

4058-
def squeeze(self, axis: None | _ShapeLike = ...) -> matrix[Any, _DType_co]: ...
4059-
def tolist(self: matrix[Any, dtype[_SupportsItem[_T]]]) -> list[list[_T]]: ... # type: ignore[typevar]
4060-
def ravel(self, order: _OrderKACF = ...) -> matrix[Any, _DType_co]: ...
4061-
def flatten(self, order: _OrderKACF = ...) -> matrix[Any, _DType_co]: ...
4061+
def squeeze(self, axis: None | _ShapeLike = ...) -> matrix[_Shape2D, _DType_co]: ...
4062+
def tolist(self: matrix[_Shape2D, dtype[_SupportsItem[_T]]]) -> list[list[_T]]: ... # type: ignore[typevar]
4063+
def ravel(self, order: _OrderKACF = ...) -> matrix[_Shape2D, _DType_co]: ...
4064+
def flatten(self, order: _OrderKACF = ...) -> matrix[_Shape2D, _DType_co]: ...
40624065

40634066
@property
4064-
def T(self) -> matrix[Any, _DType_co]: ...
4067+
def T(self) -> matrix[_Shape2D, _DType_co]: ...
40654068
@property
4066-
def I(self) -> matrix[Any, Any]: ...
4069+
def I(self) -> matrix[_Shape2D, Any]: ...
40674070
@property
40684071
def A(self) -> ndarray[_Shape2DType_co, _DType_co]: ...
40694072
@property
4070-
def A1(self) -> ndarray[Any, _DType_co]: ...
4073+
def A1(self) -> ndarray[_Shape, _DType_co]: ...
40714074
@property
4072-
def H(self) -> matrix[Any, _DType_co]: ...
4073-
def getT(self) -> matrix[Any, _DType_co]: ...
4074-
def getI(self) -> matrix[Any, Any]: ...
4075+
def H(self) -> matrix[_Shape2D, _DType_co]: ...
4076+
def getT(self) -> matrix[_Shape2D, _DType_co]: ...
4077+
def getI(self) -> matrix[_Shape2D, Any]: ...
40754078
def getA(self) -> ndarray[_Shape2DType_co, _DType_co]: ...
4076-
def getA1(self) -> ndarray[Any, _DType_co]: ...
4077-
def getH(self) -> matrix[Any, _DType_co]: ...
4079+
def getA1(self) -> ndarray[_Shape, _DType_co]: ...
4080+
def getH(self) -> matrix[_Shape2D, _DType_co]: ...
40784081

40794082
_CharType = TypeVar("_CharType", str_, bytes_)
40804083
_CharDType = TypeVar("_CharDType", dtype[str_], dtype[bytes_])

numpy/_core/defchararray.pyi

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from typing import (
33
overload,
44
TypeVar,
55
Any,
6-
SupportsIndex,
6+
SupportsIndex,
77
SupportsInt,
88
)
99

@@ -16,13 +16,14 @@ from numpy import (
1616
int_,
1717
object_,
1818
_OrderKACF,
19-
_ShapeType_co,
20-
_CharDType,
19+
_ShapeType_co,
20+
_CharDType,
2121
_SupportsBuffer,
2222
)
2323

2424
from numpy._typing import (
2525
NDArray,
26+
_Shape,
2627
_ShapeLike,
2728
_ArrayLikeStr_co as U_co,
2829
_ArrayLikeBytes_co as S_co,
@@ -33,7 +34,7 @@ from numpy._typing import (
3334
from numpy._core.multiarray import compare_chararrays as compare_chararrays
3435

3536
_SCT = TypeVar("_SCT", str_, bytes_)
36-
_CharArray = chararray[Any, dtype[_SCT]]
37+
_CharArray = chararray[_Shape, dtype[_SCT]]
3738

3839
class chararray(ndarray[_ShapeType_co, _CharDType]):
3940
@overload
@@ -46,7 +47,7 @@ class chararray(ndarray[_ShapeType_co, _CharDType]):
4647
offset: SupportsIndex = ...,
4748
strides: _ShapeLike = ...,
4849
order: _OrderKACF = ...,
49-
) -> chararray[Any, dtype[bytes_]]: ...
50+
) -> chararray[_Shape, dtype[bytes_]]: ...
5051
@overload
5152
def __new__(
5253
subtype,
@@ -57,12 +58,12 @@ class chararray(ndarray[_ShapeType_co, _CharDType]):
5758
offset: SupportsIndex = ...,
5859
strides: _ShapeLike = ...,
5960
order: _OrderKACF = ...,
60-
) -> chararray[Any, dtype[str_]]: ...
61+
) -> chararray[_Shape, dtype[str_]]: ...
6162

6263
def __array_finalize__(self, obj: object) -> None: ...
63-
def __mul__(self, other: i_co) -> chararray[Any, _CharDType]: ...
64-
def __rmul__(self, other: i_co) -> chararray[Any, _CharDType]: ...
65-
def __mod__(self, i: Any) -> chararray[Any, _CharDType]: ...
64+
def __mul__(self, other: i_co) -> chararray[_Shape, _CharDType]: ...
65+
def __rmul__(self, other: i_co) -> chararray[_Shape, _CharDType]: ...
66+
def __mod__(self, i: Any) -> chararray[_Shape, _CharDType]: ...
6667

6768
@overload
6869
def __eq__(
@@ -210,7 +211,7 @@ class chararray(ndarray[_ShapeType_co, _CharDType]):
210211
def expandtabs(
211212
self,
212213
tabsize: i_co = ...,
213-
) -> chararray[Any, _CharDType]: ...
214+
) -> chararray[_Shape, _CharDType]: ...
214215

215216
@overload
216217
def find(
@@ -435,7 +436,7 @@ class chararray(ndarray[_ShapeType_co, _CharDType]):
435436
deletechars: None | S_co = ...,
436437
) -> _CharArray[bytes_]: ...
437438

438-
def zfill(self, width: _ArrayLikeInt_co) -> chararray[Any, _CharDType]: ...
439+
def zfill(self, width: _ArrayLikeInt_co) -> chararray[_Shape, _CharDType]: ...
439440
def capitalize(self) -> chararray[_ShapeType_co, _CharDType]: ...
440441
def title(self) -> chararray[_ShapeType_co, _CharDType]: ...
441442
def swapcase(self) -> chararray[_ShapeType_co, _CharDType]: ...

0 commit comments

Comments
 (0)
0