From fd01786ea4c7dde540cede258ad11d08d25bacfc Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Wed, 7 Oct 2020 18:53:33 +0200 Subject: MAINT: Move aliases for common scalar unions to `numpy.typing` (#17429) * MAINT: Move the `Like` unions to `numpy.typing` --- numpy/typing/_array_like.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'numpy/typing/_array_like.py') 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, +] -- cgit v1.2.1 From c4684bb542db2690e678d22bf32745e484ef04e3 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Tue, 3 Nov 2020 13:08:58 +0100 Subject: MAINT: Rename `DtypeLike` to `DTypeLike` --- numpy/typing/_array_like.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'numpy/typing/_array_like.py') diff --git a/numpy/typing/_array_like.py b/numpy/typing/_array_like.py index 1c00b200f..a1a604239 100644 --- a/numpy/typing/_array_like.py +++ b/numpy/typing/_array_like.py @@ -3,7 +3,7 @@ from typing import Any, overload, Sequence, TYPE_CHECKING, Union from numpy import ndarray from ._scalars import _ScalarLike -from ._dtype_like import DtypeLike +from ._dtype_like import DTypeLike if sys.version_info >= (3, 8): from typing import Protocol @@ -19,9 +19,9 @@ else: if TYPE_CHECKING or HAVE_PROTOCOL: class _SupportsArray(Protocol): @overload - def __array__(self, __dtype: DtypeLike = ...) -> ndarray: ... + def __array__(self, __dtype: DTypeLike = ...) -> ndarray: ... @overload - def __array__(self, dtype: DtypeLike = ...) -> ndarray: ... + def __array__(self, dtype: DTypeLike = ...) -> ndarray: ... else: _SupportsArray = Any -- cgit v1.2.1 From 1237be83356e20f568f28e54bc0099f5acd3e2db Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 11 Dec 2020 13:22:53 +0100 Subject: ENH: Add dtype-support for the `__array__` protocol --- numpy/typing/_array_like.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'numpy/typing/_array_like.py') diff --git a/numpy/typing/_array_like.py b/numpy/typing/_array_like.py index a1a604239..4ea6974b2 100644 --- a/numpy/typing/_array_like.py +++ b/numpy/typing/_array_like.py @@ -1,7 +1,9 @@ +from __future__ import annotations + 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 numpy import ndarray, dtype from ._scalars import _ScalarLike from ._dtype_like import DTypeLike @@ -16,12 +18,15 @@ else: else: HAVE_PROTOCOL = True +_DType = TypeVar("_DType", bound="dtype[Any]") + if TYPE_CHECKING or HAVE_PROTOCOL: - class _SupportsArray(Protocol): - @overload - def __array__(self, __dtype: DTypeLike = ...) -> ndarray: ... - @overload - def __array__(self, dtype: DTypeLike = ...) -> ndarray: ... + # The `_SupportsArray` protocol only cares about the default dtype + # (i.e. `dtype=None`) ofthe to-be returned array. + # Concrete implementations of the protocol are responsible for adding + # any and all remaining overloads + class _SupportsArray(Protocol[_DType]): + def __array__(self, dtype: None = ...) -> ndarray[Any, _DType]: ... else: _SupportsArray = Any @@ -36,5 +41,5 @@ ArrayLike = Union[ _ScalarLike, Sequence[_ScalarLike], Sequence[Sequence[Any]], # TODO: Wait for support for recursive types - _SupportsArray, + "_SupportsArray[Any]", ] -- cgit v1.2.1 From 6139ed42af271d37234320e83a41000d93bdeae1 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 11 Dec 2020 17:33:29 +0100 Subject: STY: Fixed a typo: `ofthe` -> `of the` Addresses https://github.com/numpy/numpy/pull/17981#discussion_r541018879 Co-Authored-By: Charles Harris --- numpy/typing/_array_like.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/typing/_array_like.py') diff --git a/numpy/typing/_array_like.py b/numpy/typing/_array_like.py index 4ea6974b2..63b67b33a 100644 --- a/numpy/typing/_array_like.py +++ b/numpy/typing/_array_like.py @@ -22,7 +22,7 @@ _DType = TypeVar("_DType", bound="dtype[Any]") if TYPE_CHECKING or HAVE_PROTOCOL: # The `_SupportsArray` protocol only cares about the default dtype - # (i.e. `dtype=None`) ofthe to-be returned array. + # (i.e. `dtype=None`) of the to-be returned array. # Concrete implementations of the protocol are responsible for adding # any and all remaining overloads class _SupportsArray(Protocol[_DType]): -- cgit v1.2.1