8000 ENH: Add type annotations for the np.core.fromnumeric module: part 2/4 by BvB93 · Pull Request #71 · numpy/numpy-stubs · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.

ENH: Add type annotations for the np.core.fromnumeric module: part 2/4 #71

Merged
merged 3 commits into from
Jun 5, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Replace _ArrayLike with _ArrayLikeNested for >= 1D arrays
  • Loading branch information
Bas van Beek committed May 19, 2020
commit c566a8c263c83ad1786c131b00e6fa62e99a6a47
34 changes: 17 additions & 17 deletions numpy-stubs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,8 @@ _ScalarGenericDT = TypeVar(
_Int = Union[int, integer]
_Bool = Union[bool, bool_]
_IntOrBool = Union[_Int, _Bool]
_ArrayLikeIntNested = Any # TODO: wait for support for recursive types
_ArrayLikeBoolNested = Any # TODO: wait for support for recursive types
_ArrayLikeIntNested = ArrayLike # TODO: wait for support for recursive types
_ArrayLikeBoolNested = ArrayLike # TODO: wait for support for recursive types

# Integers and booleans can generally be used interchangeably
_ArrayLikeIntOrBool = Union[
Expand Down Expand Up @@ -922,67 +922,67 @@ def argsort(
) -> ndarray: ...
@overload
def argmax(
a: Union[Sequence[_ArrayLike], ndarray],
a: Union[Sequence[ArrayLike], ndarray],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not ArrayLike here and below?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the likes of argmax(), argmin() and diagonal() are one of the few cases where, at the very least, a vector or matrix is required. Hence the whole Sequence[ArrayLike] and Sequence[Sequence[ArrayLike]] syntaxes.

axis: None = ...,
out: Optional[ndarray] = ...,
) -> integer: ...
@overload
def argmax(
a: Union[Sequence[_ArrayLike], ndarray],
a: Union[Sequence[ArrayLike], ndarray],
axis: int = ...,
out: Optional[ndarray] = ...,
) -> Union[integer, ndarray]: ...
@overload
def argmin(
a: Union[Sequence[_ArrayLike], ndarray],
a: Union[Sequence[ArrayLike], ndarray],
axis: None = ...,
out: Optional[ndarray] = ...,
) -> integer: ...
@overload
def argmin(
a: Union[Sequence[_ArrayLike], ndarray],
a: Union[Sequence[ArrayLike], ndarray],
axis: int = ...,
out: Optional[ndarray] = ...,
) -> Union[integer, ndarray]: ...
@overload
def searchsorted(
a: Union[Sequence[_ArrayLike], ndarray],
a: Union[Sequence[ArrayLike], ndarray],
v: _Scalar,
side: _Side = ...,
sorter: Union[None, Sequence[_IntOrBool], ndarray] = ..., # 1D int array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love the complexity things like this add to each signature versus having some uniform "array like but not a scalar" type that we can use everywhere. This is less detailed for now, but we can reach for this level of detail when making ndarray/ArrayLike generic; i.e. we could have something like _NonScalarArrayLike[T] and keep the code more readable/maintainable. Right now each signature ends up being its own little puzzle.

But, I haven't complained about this on previous PRs, so maybe now is not the time to start objecting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like the _NonScalarArrayLike[T] suggestion.

A question though: do you feel it would be better to implement such a change in this pull request? Or shall i create a new pull request afterwards, addressing the issue from both #71 and #67?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s wait and I’ll merge this as-is. For now I’d like to prioritize getting stuff merged into NumPy, and then we can resume from there.

) -> integer: ...
@overload
def searchsorted(
a: Union[Sequence[_ArrayLike], ndarray],
v: _ArrayLike,
a: Union[Sequence[ArrayLike], ndarray],
v: ArrayLike,
side: _Side = ...,
sorter: Union[None, Sequence[_IntOrBool], ndarray] = ..., # 1D int array
) -> ndarray: ...
def resize(a: _ArrayLike, new_shape: _ShapeLike) -> ndarray: ...
def resize(a: ArrayLike, new_shape: _ShapeLike) -> ndarray: ...
@overload
def squeeze(a: _ScalarGeneric, axis: Optional[_ShapeLike] = ...) -> _ScalarGeneric: ...
@overload
def squeeze(a: _ArrayLike, axis: Optional[_ShapeLike] = ...) -> ndarray: ...
def squeeze(a: ArrayLike, axis: Optional[_ShapeLike] = ...) -> ndarray: ...
def diagonal(
a: Union[Sequence[Sequence[_ArrayLike]], ndarray], # >= 2D array
a: Union[Sequence[Sequence[ArrayLike]], ndarray], # >= 2D array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are encountering diminishing returns and increasing code complexity with things like Sequence[Sequence[ArrayLike]].

offset: int = ...,
axis1: int = ...,
axis2: int = ...,
) -> ndarray: ...
def trace(
a: Union[Sequence[Sequence[_ArrayLike]], ndarray], # >= 2D array
a: Union[Sequence[Sequence[ArrayLike]], ndarray], # >= 2D array
offset: int = ...,
axis1: int = ...,
axis2: int = ...,
dtype: _DtypeLike = ...,
out: Optional[ndarray] = ...,
) -> Union[number, ndarray]: ...
def ravel(a: _ArrayLike, order: _Order = ...) -> ndarray: ...
def nonzero(a: _ArrayLike) -> Tuple[ndarray, ...]: ...
def shape(a: _ArrayLike) -> _Shape: ...
def ravel(a: ArrayLike, order: _Order = ...) -> ndarray: ...
def nonzero(a: ArrayLike) -> Tuple[ndarray, ...]: ...
def shape(a: ArrayLike) -> _Shape: ...
def compress(
condition: Union[Sequence[_Bool], ndarray], # 1D bool array
a: _ArrayLike,
a: ArrayLike,
axis: Optional[int] = ...,
out: Optional[ndarray] = ...,
) -> ndarray: ...
0