-
-
Notifications
You must be signed in to change notification settings - Fork 31
ENH: Add type annotations for the np.core.fromnumeric module: part 2/4 #71
Conversation
With the recent push of #74 this pull request is ready for review again. |
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``.
@@ -919,3 +920,69 @@ def argsort( | |||
kind: Optional[_SortKind] = ..., | |||
order: Union[None, str, Sequence[str]] = ..., | |||
) -> ndarray: ... | |||
@overload | |||
def argmax( | |||
a: Union[Sequence[ArrayLike], ndarray], |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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]]
.
Add annotations for the second batch of
np.core.fromnumeric
functions from #59New Functions
argmax()
argmin()
searchsorted()
resize()
squeeze()
diagonal()
trace()
ravel()
nonzero()
shape()
compress()