diff options
author | Bas van Beek <43369155+BvB93@users.noreply.github.com> | 2020-10-07 18:53:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-07 19:53:33 +0300 |
commit | fd01786ea4c7dde540cede258ad11d08d25bacfc (patch) | |
tree | 6601706476da64b117088d6bbe368bc18521cb53 /numpy | |
parent | 2378c3c987c479cca27470d3d66b5d50e91ad673 (diff) | |
download | numpy-fd01786ea4c7dde540cede258ad11d08d25bacfc.tar.gz |
MAINT: Move aliases for common scalar unions to `numpy.typing` (#17429)
* MAINT: Move the `<scalar>Like` unions to `numpy.typing`
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/__init__.pyi | 20 | ||||
-rw-r--r-- | numpy/core/fromnumeric.pyi | 13 | ||||
-rw-r--r-- | numpy/core/function_base.pyi | 4 | ||||
-rw-r--r-- | numpy/typing/__init__.py | 11 | ||||
-rw-r--r-- | numpy/typing/_array_like.py | 8 | ||||
-rw-r--r-- | numpy/typing/_callable.py | 12 | ||||
-rw-r--r-- | numpy/typing/_scalars.py | 26 |
7 files changed, 73 insertions, 21 deletions
diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 789bac896..3d40682e7 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -4,7 +4,18 @@ import datetime as dt from abc import abstractmethod from numpy.core._internal import _ctypes -from numpy.typing import ArrayLike, DtypeLike, _Shape, _ShapeLike +from numpy.typing import ( + ArrayLike, + DtypeLike, + _Shape, + _ShapeLike, + _CharLike, + _BoolLike, + _IntLike, + _FloatLike, + _ComplexLike, + _NumberLike, +) from numpy.typing._callable import ( _BoolOp, _BoolSub, @@ -1309,13 +1320,6 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container): # See https://github.com/numpy/numpy-stubs/pull/80 for more details. -_CharLike = Union[str, bytes] -_BoolLike = Union[bool, bool_] -_IntLike = Union[int, integer] -_FloatLike = Union[_IntLike, float, floating] -_ComplexLike = Union[_FloatLike, complex, complexfloating] -_NumberLike = Union[int, float, complex, number, bool_] - class generic(_ArrayOrScalarCommon): @abstractmethod def __init__(self, *args: Any, **kwargs: Any) -> None: ... diff --git a/numpy/core/fromnumeric.pyi b/numpy/core/fromnumeric.pyi index 6b3d2268f..3167d12b9 100644 --- a/numpy/core/fromnumeric.pyi +++ b/numpy/core/fromnumeric.pyi @@ -10,9 +10,6 @@ from numpy import ( generic, _OrderKACF, _OrderACF, - _IntLike, - _BoolLike, - _NumberLike, _ArrayLikeBool, _ArrayLikeIntOrBool, _ModeKind, @@ -20,7 +17,15 @@ from numpy import ( _SortKind, _SortSide, ) -from numpy.typing import DtypeLike, ArrayLike, _ShapeLike, _Shape +from numpy.typing import ( + DtypeLike, + ArrayLike, + _ShapeLike, + _Shape, + _IntLike, + _BoolLike, + _NumberLike, +) if sys.version_info >= (3, 8): from typing import Literal diff --git a/numpy/core/function_base.pyi b/numpy/core/function_base.pyi index c6ebbd5f5..705712253 100644 --- a/numpy/core/function_base.pyi +++ b/numpy/core/function_base.pyi @@ -1,8 +1,8 @@ import sys from typing import overload, Tuple, Union, Sequence, Any -from numpy import ndarray, inexact, _NumberLike -from numpy.typing import ArrayLike, DtypeLike, _SupportsArray +from numpy import ndarray, inexact +from numpy.typing import ArrayLike, DtypeLike, _SupportsArray, _NumberLike if sys.version_info >= (3, 8): from typing import SupportsIndex, Literal diff --git a/numpy/typing/__init__.py b/numpy/typing/__init__.py index 86fd5e787..987aa39aa 100644 --- a/numpy/typing/__init__.py +++ b/numpy/typing/__init__.py @@ -90,6 +90,16 @@ since its usage is discouraged. Please see : https://numpy.org/devdocs/reference/arrays.dtypes.html """ +from ._scalars import ( + _CharLike, + _BoolLike, + _IntLike, + _FloatLike, + _ComplexLike, + _NumberLike, + _ScalarLike, + _VoidLike, +) from ._array_like import _SupportsArray, ArrayLike from ._shape import _Shape, _ShapeLike from ._dtype_like import DtypeLike @@ -97,4 +107,3 @@ from ._dtype_like import DtypeLike from numpy._pytesttester import PytestTester test = PytestTester(__name__) del PytestTester - diff --git a/numpy/typing/_array_like.py b/numpy/typing/_array_like.py index 76c0c839c..1c00b200f 100644 --- a/numpy/typing/_array_like.py +++ b/numpy/typing/_array_like.py @@ -2,6 +2,7 @@ import sys from typing import Any, overload, Sequence, TYPE_CHECKING, Union from numpy import ndarray +from ._scalars import _ScalarLike from ._dtype_like import DtypeLike if sys.version_info >= (3, 8): @@ -31,4 +32,9 @@ else: # is resolved. See also the mypy issue: # # https://github.com/python/typing/issues/593 -ArrayLike = Union[bool, int, float, complex, _SupportsArray, Sequence] +ArrayLike = Union[ + _ScalarLike, + Sequence[_ScalarLike], + Sequence[Sequence[Any]], # TODO: Wait for support for recursive types + _SupportsArray, +] diff --git a/numpy/typing/_callable.py b/numpy/typing/_callable.py index 5e14b708f..0d876ae8d 100644 --- a/numpy/typing/_callable.py +++ b/numpy/typing/_callable.py @@ -12,11 +12,6 @@ import sys from typing import Union, TypeVar, overload, Any from numpy import ( - _BoolLike, - _IntLike, - _FloatLike, - _ComplexLike, - _NumberLike, generic, bool_, timedelta64, @@ -32,6 +27,13 @@ from numpy import ( complexfloating, complex128, ) +from ._scalars import ( + _BoolLike, + _IntLike, + _FloatLike, + _ComplexLike, + _NumberLike, +) if sys.version_info >= (3, 8): from typing import Protocol diff --git a/numpy/typing/_scalars.py b/numpy/typing/_scalars.py new file mode 100644 index 000000000..e4fc28b07 --- /dev/null +++ b/numpy/typing/_scalars.py @@ -0,0 +1,26 @@ +from typing import Union, Tuple, Any + +import numpy as np + +# NOTE: `_StrLike` and `_BytesLike` are pointless, as `np.str_` and `np.bytes_` +# are already subclasses of their builtin counterpart + +_CharLike = Union[str, bytes] + +_BoolLike = Union[bool, np.bool_] +_IntLike = Union[int, np.integer] +_FloatLike = Union[_IntLike, float, np.floating] +_ComplexLike = Union[_FloatLike, complex, np.complexfloating] +_NumberLike = Union[int, float, complex, np.number, np.bool_] + +_ScalarLike = Union[ + int, + float, + complex, + str, + bytes, + np.generic, +] + +# `_VoidLike` is technically not a scalar, but it's close enough +_VoidLike = Union[Tuple[Any, ...], np.void] |