Skip to content

TYP: Gradual shape type defaults #28982

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 6 commits into from
May 19, 2025
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
37 changes: 20 additions & 17 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ from numpy._typing import (
_DTypeLikeVoid,
_VoidDTypeLike,
# Shapes
_AnyShape,
_Shape,
_ShapeLike,
# Scalars
Expand Down Expand Up @@ -794,7 +795,7 @@ _RealArrayT = TypeVar("_RealArrayT", bound=NDArray[floating | integer | timedelt
_NumericArrayT = TypeVar("_NumericArrayT", bound=NDArray[number | timedelta64 | object_])

_ShapeT = TypeVar("_ShapeT", bound=_Shape)
_ShapeT_co = TypeVar("_ShapeT_co", bound=_Shape, default=_Shape, covariant=True)
_ShapeT_co = TypeVar("_ShapeT_co", bound=_Shape, default=_AnyShape, covariant=True)
_1DShapeT = TypeVar("_1DShapeT", bound=_1D)
_2DShapeT_co = TypeVar("_2DShapeT_co", bound=_2D, default=_2D, covariant=True)
_1NShapeT = TypeVar("_1NShapeT", bound=tuple[L[1], *tuple[L[1], ...]]) # (1,) | (1, 1) | (1, 1, 1) | ...
Expand Down Expand Up @@ -1579,11 +1580,11 @@ class dtype(Generic[_ScalarT_co], metaclass=_DTypeMeta):
@property
def num(self) -> _DTypeNum: ...
@property
def shape(self) -> tuple[()] | _Shape: ...
def shape(self) -> _AnyShape: ...
@property
def ndim(self) -> int: ...
@property
def subdtype(self) -> tuple[dtype, _Shape] | None: ...
def subdtype(self) -> tuple[dtype, _AnyShape] | None: ...
def newbyteorder(self, new_order: _ByteOrder = ..., /) -> Self: ...
@property
def str(self) -> LiteralString: ...
Expand Down Expand Up @@ -1627,9 +1628,9 @@ class flatiter(Generic[_ArrayT_co]):
@overload
def __array__(self: flatiter[ndarray[_1DShapeT, Any]], dtype: _DTypeT, /) -> ndarray[_1DShapeT, _DTypeT]: ...
@overload
def __array__(self: flatiter[ndarray[_Shape, _DTypeT]], dtype: None = ..., /) -> ndarray[_Shape, _DTypeT]: ...
def __array__(self: flatiter[ndarray[Any, _DTypeT]], dtype: None = ..., /) -> ndarray[_AnyShape, _DTypeT]: ...
@overload
def __array__(self, dtype: _DTypeT, /) -> ndarray[_Shape, _DTypeT]: ...
def __array__(self, dtype: _DTypeT, /) -> ndarray[_AnyShape, _DTypeT]: ...

@type_check_only
class _ArrayOrScalarCommon:
Expand Down Expand Up @@ -2084,11 +2085,11 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
) -> ndarray[_ShapeT, _DTypeT]: ...

@overload
def __getitem__(self, key: _ArrayInt_co | tuple[_ArrayInt_co, ...], /) -> ndarray[_Shape, _DTypeT_co]: ...
def __getitem__(self, key: _ArrayInt_co | tuple[_ArrayInt_co, ...], /) -> ndarray[_AnyShape, _DTypeT_co]: ...
@overload
def __getitem__(self, key: SupportsIndex | tuple[SupportsIndex, ...], /) -> Any: ...
@overload
def __getitem__(self, key: _ToIndices, /) -> ndarray[_Shape, _DTypeT_co]: ...
def __getitem__(self, key: _ToIndices, /) -> ndarray[_AnyShape, _DTypeT_co]: ...
@overload
def __getitem__(self: NDArray[void], key: str, /) -> ndarray[_ShapeT_co, np.dtype]: ...
@overload
Expand Down Expand Up @@ -2166,6 +2167,8 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
*args: SupportsIndex,
) -> str: ...

@overload # this first overload prevents mypy from over-eagerly selecting `tuple[()]` in case of `_AnyShape`
def tolist(self: ndarray[tuple[Never], dtype[generic[_T]]], /) -> Any: ...
@overload
def tolist(self: ndarray[tuple[()], dtype[generic[_T]]], /) -> _T: ...
@overload
Expand All @@ -2187,13 +2190,13 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
def squeeze(
self,
axis: SupportsIndex | tuple[SupportsIndex, ...] | None = ...,
) -> ndarray[_Shape, _DTypeT_co]: ...
) -> ndarray[_AnyShape, _DTypeT_co]: ...

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

@overload
def transpose(self, axes: _ShapeLike | None, /) -> Self: ...
Expand Down Expand Up @@ -2320,7 +2323,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
offset: SupportsIndex = ...,
axis1: SupportsIndex = ...,
axis2: SupportsIndex = ...,
) -> ndarray[_Shape, _DTypeT_co]: ...
) -> ndarray[_AnyShape, _DTypeT_co]: ...

# 1D + 1D returns a scalar;
# all other with at least 1 non-0D array return an ndarray.
Expand Down Expand Up @@ -2396,7 +2399,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
axis: SupportsIndex | None = ...,
out: None = ...,
mode: _ModeKind = ...,
) -> ndarray[_Shape, _DTypeT_co]: ...
) -> ndarray[_AnyShape, _DTypeT_co]: ...
@overload
def take(
self,
Expand All @@ -2417,7 +2420,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
self,
repeats: _ArrayLikeInt_co,
axis: SupportsIndex,
) -> ndarray[_Shape, _DTypeT_co]: ...
) -> ndarray[_AnyShape, _DTypeT_co]: ...

def flatten(self, /, order: _OrderKACF = "C") -> ndarray[tuple[int], _DTypeT_co]: ...
def ravel(self, /, order: _OrderKACF = "C") -> ndarray[tuple[int], _DTypeT_co]: ...
Expand Down Expand Up @@ -2493,7 +2496,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
*shape: SupportsIndex,
order: _OrderACF = "C",
copy: builtins.bool | None = None,
) -> ndarray[_Shape, _DTypeT_co]: ...
) -> ndarray[_AnyShape, _DTypeT_co]: ...
@overload # (sequence[index])
def reshape(
self,
Expand All @@ -2502,7 +2505,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
*,
order: _OrderACF = "C",
copy: builtins.bool | None = None,
) -> ndarray[_Shape, _DTypeT_co]: ...
) -> ndarray[_AnyShape, _DTypeT_co]: ...

@overload
def astype(
Expand Down Expand Up @@ -4941,7 +4944,7 @@ class broadcast:
@property
def numiter(self) -> int: ...
@property
def shape(self) -> _Shape: ...
def shape(self) -> _AnyShape: ...
@property
def size(self) -> int: ...
def __next__(self) -> tuple[Any, ...]: ...
Expand Down Expand Up @@ -5398,8 +5401,8 @@ class matrix(ndarray[_2DShapeT_co, _DTypeT_co]):
def A(self) -> ndarray[_2DShapeT_co, _DTypeT_co]: ...
def getA(self) -> ndarray[_2DShapeT_co, _DTypeT_co]: ...
@property
def A1(self) -> ndarray[_Shape, _DTypeT_co]: ...
def getA1(self) -> ndarray[_Shape, _DTypeT_co]: ...
def A1(self) -> ndarray[_AnyShape, _DTypeT_co]: ...
def getA1(self) -> ndarray[_AnyShape, _DTypeT_co]: ...
@property
def H(self) -> matrix[_2D, _DTypeT_co]: ...
def getH(self) -> matrix[_2D, _DTypeT_co]: ...
Expand Down
76 changes: 26 additions & 50 deletions numpy/_core/defchararray.pyi
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
from typing import (
Any,
SupportsIndex,
SupportsInt,
TypeAlias,
overload,
)
from typing import (
Literal as L,
)
from typing import Any, Self, SupportsIndex, SupportsInt, TypeAlias, overload
from typing import Literal as L

from typing_extensions import TypeVar

import numpy as np
from numpy import (
_OrderKACF,
_SupportsArray,
_SupportsBuffer,
bytes_,
dtype,
Expand All @@ -24,29 +15,13 @@ from numpy import (
str_,
)
from numpy._core.multiarray import compare_chararrays
from numpy._typing import (
NDArray,
_Shape,
_ShapeLike,
)
from numpy._typing import (
_ArrayLikeAnyString_co as UST_co,
)
from numpy._typing import (
_ArrayLikeBool_co as b_co,
)
from numpy._typing import (
_ArrayLikeBytes_co as S_co,
)
from numpy._typing import (
_ArrayLikeInt_co as i_co,
)
from numpy._typing import (
_ArrayLikeStr_co as U_co,
)
from numpy._typing import (
_ArrayLikeString_co as T_co,
)
from numpy._typing import NDArray, _AnyShape, _Shape, _ShapeLike, _SupportsArray
from numpy._typing import _ArrayLikeAnyString_co as UST_co
from numpy._typing import _ArrayLikeBool_co as b_co
from numpy._typing import _ArrayLikeBytes_co as S_co
from numpy._typing import _ArrayLikeInt_co as i_co
from numpy._typing import _ArrayLikeStr_co as U_co
from numpy._typing import _ArrayLikeString_co as T_co

__all__ = [
"equal",
Expand Down Expand Up @@ -104,14 +79,15 @@ __all__ = [
"chararray",
]

_ShapeT_co = TypeVar("_ShapeT_co", bound=_Shape, default=_Shape, covariant=True)
_ShapeT_co = TypeVar("_ShapeT_co", bound=_Shape, default=_AnyShape, covariant=True)
_CharacterT = TypeVar("_CharacterT", bound=np.character)
_CharDTypeT_co = TypeVar("_CharDTypeT_co", bound=dtype[np.character], default=dtype, covariant=True)
_CharArray: TypeAlias = chararray[_Shape, dtype[_CharacterT]]

_StringDTypeArray: TypeAlias = np.ndarray[_Shape, np.dtypes.StringDType]
_CharArray: TypeAlias = chararray[_AnyShape, dtype[_CharacterT]]

_StringDTypeArray: TypeAlias = np.ndarray[_AnyShape, np.dtypes.StringDType]
_StringDTypeOrUnicodeArray: TypeAlias = _StringDTypeArray | NDArray[np.str_]
_StringDTypeSupportsArray: TypeAlias = _SupportsArray[np.dtypes.StringDType]
_StringDTypeOrUnicodeArray: TypeAlias = np.ndarray[_Shape, np.dtype[np.str_]] | np.ndarray[_Shape, np.dtypes.StringDType]

class chararray(ndarray[_ShapeT_co, _CharDTypeT_co]):
@overload
Expand All @@ -124,7 +100,7 @@ class chararray(ndarray[_ShapeT_co, _CharDTypeT_co]):
offset: SupportsIndex = ...,
strides: _ShapeLike = ...,
order: _OrderKACF = ...,
) -> chararray[_Shape, dtype[bytes_]]: ...
) -> _CharArray[bytes_]: ...
@overload
def __new__(
subtype,
Expand All @@ -135,12 +111,12 @@ class chararray(ndarray[_ShapeT_co, _CharDTypeT_co]):
offset: SupportsIndex = ...,
strides: _ShapeLike = ...,
order: _OrderKACF = ...,
) -> chararray[_Shape, dtype[str_]]: ...
) -> _CharArray[str_]: ...

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

@overload
def __eq__(
Expand Down Expand Up @@ -288,7 +264,7 @@ class chararray(ndarray[_ShapeT_co, _CharDTypeT_co]):
def expandtabs(
self,
tabsize: i_co = ...,
) -> chararray[_Shape, _CharDTypeT_co]: ...
) -> Self: ...

@overload
def find(
Expand Down Expand Up @@ -513,12 +489,12 @@ class chararray(ndarray[_ShapeT_co, _CharDTypeT_co]):
deletechars: S_co | None = ...,
) -> _CharArray[bytes_]: ...

def zfill(self, width: i_co) -> chararray[_Shape, _CharDTypeT_co]: ...
def capitalize(self) -> chararray[_ShapeT_co, _CharDTypeT_co]: ...
def title(self) -> chararray[_ShapeT_co, _CharDTypeT_co]: ...
def swapcase(self) -> chararray[_ShapeT_co, _CharDTypeT_co]: ...
def lower(self) -> chararray[_ShapeT_co, _CharDTypeT_co]: ...
def upper(self) -> chararray[_ShapeT_co, _CharDTypeT_co]: ...
def zfill(self, width: i_co) -> Self: ...
def capitalize(self) -> Self: ...
def title(self) -> Self: ...
def swapcase(self) -> Self: ...
def lower(self) -> Self: ...
def upper(self) -> Self: ...
def isalnum(self) -> ndarray[_ShapeT_co, dtype[np.bool]]: ...
def isalpha(self) -> ndarray[_ShapeT_co, dtype[np.bool]]: ...
def isdigit(self) -> ndarray[_ShapeT_co, dtype[np.bool]]: ...
Expand Down
5 changes: 3 additions & 2 deletions numpy/_core/fromnumeric.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ from numpy._typing import (
ArrayLike,
DTypeLike,
NDArray,
_AnyShape,
_ArrayLike,
_ArrayLikeBool_co,
_ArrayLikeComplex_co,
Expand Down Expand Up @@ -579,7 +580,7 @@ def nonzero(a: _ArrayLike[Any]) -> tuple[NDArray[intp], ...]: ...

# this prevents `Any` from being returned with Pyright
@overload
def shape(a: _SupportsShape[Never]) -> tuple[int, ...]: ...
def shape(a: _SupportsShape[Never]) -> _AnyShape: ...
@overload
def shape(a: _SupportsShape[_ShapeT]) -> _ShapeT: ...
@overload
Expand All @@ -594,7 +595,7 @@ def shape(a: _PyArray[_PyArray[_PyScalar]]) -> tuple[int, int]: ...
@overload
def shape(a: memoryview | bytearray) -> tuple[int]: ...
@overload
def shape(a: ArrayLike) -> tuple[int, ...]: ...
def shape(a: ArrayLike) -> _AnyShape: ...

@overload
def compress(
Expand Down
3 changes: 2 additions & 1 deletion numpy/_core/multiarray.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ from numpy._typing import (
_NestedSequence,
_ScalarLike_co,
# Shapes
_Shape,
_ShapeLike,
_SupportsArrayFunc,
_SupportsDType,
Expand Down Expand Up @@ -207,7 +208,7 @@ _IDType = TypeVar("_IDType")
_Nin = TypeVar("_Nin", bound=int)
_Nout = TypeVar("_Nout", bound=int)

_ShapeT = TypeVar("_ShapeT", bound=tuple[int, ...])
_ShapeT = TypeVar("_ShapeT", bound=_Shape)
_Array: TypeAlias = ndarray[_ShapeT, dtype[_ScalarT]]
_Array1D: TypeAlias = ndarray[tuple[int], dtype[_ScalarT]]

Expand Down
33 changes: 16 additions & 17 deletions numpy/_core/numeric.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import numpy as np
from numpy import (
False_,
True_,
_AnyShapeT,
_OrderCF,
_OrderKACF,
# re-exports
Expand Down Expand Up @@ -63,26 +62,17 @@ from numpy._typing import (
_DTypeLike,
_NestedSequence,
_ScalarLike_co,
_Shape,
_ShapeLike,
_SupportsArrayFunc,
_SupportsDType,
)

from .fromnumeric import (
all as all,
)
from .fromnumeric import (
any as any,
)
from .fromnumeric import (
argpartition as argpartition,
)
from .fromnumeric import (
matrix_transpose as matrix_transpose,
)
from .fromnumeric import (
mean as mean,
)
from .fromnumeric import all as all
from .fromnumeric import any as any
from .fromnumeric import argpartition as argpartition
from .fromnumeric import matrix_transpose as matrix_transpose
from .fromnumeric import mean as mean
from .multiarray import (
# other
_Array,
Expand Down Expand Up @@ -198,7 +188,16 @@ _T = TypeVar("_T")
_ScalarT = TypeVar("_ScalarT", bound=generic)
_DTypeT = TypeVar("_DTypeT", bound=np.dtype)
_ArrayT = TypeVar("_ArrayT", bound=np.ndarray[Any, Any])
_ShapeT = TypeVar("_ShapeT", bound=tuple[int, ...])
_ShapeT = TypeVar("_ShapeT", bound=_Shape)
_AnyShapeT = TypeVar(
"_AnyShapeT",
tuple[()],
tuple[int],
tuple[int, int],
tuple[int, int, int],
tuple[int, int, int, int],
tuple[int, ...],
)

_CorrelateMode: TypeAlias = L["valid", "same", "full"]

Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy