Skip to content

Commit

Permalink
TYP,ENH: Improve the dtype-overload of stack, hstack and vstack
Browse files Browse the repository at this point in the history
  • Loading branch information
BvB93 committed Nov 14, 2022
1 parent 8205e5d commit 473b80f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
51 changes: 40 additions & 11 deletions numpy/core/shape_base.pyi
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
3 changes: 3 additions & 0 deletions numpy/typing/tests/data/reveal/array_constructors.pyi
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

0 comments on commit 473b80f

Please sign in to comment.