8000 MAINT: Introduced a couple of fixes for the np.core.fromnumeric functions by BvB93 · Pull Request #74 · 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.

MAINT: Introduced a couple of fixes for the np.core.fromnumeric functions #74

Merged
merged 9 commits into from
May 1, 2020
Prev Previous commit
Next Next commit
Addressed comments from #74
* Renamed ``_ScalarGenericPlus `` to ``_ScalarGenericDT`` (#74 (comment)).
* Renamed ``_Int`` to ``_IntOrBool`` (#74 (comment)).
* Renamed ``_ArrayLikeInt`` to ``_ArrayLikeIntOrBool`` (#74 (comment)).
  • Loading branch information
Bas van Beek committed Apr 29, 2020
commit 911eed0a6998ba36fe8ead171dd4a801031475a9
36 changes: 19 additions & 17 deletions numpy-stubs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -814,10 +814,10 @@ _ScalarBuiltin = Union[str, bytes, dt.date, dt.timedelta, bool, int, float, comp
_Scalar = Union[_ScalarBuiltin, _ScalarNumpy]

# Integers and booleans can generally be used interchangeably
_ScalarInt = TypeVar("_ScalarInt", bound=Union[integer, bool_])
_ScalarIntOrBool = TypeVar("_ScalarIntOrBool", bound=Union[integer, bool_])
_ScalarGeneric = TypeVar("_ScalarGeneric", bound=generic)
_ScalarGenericPlus = TypeVar(
"_ScalarGenericPlus", bound=Union[dt.datetime, dt.timedelta, generic]
_ScalarGenericDT = TypeVar(
"_ScalarGenericDT", bound=Union[dt.datetime, dt.timedelta, generic]
)

# An array-like object consisting of integers
Expand All @@ -827,11 +827,11 @@ _ArrayLikeIntNested = Any # TODO: wait for support for recursive types
_ArrayLikeBoolNested = Any # TODO: wait for support for recursive types

# Integers and booleans can generally be used interchangeably
_ArrayLikeInt = Union[
_ArrayLikeIntOrBool = Union[
_Int,
ndarray,
Sequence[_Int],
Sequence[_ArrayLikeIntNested],
Sequence[_ArrayLikeIntOrBoolNested],
Sequence[_ArrayLikeBoolNested],
]

Expand All @@ -842,12 +842,12 @@ _ArrayLikeInt = Union[
# 4. An array-like object comes in; an ndarray or generic comes out
@overload
def take(
a: _ScalarGenericPlus,
a: _ScalarGenericDT,
indices: int,
axis: Optional[int] = ...,
out: Optional[ndarray] = ...,
mode: _Mode = ...,
) -> _ScalarGenericPlus: ...
) -> _ScalarGenericDT 8000 : ...
@overload
def take(
a: _Scalar,
Expand All @@ -867,19 +867,19 @@ def take(
@overload
def take(
a: _ArrayLike,
indices: _ArrayLikeInt,
indices: _ArrayLikeIntOrBool,
axis: Optional[int] = ...,
out: Optional[ndarray] = ...,
mode: _Mode = ...,
) -> Union[_ScalarNumpy, ndarray]: ...
def reshape(a: _ArrayLike, newshape: _ShapeLike, order: _Order = ...) -> ndarray: ...
@overload
def choose(
a: _ScalarInt,
a: _ScalarIntOrBool,
choices: Union[Sequence[_ArrayLike], ndarray],
out: Optional[ndarray] = ...,
mode: _Mode = ...,
) -> _ScalarInt: ...
) -> _ScalarIntOrBool: ...
@overload
def choose(
a: _Int,
Expand All @@ -889,15 +889,17 @@ def choose(
) -> Union[integer, bool_]: ...
@overload
def choose(
a: _ArrayLikeInt,
a: _ArrayLikeIntOrBool,
choices: Union[Sequence[_ArrayLike], ndarray],
out: Optional[ndarray] = ...,
mode: _Mode = ...,
) -> ndarray: ...
def repeat(
a: _ArrayLike, repeats: _ArrayLikeInt, axis: Optional[int] = ...
a: _ArrayLike, repeats: _ArrayLikeIntOrBool, axis: Optional[int] = ...
) -> ndarray: ...
def put(a: ndarray, ind: _ArrayLikeInt, v: _ArrayLike, mode: _Mode = ...) -> None: ...
def put(
a: ndarray, ind: _ArrayLikeIntOrBool, v: _ArrayLike, mode: _Mode = ...
) -> None: ...
def swapaxes(
a: Union[Sequence[_ArrayLike], ndarray], axis1: int, axis2: int
) -> ndarray: ...
Expand All @@ -906,31 +908,31 @@ def transpose(
) -> ndarray: ...
def partition(
a: _ArrayLike,
kth: _ArrayLikeInt,
kth: _ArrayLikeIntOrBool,
axis: Optional[int] = ...,
kind: _PartitionKind = ...,
order: Union[None, str, Sequence[str]] = ...,
) -> ndarray: ...
@overload
Copy link
Member Author

Choose a reason for hiding this comment

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

The behaviour of np.argpartition() here is a bit odd.
If an np.ndarray is passed an np.ndarray is returned and if a np.generic is passed a np.generic is returned, so far so good.

However, if a builtin scalar is passed it also returns a np.ndarray, rather than a np.generic.

>>> import numpy as np

>>> type(np.argpartition(0, 0))
numpy.ndarray

>>> type(np.argpartition(np.int64(0), 0))
numpy.int64

Copy link
Member

Choose a reason for hiding this comment

The 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,
kth: _ArrayLikeIntOrBool,
axis: Optional[int] = ...,
kind: _PartitionKind = ...,
order: Union[None, str, Sequence[str]] = ...,
) -> integer: ...
@overload
def argpartition(
a: _ScalarBuiltin,
kth: _ArrayLikeInt,
kth: _ArrayLikeIntOrBool,
axis: Optional[int] = ...,
kind: _PartitionKind = ...,
order: Union[None, str, Sequence[str]] = ...,
) -> ndarray: ...
@overload
def argpartition(
a: _ArrayLike,
kth: _ArrayLikeInt,
kth: _ArrayLikeIntOrBool,
axis: Optional[int] = ...,
kind: _PartitionKind = ...,
order: Union[None, str, Sequence[str]] = ...,
Expand Down
0