8000 TYP: Stop using ``Any`` as shape-type default by jorenham · Pull Request #27211 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

TYP: Stop using Any as shape-type default #27211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 52 additions & 49 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1376,9 +1376,9 @@ class flatiter(Generic[_NdArraySubClass_co]):
@overload
def __array__(self: flatiter[ndarray[_FlatShapeType, Any]], dtype: _DType, /) -> ndarray[_FlatShapeType, _DType]: ...
@overload
def __array__(self: flatiter[ndarray[Any, _DType]], dtype: None = ..., /) -> ndarray[Any, _DType]: ...
def __array__(self: flatiter[ndarray[_Shape, _DType]], dtype: None = ..., /) -> ndarray[_Shape, _DType]: ...
@overload
def __array__(self, dtype: _DType, /) -> ndarray[Any, _DType]: ...
def __array__(self, dtype: _DType, /) -> ndarray[_Shape, _DType]: ...

_OrderKACF: TypeAlias = L[None, "K", "A", "C", "F"]
_OrderACF: TypeAlias = L[None, "A", "C", "F"]
Expand Down Expand Up @@ -1831,11 +1831,15 @@ _DType = TypeVar("_DType", bound=dtype[Any])
_DType_co = TypeVar("_DType_co", covariant=True, bound=dtype[Any])
_FlexDType = TypeVar("_FlexDType", bound=dtype[flexible])

_ShapeType_co = TypeVar("_ShapeType_co", covariant=True, bound=tuple[int, ...])
_ShapeType2 = TypeVar("_ShapeType2", bound=tuple[int, ...])
_Shape2DType_co = TypeVar("_Shape2DType_co", covariant=True, bound=tuple[int, int])
_Shape1D: TypeAlias = tuple[int]
_Shape2D: TypeAlias = tuple[int, int]

_ShapeType_co = TypeVar("_ShapeType_co", covariant=True, bound=_Shape)
_ShapeType2 = TypeVar("_ShapeType2", bound=_Shape)
_Shape2DType_co = TypeVar("_Shape2DType_co", covariant=True, bound=_Shape2D)
_NumberType = TypeVar("_NumberType", bound=number[Any])


if sys.version_info >= (3, 12):
from collections.abc import Buffer as _SupportsBuffer
else:
Expand Down Expand Up @@ -1961,7 +1965,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
NDArray[integer[Any]]
| NDArray[np.bool]
| tuple[NDArray[integer[Any]] | NDArray[np.bool], ...]
)) -> ndarray[Any, _DType_co]: ...
)) -> ndarray[_Shape, _DType_co]: ...
@overload
def __getitem__(self, key: SupportsIndex | tuple[SupportsIndex, ...]) -> Any: ...
@overload
10000 Expand All @@ -1972,7 +1976,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
| SupportsIndex
| _ArrayLikeInt_co
| tuple[None | slice | EllipsisType | _ArrayLikeInt_co | SupportsIndex, ...]
)) -> ndarray[Any, _DType_co]: ...
)) -> ndarray[_Shape, _DType_co]: ...
@overload
def __getitem__(self: NDArray[void], key: str) -> NDArray[Any]: ...
@overload
Expand Down Expand Up @@ -2018,13 +2022,13 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
def squeeze(
self,
axis: None | SupportsIndex | tuple[SupportsIndex, ...] = ...,
) -> ndarray[Any, _DType_co]: ...
) -> ndarray[_Shape, _DType_co]: ...

def swapaxes(
self,
axis1: SupportsIndex,
axis2: SupportsIndex,
) -> ndarray[Any, _DType_co]: ...
) -> ndarray[_Shape, _DType_co]: ...

@overload
def transpose(self: _ArraySelf, axes: None | _ShapeLike, /) -> _ArraySelf: ...
Expand All @@ -2044,7 +2048,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
offset: SupportsIndex = ...,
axis1: SupportsIndex = ...,
axis2: SupportsIndex = ...,
) -> ndarray[Any, _DType_co]: ...
) -> ndarray[_Shape, _DType_co]: ...

# 1D + 1D returns a scalar;
# all other with at least 1 non-0D array return an ndarray.
Expand Down Expand Up @@ -2140,7 +2144,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
axis: None | SupportsIndex = ...,
out: None = ...,
mode: _ModeKind = ...,
) -> ndarray[Any, _DType_co]: ...
) -> ndarray[_Shape, _DType_co]: ...
@overload
def take(
self,
Expand All @@ -2154,19 +2158,19 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
self,
repeats: _ArrayLikeInt_co,
axis: None | SupportsIndex = ...,
) -> ndarray[Any, _DType_co]: ...
) -> ndarray[_Shape, _DType_co]: ...

# TODO: use `tuple[int]` as shape type once covariant (#26081)
def flatten(
self,
order: _OrderKACF = ...,
) -> ndarray[Any, _DType_co]: ...
) -> ndarray[_Shape, _DType_co]: ...

# TODO: use `tuple[int]` as shape type once covariant (#26081)
def ravel(
self,
order: _OrderKACF = ...,
) -> ndarray[Any, _DType_co]: ...
) -> ndarray[_Shape, _DType_co]: ...

@overload
def reshape(
Expand All @@ -2176,14 +2180,14 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
*,
order: _OrderACF = ...,
copy: None | bool = ...,
) -> ndarray[Any, _DType_co]: ...
) -> ndarray[_Shape, _DType_co]: ...
@overload
def reshape(
self,
*shape: SupportsIndex,
order: _OrderACF = ...,
copy: None | bool = ...,
) -> ndarray[Any, _DType_co]: ...
) -> ndarray[_Shape, _DType_co]: ...

@overload
def astype(
Expand Down Expand Up @@ -3069,7 +3073,7 @@ class generic(_ArrayOrScalarCommon):
@overload
def __array__(self: _ScalarType, dtype: None = ..., /) -> NDArray[_ScalarType]: ...
@overload
def __array__(self, dtype: _DType, /) -> ndarray[Any, _DType]: ...
def __array__(self, dtype: _DType, /) -> ndarray[_Shape, _DType]: ...
def __hash__(self) -> int: ...
@property
def base(self) -> None: ...
Expand Down Expand Up @@ -4247,7 +4251,7 @@ class poly1d:
@overload
def __array__(self, t: None = ..., copy: None | bool = ...) -> NDArray[Any]: ...
@overload
def __array__(self, t: _DType, copy: None | bool = ...) -> ndarray[Any, _DType]: ...
def __array__(self, t: _DType, copy: None | bool = ...) -> ndarray[_Shape, _DType]: ...

@overload
def __call__(self, val: _ScalarLike_co) -> Any: ...
Expand Down Expand Up @@ -4287,15 +4291,14 @@ class poly1d:
) -> poly1d: ...



class matrix(ndarray[_Shape2DType_co, _DType_co]):
__array_priority__: ClassVar[float]
def __new__(
subtype,
data: ArrayLike,
dtype: DTypeLike = ...,
copy: builtins.bool = ...,
) -> matrix[Any, Any]: ...
) -> matrix[_Shape2D, Any]: ...
def __array_finalize__(self, obj: object) -> None: ...

@overload
Expand All @@ -4320,122 +4323,122 @@ class matrix(ndarray[_Shape2DType_co, _DType_co]):
| tuple[None | slice | EllipsisType | _ArrayLikeInt_co | SupportsIndex, ...]
),
/,
) -> matrix[Any, _DType_co]: ...
) -> matrix[_Shape2D, _DType_co]: ...
@overload
def __getitem__(self: NDArray[void], key: str, /) -> matrix[Any, dtype[Any]]: ...
def __getitem__(self: NDArray[void], key: str, /) -> matrix[_Shape2D, dtype[Any]]: ...
@overload
def __getitem__(self: NDArray[void], key: list[str], /) -> matrix[_Shape2DType_co, dtype[void]]: ...

def __mul__(self, other: ArrayLike, /) -> matrix[Any, Any]: ...
def __rmul__(self, other: ArrayLike, /) -> matrix[Any, Any]: ...
def __mul__(self, other: ArrayLike, /) -> matrix[_Shape2D, Any]: ...
def __rmul__(self, other: ArrayLike, /) -> matrix[_Shape2D, Any]: ...
def __imul__(self, other: ArrayLike, /) -> matrix[_Shape2DType_co, _DType_co]: ...
def __pow__(self, other: ArrayLike, /) -> matrix[Any, Any]: ...
def __pow__(self, other: ArrayLike, /) -> matrix[_Shape2D, Any]: ...
def __ipow__(self, other: ArrayLike, /) -> matrix[_Shape2DType_co, _DType_co]: ...

@overload
def sum(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
@overload
def sum(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
def sum(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[_Shape2D, Any]: ...
@overload
def sum(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...

@overload
def mean(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
@overload
def mean(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
def mean(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[_Shape2D, Any]: ...
@overload
def mean(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...

@overload
def std(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> Any: ...
@overload
def std(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> matrix[Any, Any]: ...
def std(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> matrix[_Shape2D, Any]: ...
@overload
def std(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ..., ddof: float = ...) -> _NdArraySubClass: ...

@overload
def var(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> Any: ...
@overload
def var(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> matrix[Any, Any]: ...
def var(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ..., ddof: float = ...) -> matrix[_Shape2D, Any]: ...
@overload
def var(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ..., ddof: float = ...) -> _NdArraySubClass: ...

@overload
def prod(self, axis: None = ..., dtype: DTypeLike = ..., out: None = ...) -> Any: ...
@overload
def prod(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[Any, Any]: ...
def prod(self, axis: _ShapeLike, dtype: DTypeLike = ..., out: None = ...) -> matrix[_Shape2D, Any]: ...
@overload
def prod(self, axis: None | _ShapeLike = ..., dtype: DTypeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...

@overload
def any(self, axis: None = ..., out: None = ...) -> np.bool: ...
@overload
def any(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[np.bool]]: ...
def any(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, dtype[np.bool]]: ...
@overload
def any(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...

@overload
def all(self, axis: None = ..., out: None = ...) -> np.bool: ...
@overload
def all(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[np.bool]]: ...
def all(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, dtype[np.bool]]: ...
@overload
def all(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...

@overload
def max(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ...
@overload
def max(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ...
def max(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, _DType_co]: ...
@overload
def max(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...

@overload
def min(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ...
@overload
def min(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ...
def min(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, _DType_co]: ...
@overload
def min(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...

@overload
def argmax(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> intp: ...
@overload
def argmax(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[intp]]: ...
def argmax(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, dtype[intp]]: ...
@overload
def argmax(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...

@overload
def argmin(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> intp: ...
@overload
def argmin(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, dtype[intp]]: ...
def argmin(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, dtype[intp]]: ...
@overload
def argmin(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...

@overload
def ptp(self: NDArray[_ScalarType], axis: None = ..., out: None = ...) -> _ScalarType: ...
@overload
def ptp(self, axis: _ShapeLike, out: None = ...) -> matrix[Any, _DType_co]: ...
def ptp(self, axis: _ShapeLike, out: None = ...) -> matrix[_Shape2D, _DType_co]: ...
@overload
def ptp(self, axis: None | _ShapeLike = ..., out: _NdArraySubClass = ...) -> _NdArraySubClass: ...

def squeeze(self, axis: None | _ShapeLike = ...) -> matrix[Any, _DType_co]: ...
def tolist(self: matrix[Any, dtype[_SupportsItem[_T]]]) -> list[list[_T]]: ... # type: ignore[typevar]
def ravel(self, order: _OrderKACF = ...) -> matrix[Any, _DType_co]: ...
def flatten(self, order: _OrderKACF = ...) -> matrix[Any, _DType_co]: ...
def squeeze(self, axis: None | _ShapeLike = ...) -> matrix[_Shape2D, _DType_co]: ...
def tolist(self: matrix[_Shape2D, dtype[_SupportsItem[_T]]]) -> list[list[_T]]: ... # type: ignore[typevar]
def ravel(self, order: _OrderKACF = ...) -> matrix[_Shape2D, _DType_co]: ...
def flatten(self, order: _OrderKACF = ...) -> matrix[_Shape2D, _DType_co]: ...

@property
def T(self) -> matrix[Any, _DType_co]: ...
def T(self) -> matrix[_Shape2D, _DType_co]: ...
@property
def I(self) -> matrix[Any, Any]: ...
def I(self) -> matrix[_Shape2D, Any]: ...
@property
def A(self) -> ndarray[_Shape2DType_co, _DType_co]: ...
@property
def A1(self) -> ndarray[Any, _DType_co]: ...
def A1(self) -> ndarray[_Shape, _DType_co]: ...
@property
def H(self) -> matrix[Any, _DType_co]: ...
def getT(self) -> matrix[Any, _DType_co]: ...
def getI(self) -> matrix[Any, Any]: ...
def H(self) -> matrix[_Shape2D, _DType_co]: ...
def getT(self) -> matrix[_Shape2D, _DType_co]: ...
def getI(self) -> matrix[_Shape2D, Any]: ...
def getA(self) -> ndarray[_Shape2DType_co, _DType_co]: ...
def getA1(self) -> ndarray[Any, _DType_co]: ...
def getH(self) -> matrix[Any, _DType_co]: ...
def getA1(self) -> ndarray[_Shape, _DType_co]: ...
def getH(self) -> matrix[_Shape2D, _DType_co]: ...

_CharType = TypeVar("_CharType", str_, bytes_)
_CharDType = TypeVar("_CharDType", dtype[str_], dtype[bytes_])
Expand Down
17 changes: 9 additions & 8 deletions numpy/_core/defchararray.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ from numpy import (

from numpy._typing import (
NDArray,
_Shape,
_ShapeLike,
_ArrayLikeStr_co as U_co,
_ArrayLikeBytes_co as S_co,
Expand All @@ -33,7 +34,7 @@ from numpy._typing import (
from numpy._core.multiarray import compare_chararrays as compare_chararrays

_SCT = TypeVar("_SCT", str_, bytes_)
_CharArray = chararray[Any, dtype[_SCT]]
_CharArray = chararray[_Shape, dtype[_SCT]]

class chararray(ndarray[_ShapeType_co, _CharDType]):
@overload
Expand All @@ -46,7 +47,7 @@ class chararray(ndarray[_ShapeType_co, _CharDType]):
offset: SupportsIndex = ...,
strides: _ShapeLike = ...,
order: _OrderKACF = ...,
) -> chararray[Any, dtype[bytes_]]: ...
) -> chararray[_Shape, dtype[bytes_]]: ...
@overload
def __new__(
subtype,
Expand All @@ -57,12 +58,12 @@ class chararray(ndarray[_ShapeType_co, _CharDType]):
offset: SupportsIndex = ...,
strides: _ShapeLike = ...,
order: _OrderKACF = ...,
) -> chararray[Any, dtype[str_]]: ...
) -> chararray[_Shape, dtype[str_]]: ...

def __array_finalize__(self, obj: object) -> None: ...
def __mul__(self, other: i_co) -> chararray[Any, _CharDType]: ...
def __rmul__(self, other: i_co) -> chararray[Any, _CharDType]: ...
def __mod__(self, i: Any) -> chararray[Any, _CharDType]: ...
def __mul__(self, other: i_co) -> chararray[_Shape, _CharDType]: ...
def __rmul__(self, other: i_co) -> chararray[_Shape, _CharDType]: ...
def __mod__(self, i: Any) -> chararray[_Shape, _CharDType]: ...

@overload
def __eq__(
Expand Down Expand Up @@ -210,7 +211,7 @@ class chararray(ndarray[_ShapeType_co, _CharDType]):
def expandtabs(
self,
tabsize: i_co = ...,
) -> chararray[Any, _CharDType]: ...
) -> chararray[_Shape, _CharDType]: ...

@overload
def find(
Expand Down Expand Up @@ -435,7 +436,7 @@ class chararray(ndarray[_ShapeType_co, _CharDType]):
deletechars: None | S_co = ...,
) -> _CharArray[bytes_]: ...

def zfill(self, width: _ArrayLikeInt_co) -> chararray[Any, _CharDType]: ...
def zfill(self, width: _ArrayLikeInt_co) -> chararray[_Shape, _CharDType]: ...
def capitalize(self) -> chararray[_ShapeType_co, _CharDType]: ...
def title(self) -> chararray[_ShapeType_co, _CharDType]: ...
def swapcase(self) -> chararray[_ShapeType_co, _CharDType]: ...
Expand Down
5 changes: 3 additions & 2 deletions numpy/_core/records.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ from numpy._typing import (
ArrayLike,
DTypeLike,
NDArray,
_Shape,
_ShapeLike,
_ArrayLikeInt_co,
_ArrayLikeVoid_co,
Expand Down Expand Up @@ -102,7 +103,7 @@ class recarray(ndarray[_ShapeType_co, _DType_co]):
| SupportsIndex
| _ArrayLikeInt_co
| tuple[None | slice | EllipsisType | _ArrayLikeInt_co | SupportsIndex, ...]
)) -> recarray[Any, _DType_co]: ...
)) -> recarray[_Shape, _DType_co]: ...
@overload
def __getitem__(self, indx: (
None
Expand All @@ -111,7 +112,7 @@ class recarray(ndarray[_ShapeType_co, _DType_co]):
| SupportsIndex
| _ArrayLikeInt_co
| tuple[None | slice | EllipsisType | _ArrayLikeInt_co | SupportsIndex, ...]
)) -> ndarray[Any, _DType_co]: ...
)) -> ndarray[_Shape, _DType_co]: ...
@overload
def __getitem__(self, indx: str) -> NDArray[Any]: ...
@overload
Expand Down
Loading
0