8000 ENH: Add aliases for common scalar unions by BvB93 · Pull Request #17096 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add aliases for common scalar unions #17096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
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
ENH,MAINT: Added _ScalarType; made ArrayLike a typevar
  • Loading branch information
Bas van Beek committed Aug 18, 2020
commit 1a2406aa02edd1ba44e4fd5ec2c1e5f084219b4a
15 changes: 4 additions & 11 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ from numpy.typing import (
_StrLike,
_BytesLike,
_DatetimeLike,
_ScalarLike,
)

from typing import (
Expand Down Expand Up @@ -907,17 +908,9 @@ _Number = TypeVar("_Number", bound=number)

# An array-like object consisting of integers
_IntOrBool = Union[_IntLike, _BoolLike]
_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[
_IntOrBool,
ndarray,
Sequence[_IntOrBool],
Sequence[_ArrayLikeIntNested],
Sequence[_ArrayLikeBoolNested],
]
_ArrayLikeIntOrBool = ArrayLike[_IntOrBool]
_ArrayLikeBool = Union[
_BoolLike,
Sequence[_BoolLike],
Expand All @@ -939,7 +932,7 @@ def take(
) -> _ScalarGenericDT: ...
@overload
def take(
a: _Scalar,
a: _ScalarLike,
indices: int,
axis: Optional[int] = ...,
out: Optional[ndarray] = ...,
Expand Down Expand Up @@ -1048,7 +1041,7 @@ def argmin(
@overload
def searchsorted(
a: ArrayLike,
v: _Scalar,
v: _ScalarLike,
side: _Side = ...,
sorter: Optional[_ArrayLikeIntOrBool] = ..., # 1D int array
) -> integer: ...
Expand Down
7 changes: 4 additions & 3 deletions numpy/typing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@
Please see : https://numpy.org/devdocs/reference/arrays.dtypes.html

"""
from ._array_like import _SupportsArray, ArrayLike
from ._shape import _Shape, _ShapeLike
from ._dtype_like import DtypeLike
from ._scalars import (
_DatetimeLike,
_TimedeltaLike,
Expand All @@ -105,4 +102,8 @@
_BytesLike,
_CharacterLike,
_VoidLike,
_ScalarLike,
)
from ._array_like import _SupportsArray, ArrayLike
from ._shape import _Shape, _ShapeLike
from ._dtype_like import DtypeLike
6 changes: 4 additions & 2 deletions numpy/typing/_array_like.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sys
from typing import Any, overload, Sequence, TYPE_CHECKING, Union
from typing import Any, overload, Sequence, TYPE_CHECKING, Union, TypeVar

from numpy import ndarray
from ._dtype_like import DtypeLike
from ._scalars import _ScalarLike

if sys.version_info >= (3, 8):
from typing import Protocol
Expand Down Expand Up @@ -31,4 +32,5 @@ def __array__(self, dtype: DtypeLike = ...) -> ndarray: ...
# is resolved. See also the mypy issue:
#
# https://github.com/python/typing/issues/593
ArrayLike = Union[bool, int, float, complex, _SupportsArray, Sequence]
_ScalarType = TypeVar('_ScalarType', bound=_ScalarLike)
ArrayLike = Union[_ScalarType, _SupportsArray, Sequence]
5 changes: 5 additions & 0 deletions numpy/typing/_scalars.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@
_BytesLike = Union[bytes, np.bytes_]
_CharacterLike = Union[str, bytes, np.character]

# _VoidLike is technically not a scalar, but it's close enough
_VoidLike = Union[tuple, np.void]

_ScalarLike = Union[
datetime, timedelta, int, float, complex, str, bytes, tuple, np.generic
]
0