Skip to content

Commit

Permalink
Merge pull request #22587 from BvB93/typing
Browse files Browse the repository at this point in the history
TYP: Update type annotations for new 1.24 features
  • Loading branch information
charris committed Nov 15, 2022
2 parents fbe1b65 + b780d8b commit 8fff8ce
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 23 deletions.
1 change: 1 addition & 0 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ from numpy.lib.utils import (
lookfor as lookfor,
byte_bounds as byte_bounds,
safe_eval as safe_eval,
show_runtime as show_runtime,
)

from numpy.matrixlib import (
Expand Down
51 changes: 40 additions & 11 deletions numpy/core/shape_base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ from collections.abc import Sequence
from typing import TypeVar, overload, Any, SupportsIndex

from numpy import generic, _CastingKind
from numpy._typing import ArrayLike, NDArray, _ArrayLike, DTypeLike
from numpy._typing import (
NDArray,
ArrayLike,
DTypeLike,
_ArrayLike,
_DTypeLike,
)

_SCT = TypeVar("_SCT", bound=generic)
_ArrayType = TypeVar("_ArrayType", bound=NDArray[Any])
Expand Down Expand Up @@ -34,39 +40,62 @@ def atleast_3d(*arys: ArrayLike) -> list[NDArray[Any]]: ...
def vstack(
tup: Sequence[_ArrayLike[_SCT]],
*,
dtype: DTypeLike = ...,
dtype: None = ...,
casting: _CastingKind = ...
) -> NDArray[_SCT]: ...
@overload
def vstack(
tup: Sequence[ArrayLike],
*,
dtype: DTypeLike = ...,
dtype: _DTypeLike[_SCT],
casting: _CastingKind = ...
) -> NDArray[_SCT]: ...
@overload
def vstack(
tup: Sequence[ArrayLike],
*,
dtype: DTypeLike = ...,
casting: _CastingKind = ...
) -> NDArray[Any]: ...

@overload
def hstack(
tup: Sequence[_ArrayLike[_SCT]],
tup: Sequence[_ArrayLike[_SCT]],
*,
dtype: None = ...,
casting: _CastingKind = ...
) -> NDArray[_SCT]: ...
@overload
def hstack(
tup: Sequence[ArrayLike],
*,
dtype: DTypeLike = ...,
dtype: _DTypeLike[_SCT],
casting: _CastingKind = ...
) -> NDArray[_SCT]: ...
@overload
def hstack(
tup: Sequence[ArrayLike],
tup: Sequence[ArrayLike],
*,
dtype: DTypeLike = ...,
dtype: DTypeLike = ...,
casting: _CastingKind = ...
) -> NDArray[Any]: ...

@overload
def stack(
arrays: Sequence[_ArrayLike[_SCT]],
axis: SupportsIndex = ...,
out: None = ...,
out: None = ...,
*,
dtype: DTypeLike = ...,
dtype: None = ...,
casting: _CastingKind = ...
) -> NDArray[_SCT]: ...
@overload
def stack(
arrays: Sequence[ArrayLike],
axis: SupportsIndex = ...,
out: None = ...,
*,
dtype: _DTypeLike[_SCT],
casting: _CastingKind = ...
) -> NDArray[_SCT]: ...
@overload
Expand All @@ -75,7 +104,7 @@ def stack(
axis: SupportsIndex = ...,
out: None = ...,
*,
dtype: DTypeLike = ...,
dtype: DTypeLike = ...,
casting: _CastingKind = ...
) -> NDArray[Any]: ...
@overload
Expand All @@ -84,7 +113,7 @@ def stack(
axis: SupportsIndex = ...,
out: _ArrayType = ...,
*,
dtype: DTypeLike = ...,
dtype: DTypeLike = ...,
casting: _CastingKind = ...
) -> _ArrayType: ...

Expand Down
1 change: 1 addition & 0 deletions numpy/lib/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ from numpy.lib.utils import (
lookfor as lookfor,
byte_bounds as byte_bounds,
safe_eval as safe_eval,
show_runtime as show_runtime,
)

from numpy.core.multiarray import (
Expand Down
8 changes: 2 additions & 6 deletions numpy/lib/function_base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,8 @@ def sinc(x: _ArrayLikeFloat_co) -> NDArray[floating[Any]]: ...
@overload
def sinc(x: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ...

@overload
def msort(a: _ArrayType) -> _ArrayType: ...
@overload
def msort(a: _ArrayLike[_SCT]) -> NDArray[_SCT]: ...
@overload
def msort(a: ArrayLike) -> NDArray[Any]: ...
# NOTE: Deprecated
# def msort(a: ArrayLike) -> NDArray[Any]: ...

@overload
def median(
Expand Down
2 changes: 2 additions & 0 deletions numpy/lib/utils.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,5 @@ def lookfor(
) -> None: ...

def safe_eval(source: str | AST) -> Any: ...

def show_runtime() -> None: ...
1 change: 0 additions & 1 deletion numpy/ma/core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ class MaskedArray(ndarray[_ShapeType, _DType_co]):
def sort(self, axis=..., kind=..., order=..., endwith=..., fill_value=...): ...
def min(self, axis=..., out=..., fill_value=..., keepdims=...): ...
# NOTE: deprecated
# def mini(self, axis=...): ...
# def tostring(self, fill_value=..., order=...): ...
def max(self, axis=..., out=..., fill_value=..., keepdims=...): ...
def ptp(self, axis=..., out=..., fill_value=..., keepdims=...): ...
Expand Down
4 changes: 3 additions & 1 deletion numpy/polynomial/_polybase.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class ABCPolyBase(abc.ABC):
maxpower: ClassVar[int]
coef: Any
@property
def symbol(self) -> str: ...
@property
@abc.abstractmethod
def domain(self): ...
@property
Expand All @@ -21,7 +23,7 @@ class ABCPolyBase(abc.ABC):
def has_samedomain(self, other): ...
def has_samewindow(self, other): ...
def has_sametype(self, other): ...
def __init__(self, coef, domain=..., window=...): ...
def __init__(self, coef, domain=..., window=..., symbol: str = ...) -> None: ...
def __format__(self, fmt_str): ...
def __call__(self, arg): ...
def __iter__(self): ...
Expand Down
3 changes: 3 additions & 0 deletions numpy/typing/tests/data/reveal/array_constructors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,15 @@ reveal_type(np.atleast_2d(A)) # E: ndarray[Any, dtype[{float64}]]
reveal_type(np.atleast_3d(A)) # E: ndarray[Any, dtype[{float64}]]

reveal_type(np.vstack([A, A])) # E: ndarray[Any, Any]
reveal_type(np.vstack([A, A], dtype=np.float64)) # E: ndarray[Any, dtype[{float64}]]
reveal_type(np.vstack([A, C])) # E: ndarray[Any, dtype[Any]]
reveal_type(np.vstack([C, C])) # E: ndarray[Any, dtype[Any]]

reveal_type(np.hstack([A, A])) # E: ndarray[Any, Any]
reveal_type(np.hstack([A, A], dtype=np.float64)) # E: ndarray[Any, dtype[{float64}]]

reveal_type(np.stack([A, A])) # E: Any
reveal_type(np.stack([A, A], dtype=np.float64)) # E: ndarray[Any, dtype[{float64}]]
reveal_type(np.stack([A, C])) # E: ndarray[Any, dtype[Any]]
reveal_type(np.stack([C, C])) # E: ndarray[Any, dtype[Any]]
reveal_type(np.stack([A, A], axis=0)) # E: Any
Expand Down
4 changes: 0 additions & 4 deletions numpy/typing/tests/data/reveal/lib_function_base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ reveal_type(np.sinc(1j)) # E: complexfloating[Any, Any]
reveal_type(np.sinc(AR_f8)) # E: ndarray[Any, dtype[floating[Any]]]
reveal_type(np.sinc(AR_c16)) # E: ndarray[Any, dtype[complexfloating[Any, Any]]]

reveal_type(np.msort(CHAR_AR_U)) # E: Any
reveal_type(np.msort(AR_U)) # E: ndarray[Any, dtype[str_]]
reveal_type(np.msort(AR_LIKE_f8)) # E: ndarray[Any, dtype[Any]]

reveal_type(np.median(AR_f8, keepdims=False)) # E: floating[Any]
reveal_type(np.median(AR_c16, overwrite_input=True)) # E: complexfloating[Any, Any]
reveal_type(np.median(AR_m)) # E: timedelta64
Expand Down

0 comments on commit 8fff8ce

Please sign in to comment.