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

Skip to content

Commit b93a529

Browse files
committed
TYP: Stop using Any as shape-type default
1 parent 8cba529 commit b93a529

18 files changed

+195
-180
lines changed

numpy/__init__.pyi

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,9 +1045,9 @@ class flatiter(Generic[_NdArraySubClass_co]):
10451045
@overload
10461046
def __array__(self: flatiter[ndarray[_FlatShapeType, Any]], dtype: _DType, /) -> ndarray[_FlatShapeType, _DType]: ...
10471047
@overload
1048-
def __array__(self: flatiter[ndarray[Any, _DType]], dtype: None = ..., /) -> ndarray[Any, _DType]: ...
1048+
def __array__(self: flatiter[ndarray[_Shape, _DType]], dtype: None = ..., /) -> ndarray[_Shape, _DType]: ...
10491049
@overload
1050-
def __array__(self, dtype: _DType, /) -> ndarray[Any, _DType]: ...
1050+
def __array__(self, dtype: _DType, /) -> ndarray[_Shape, _DType]: ...
10511051

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

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

1511+
15081512
if sys.version_info >= (3, 12):
15091513
from collections.abc import Buffer as _SupportsBuffer
15101514
else:
@@ -1630,7 +1634,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
16301634
NDArray[integer[Any]]
16311635
| NDArray[np.bool]
16321636
| tuple[NDArray[integer[Any]] | NDArray[np.bool], ...]
1633-
)) -> ndarray[Any, _DType_co]: ...
1637+
)) -> ndarray[_Shape, _DType_co]: ...
16341638
@overload
16351639
def __getitem__(self, key: SupportsIndex | tuple[SupportsIndex, ...]) -> Any: ...
16361640
@overload
@@ -1641,7 +1645,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
16411645
| SupportsIndex
16421646
| _ArrayLikeInt_co
16431647
| tuple[None | slice | EllipsisType | _ArrayLikeInt_co | SupportsIndex, ...]
1644-
)) -> ndarray[Any, _DType_co]: ...
1648+
)) -> ndarray[_Shape, _DType_co]: ...
16451649
@overload
16461650
def __getitem__(self: NDArray[void], key: str) -> NDArray[Any]: ...
16471651
@overload
@@ -1687,13 +1691,13 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
16871691
def squeeze(
16881692
self,
16891693
axis: None | SupportsIndex | tuple[SupportsIndex, ...] = ...,
1690-
) -> ndarray[Any, _DType_co]: ...
1694+
) -> ndarray[_Shape, _DType_co]: ...
16911695

16921696
def swapaxes(
16931697
self,
16941698
axis1: SupportsIndex,
16951699
axis2: SupportsIndex,
1696-
) -> ndarray[Any, _DType_co]: ...
1700+
) -> ndarray[_Shape, _DType_co]: ...
16971701

16981702
@overload
16991703
def transpose(self: _ArraySelf, axes: None | _ShapeLike, /) -> _ArraySelf: ...
@@ -1713,7 +1717,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
17131717
offset: SupportsIndex = ...,
17141718
axis1: SupportsIndex = ...,
17151719
axis2: SupportsIndex = ...,
1716-
) -> ndarray[Any, _DType_co]: ...
1720+
) -> ndarray[_Shape, _DType_co]: ...
17171721

17181722
# 1D + 1D returns a scalar;
17191723
# all other with at least 1 non-0D array return an ndarray.
@@ -1809,7 +1813,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
18091813
axis: None | SupportsIndex = ...,
18101814
out: None = ...,
18111815
mode: _ModeKind = ...,
1812-
) -> ndarray[Any, _DType_co]: ...
1816+
) -> ndarray[_Shape, _DType_co]: ...
18131817
@overload
18141818
def take(
18151819
self,
@@ -1823,19 +1827,19 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
18231827
self,
18241828
repeats: _ArrayLikeInt_co,
18251829
axis: None | SupportsIndex = ...,
1826-
) -> ndarray[Any, _DType_co]: ...
1830+
) -> ndarray[_Shape, _DType_co]: ...
18271831

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

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

18401844
@overload
18411845
def reshape(
@@ -1845,14 +1849,14 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
18451849
*,
18461850
order: _OrderACF = ...,
18471851
copy: None | bool = ...,
1848-
) -> ndarray[Any, _DType_co]: ...
1852+
) -> ndarray[_Shape, _DType_co]: ...
18491853
@overload
18501854
def reshape(
18511855
self,
18521856
*shape: SupportsIndex,
18531857
order: _OrderACF = ...,
18541858
copy: None | bool = ...,
1855-
) -> ndarray[Any, _DType_co]: ...
1859+
) -> ndarray[_Shape, _DType_co]: ...
18561860

18571861
@overload
18581862
def astype(
@@ -2738,7 +2742,7 @@ class generic(_ArrayOrScalarCommon):
27382742
@overload
27392743
def __array__(self: _ScalarType, dtype: None = ..., /) -> NDArray[_ScalarType]: ...
27402744
@overload
2741-
def __array__(self, dtype: _DType, /) -> ndarray[Any, _DType]: ...
2745+
def __array__(self, dtype: _DType, /) -> ndarray[_Shape, _DType]: ...
27422746
def __hash__(self) -> int: ...
27432747
@property
27442748
def base(self) -> None: ...
@@ -3887,7 +3891,7 @@ class poly1d:
38873891
@overload
38883892
def __array__(self, t: None = ..., copy: None | bool = ...) -> NDArray[Any]: ...
38893893
@overload
3890-
def __array__(self, t: _DType, copy: None | bool = ...) -> ndarray[Any, _DType]: ...
3894+
def __array__(self, t: _DType, copy: None | bool = ...) -> ndarray[_Shape, _DType]: ...
38913895

38923896
@overload
38933897
def __call__(self, val: _ScalarLike_co) -> Any: ...
@@ -3927,15 +3931,14 @@ class poly1d:
39273931
) -> poly1d: ...
39283932

39293933

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

39413944
@overload
@@ -3960,122 +3963,122 @@ class matrix(ndarray[_Shape2DType_co, _DType_co]):
39603963
| tuple[None | slice | EllipsisType | _ArrayLikeInt_co | SupportsIndex, ...]
39613964
),
39623965
/,
3963-
) -> matrix[Any, _DType_co]: ...
3966+
) -> matrix[_Shape2D, _DType_co]: ...
39643967
@overload
3965-
def __getitem__(self: NDArray[void], key: str, /) -> matrix[Any, dtype[Any]]: ...
3968+
def __getitem__(self: NDArray[void], key: str, /) -> matrix[_Shape2D, dtype[Any]]: ...
39663969
@overload
39673970
def __getitem__(self: NDArray[void], key: list[str], /) -> matrix[_Shape2DType_co, dtype[void]]: ...
39683971

3969-
def __mul__(self, other: ArrayLike, /) -> matrix[Any, Any]: ...
3970-
def __rmul__(self, other: ArrayLike, /) -> matrix[Any, Any]: ...
3972+
def __mul__(self, other: ArrayLike, /) -> matrix[_Shape2D, Any]: ...
3973+
def __rmul__(self, other: ArrayLike, /) -> matrix[_Shape2D, Any]: ...
39713974
def __imul__(self, other: ArrayLike, /) -> matrix[_Shape2DType_co, _DType_co]: ...
3972-
def __pow__(self, other: ArrayLike, /) -> matrix[Any, Any]: ...
3975+
def __pow__(self, other: ArrayLike, /) -> matrix[_Shape2D, Any]: ...
39733976
def __ipow__(self, other: ArrayLike, /) -> matrix[_Shape2DType_co, _DType_co]: ...
39743977

39753978
@overload
39763979
def sum(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
39773980
@overload
3978-
def sum(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
3981+
def sum(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[_Shape2D, Any]: ...
39793982
@overload
39803983
def sum(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
39813984

39823985
@overload
39833986
def mean(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
39843987
@overload
3985-
def mean(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
3988+
def mean(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[_Shape2D, Any]: ...
39863989
@overload
39873990
def mean(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
39883991

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

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

40034006
@overload
40044007
def prod(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
40054008
@overload
4006-
def prod(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
4009+
def prod(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[_Shape2D, Any]: ...
40074010
@overload
40084011
def prod(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
40094012

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

40174020
@overload
4018< D96B code>4021
def all(self, axis: None = ..., out: None = ...) -> np.bool: ...
40194022
@overload
4020-
def all(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[np.bool]]: ...
4023+
def all(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, dtype[np.bool]]: ...
40214024
@overload
40224025
def all(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...
40234026

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

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

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

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

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

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

40644067
@property
4065-
def T(self) -> matrix[Any, _DType_co]: ...
4068+
def T(self) -> matrix[_Shape2D, _DType_co]: ...
40664069
@property
4067-
def I(self) -> matrix[Any, Any]: ...
4070+
def I(self) -> matrix[_Shape2D, Any]: ...
40684071
@property
40694072
def A(self) -> ndarray[_Shape2DType_co, _DType_co]: ...
40704073
@property
4071-
def A1(self) -> ndarray[Any, _DType_co]: ...
4074+
def A1(self) -> ndarray[_Shape, _DType_co]: ...
40724075
@property
4073-
def H(self) -> matrix[Any, _DType_co]: ...
4074-
def getT(self) -> matrix[Any, _DType_co]: ...
4075-
def getI(self) -> matrix[Any, Any]: ...
4076+
def H(self) -> matrix[_Shape2D, _DType_co]: ...
4077+
def getT(self) -> matrix[_Shape2D, _DType_co]: ...
4078+
def getI(self) -> matrix[_Shape2D, Any]: ...
40764079
def getA(self) -> ndarray[_Shape2DType_co, _DType_co]: ...
4077-
def getA1(self) -> ndarray[Any, _DType_co]: ...
4078-
def getH(self) -> matrix[Any, _DType_co]: ...
4080+
def getA1(self) -> ndarray[_Shape, _DType_co]: ...
4081+
def getH(self) -> matrix[_Shape2D, _DType_co]: ...
40794082

40804083
_CharType = TypeVar("_CharType", str_, bytes_)
40814084
_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