-
-
Notifications
You must be signed in to change notification settings - Fork 32
MAINT: Introduced a couple of fixes for the np.core.fromnumeric functions #74
Changes from 6 commits
22a8632
4e2e6bf
fcd4e46
062947b
443b301
e519d92
911eed0
cda1e70
45158c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -813,14 +813,27 @@ _ScalarNumpy = Union[generic, dt.datetime, dt.timedelta] | |
_ScalarBuiltin = Union[str, bytes, dt.date, dt.timedelta, bool, int, float, complex] | ||
_Scalar = Union[_ScalarBuiltin, _ScalarNumpy] | ||
|
||
_ScalarGeneric = TypeVar( | ||
"_ScalarGeneric", bound=Union[dt.datetime, dt.timedelta, generic] | ||
# Integers and booleans can generally be used interchangeably | ||
_ScalarInt = TypeVar("_ScalarInt", bound=Union[integer, bool_]) | ||
_ScalarGeneric = TypeVar("_ScalarGeneric", bound=generic) | ||
_ScalarGenericPlus = TypeVar( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe something like About your second concern, some quick testing suggests that subclassing does indeed work: >>> import datetime as dt
>>> import numpy as np
>>> class A(dt.timedelta): ...
>>> np.take(A(1), 0).__class__
__main__.A
>>> class B(dt.datetime): ...
>>> np.take(B(2000, 1, 1), 0).__class__
__main__.B |
||
"_ScalarGenericPlus", bound=Union[dt.datetime, dt.timedelta, generic] | ||
) | ||
|
||
# An array-like object consisting of integers | ||
_Int = Union[int, integer] | ||
_Int = Union[int, integer, bool, bool_] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above about naming. |
||
_Bool = Union[bool, bool_] | ||
_ArrayLikeIntNested = Any # TODO: wait for support for recursive types | ||
_ArrayLikeInt = Union[_Int, ndarray, Sequence[_Int], Sequence[_ArrayLikeIntNested]] | ||
_ArrayLikeBoolNested = Any # TODO: wait for support for recursive types | ||
|
||
# Integers and booleans can generally be used interchangeably | ||
_ArrayLikeInt = Union[ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above about naming. |
||
_Int, | ||
ndarray, | ||
Sequence[_Int], | ||
Sequence[_ArrayLikeIntNested], | ||
Sequence[_ArrayLikeBoolNested], | ||
] | ||
|
||
# The signature of take() follows a common theme with its overloads: | ||
# 1. A generic comes in; the same generic comes out | ||
|
@@ -829,12 +842,12 @@ _ArrayLikeInt = Union[_Int, ndarray, Sequence[_Int], Sequence[_ArrayLikeIntNeste | |
# 4. An array-like object comes in; an ndarray or generic comes out | ||
@overload | ||
def take( | ||
a: _ScalarGeneric, | ||
a: _ScalarGenericPlus, | ||
indices: int, | ||
axis: Optional[int] = ..., | ||
out: Optional[ndarray] = ..., | ||
mode: _Mode = ..., | ||
) -> _ScalarGeneric: ... | ||
) -> _ScalarGenericPlus: ... | ||
@overload | ||
def take( | ||
a: _Scalar, | ||
|
@@ -862,21 +875,21 @@ def take( | |
def reshape(a: _ArrayLike, newshape: _ShapeLike, order: _Order = ...) -> ndarray: ... | ||
@overload | ||
def choose( | ||
a: _ScalarGeneric, | ||
a: _ScalarInt, | ||
choices: Union[Sequence[_ArrayLike], ndarray], | ||
out: Optional[ndarray] = ..., | ||
mode: _Mode = ..., | ||
) -> _ScalarGeneric: ... | ||
) -> _ScalarInt: ... | ||
@overload | ||
def choose( | ||
a: _Scalar, | ||
a: _Int, | ||
choices: Union[Sequence[_ArrayLike], ndarray], | ||
out: Optional[ndarray] = ..., | ||
mode: _Mode = ..., | ||
) -> _ScalarNumpy: ... | ||
) -> Union[integer, bool_]: ... | ||
@overload | ||
def choose( | ||
a: _ArrayLike, | ||
a: _ArrayLikeInt, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So while passing bools technically works, given that the the docstring says "This array must contain integers in [0, n-1], where n is the number of choices" it seems to me that this is more an accident than the intent. I'm not sure that we should allow this in the types as it seems like not a best practice. @rgommers WDYT about this case? (We could also solicit opinions on the mailing list.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accident might not be the best term here. >>> import numpy as np
>>> issubclass(int, bool)
True
>>> True == 1 and False == 0
True
>>> np.bool_(True) == 1 and np.bool_(False) == 0
True
This also means that we could remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
And this is why I don't want to include it. Yes, including There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough, I've implemented the name changes in #74. |
||
choices: Union[Sequence[_ArrayLike], ndarray], | ||
out: Optional[ndarray] = ..., | ||
mode: _Mode = ..., | ||
|
@@ -898,6 +911,23 @@ def partition( | |
kind: _PartitionKind = ..., | ||
order: Union[None, str, Sequence[str]] = ..., | ||
) -> ndarray: ... | ||
@overload | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The behaviour of However, if a builtin scalar is passed it also returns a >>> import numpy as np
>>> type(np.argpartition(0, 0))
numpy.ndarray
>>> type(np.argpartition(np.int64(0), 0))
numpy.int64 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ouch. Sounds like it could potentially be worthy of opening an issue against NumPy. |
||
def argpartition( | ||
a: generic, | ||
kth: _ArrayLikeInt, | ||
axis: Optional[int] = ..., | ||
kind: _PartitionKind = ..., | ||
order: Union[None, str, Sequence[str]] = ..., | ||
) -> integer: ... | ||
@overload | ||
def argpartition( | ||
a: _ScalarBuiltin, | ||
kth: _ArrayLikeInt, | ||
axis: Optional[int] = ..., | ||
kind: _PartitionKind = ..., | ||
order: Union[None, str, Sequence[str]] = ..., | ||
) -> ndarray: ... | ||
@overload | ||
def argpartition( | ||
a: _ArrayLike, | ||
kth: _ArrayLikeInt, | ||
|
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 think
_ScalarInt
is the best name here; seems like it should be_ScalarIntOrBool
. I mostly don't want this to accidentally get cargo-culted to other places where NumPy int/bool diverge; e.g. arithmetic.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.
Based on #74 (comment) I'd argue that it is a suitable name. It can still be changed though if you don't agree,
Besides, for arithmetic it would (most likely) be more convenient to use a typevar of
np.number
anyway.