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

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

merged 3 commits into from
Jun 5, 2020

Conversation

BvB93
Copy link
Member
@BvB93 BvB93 commented Apr 24, 2020

Add annotations for the second batch of np.core.fromnumeric functions from #59

New Functions

  • argmax()
  • argmin()
  • searchsorted()
  • resize()
  • squeeze()
  • diagonal()
  • trace()
  • ravel()
  • nonzero()
  • shape()
  • compress()

@person142
Copy link
Member

Hey @BvB93, can we move the

#67 Fixes & Updates

parts into their own PR to keep concerns separate?

@BvB93 BvB93 marked this pull request as draft April 25, 2020 21:34
@BvB93 BvB93 changed the title ENH & MAINT: Add type annotations for the np.core.fromnumeric module: part 2/4 ENH: Add type annotations for the np.core.fromnumeric module: part 2/4 Apr 25, 2020
@BvB93
Copy link
Member Author
BvB93 commented Apr 25, 2020

Hey @BvB93, can we move the
...
parts into their own PR to keep concerns separate?

Yeah sure, I've created a pull request for the fixes at #74.
In the mean time (i.e. until #74 is pushed) I'll keep the pull request here as a draft.

@BvB93 BvB93 marked this pull request as ready for review May 1, 2020 09:37
@BvB93
Copy link
Member Author
BvB93 commented May 1, 2020

With the recent push of #74 this pull request is ready for review again.

Bas van Beek added 3 commits May 19, 2020 22:53
New functions:
* ``argmax()``
* ``argmin()``
* ``searchsorted()``
* ``resize()``
* ``squeeze()``
* ``diagonal()``
* ``trace()``
* ``ravel()``
* ``nonzero()``
* ``shape()``
* ``compress()``

Updates:
* Integers and booleans can often be used interchangebly (in fact, ``bool`` is an ``int`` subclass in builtin python).
  Updated ``_ArrayLikeInt`` to reflect this compatibility.
* Rename ``_ScalarGeneric`` to ``_ScalarGenericPlus``;
  add a new ``_ScalarGeneric`` TypeVar which is *only* bound to ``np.generic``.
* ``np.choose()`` cannot only accept integers (or booleans); reflect this in its annotation.
* It seems ``np.partition()`` is bugged in NumPy 1.17.3;
  it's default value for ``axis`` is set to ``-1`` instead of ``None``.
  Consquently, it will raise an ``AxisError`` if a scalar is passed *unless* one explicitly passes ``axis=None`` to the function.
* ``np.argpartition()`` will return a ``np.generic`` if a scalar is passed; not an ``np.ndarray``.
@person142 person142 added this to the before-merging-into-numpy milestone May 24, 2020
@@ -919,3 +920,69 @@ def argsort(
kind: Optional[_SortKind] = ...,
order: Union[None, str, Sequence[str]] = ...,
) -> ndarray: ...
@overload
def argmax(
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.

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.

@overload
def squeeze(a: ArrayLike, axis: Optional[_ShapeLike] = ...) -> ndarray: ...
def diagonal(
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]].

@person142 person142 merged commit 5aaa332 into numpy:master Jun 5, 2020
@BvB93 BvB93 deleted the fromnumeric branch June 5, 2020 19:32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0