summaryrefslogtreecommitdiff
path: root/numpy/_typing/_array_like.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/_typing/_array_like.py')
-rw-r--r--numpy/_typing/_array_like.py58
1 files changed, 32 insertions, 26 deletions
diff --git a/numpy/_typing/_array_like.py b/numpy/_typing/_array_like.py
index 67d67ce19..cba6fffaf 100644
--- a/numpy/_typing/_array_like.py
+++ b/numpy/_typing/_array_like.py
@@ -1,9 +1,7 @@
from __future__ import annotations
-# NOTE: Import `Sequence` from `typing` as we it is needed for a type-alias,
-# not an annotation
-from collections.abc import Collection, Callable
-from typing import Any, Sequence, Protocol, Union, TypeVar, runtime_checkable
+from collections.abc import Collection, Callable, Sequence
+from typing import Any, Protocol, Union, TypeVar, runtime_checkable
from numpy import (
ndarray,
dtype,
@@ -25,8 +23,11 @@ from ._nested_sequence import _NestedSequence
_T = TypeVar("_T")
_ScalarType = TypeVar("_ScalarType", bound=generic)
-_DType = TypeVar("_DType", bound="dtype[Any]")
-_DType_co = TypeVar("_DType_co", covariant=True, bound="dtype[Any]")
+_ScalarType_co = TypeVar("_ScalarType_co", bound=generic, covariant=True)
+_DType = TypeVar("_DType", bound=dtype[Any])
+_DType_co = TypeVar("_DType_co", covariant=True, bound=dtype[Any])
+
+NDArray = ndarray[Any, dtype[_ScalarType_co]]
# The `_SupportsArray` protocol only cares about the default dtype
# (i.e. `dtype=None` or no `dtype` parameter at all) of the to-be returned
@@ -61,8 +62,8 @@ _FiniteNestedSequence = Union[
# A subset of `npt.ArrayLike` that can be parametrized w.r.t. `np.generic`
_ArrayLike = Union[
- _SupportsArray["dtype[_ScalarType]"],
- _NestedSequence[_SupportsArray["dtype[_ScalarType]"]],
+ _SupportsArray[dtype[_ScalarType]],
+ _NestedSequence[_SupportsArray[dtype[_ScalarType]]],
]
# A union representing array-like objects; consists of two typevars:
@@ -83,64 +84,69 @@ _DualArrayLike = Union[
#
# https://github.com/python/typing/issues/593
ArrayLike = _DualArrayLike[
- dtype,
+ dtype[Any],
Union[bool, int, float, complex, str, bytes],
]
# `ArrayLike<X>_co`: array-like objects that can be coerced into `X`
# given the casting rules `same_kind`
_ArrayLikeBool_co = _DualArrayLike[
- "dtype[bool_]",
+ dtype[bool_],
bool,
]
_ArrayLikeUInt_co = _DualArrayLike[
- "dtype[Union[bool_, unsignedinteger[Any]]]",
+ dtype[Union[bool_, unsignedinteger[Any]]],
bool,
]
_ArrayLikeInt_co = _DualArrayLike[
- "dtype[Union[bool_, integer[Any]]]",
+ dtype[Union[bool_, integer[Any]]],
Union[bool, int],
]
_ArrayLikeFloat_co = _DualArrayLike[
- "dtype[Union[bool_, integer[Any], floating[Any]]]",
+ dtype[Union[bool_, integer[Any], floating[Any]]],
Union[bool, int, float],
]
_ArrayLikeComplex_co = _DualArrayLike[
- "dtype[Union[bool_, integer[Any], floating[Any], complexfloating[Any, Any]]]",
+ dtype[Union[
+ bool_,
+ integer[Any],
+ floating[Any],
+ complexfloating[Any, Any],
+ ]],
Union[bool, int, float, complex],
]
_ArrayLikeNumber_co = _DualArrayLike[
- "dtype[Union[bool_, number[Any]]]",
+ dtype[Union[bool_, number[Any]]],
Union[bool, int, float, complex],
]
_ArrayLikeTD64_co = _DualArrayLike[
- "dtype[Union[bool_, integer[Any], timedelta64]]",
+ dtype[Union[bool_, integer[Any], timedelta64]],
Union[bool, int],
]
_ArrayLikeDT64_co = Union[
- _SupportsArray["dtype[datetime64]"],
- _NestedSequence[_SupportsArray["dtype[datetime64]"]],
+ _SupportsArray[dtype[datetime64]],
+ _NestedSequence[_SupportsArray[dtype[datetime64]]],
]
_ArrayLikeObject_co = Union[
- _SupportsArray["dtype[object_]"],
- _NestedSequence[_SupportsArray["dtype[object_]"]],
+ _SupportsArray[dtype[object_]],
+ _NestedSequence[_SupportsArray[dtype[object_]]],
]
_ArrayLikeVoid_co = Union[
- _SupportsArray["dtype[void]"],
- _NestedSequence[_SupportsArray["dtype[void]"]],
+ _SupportsArray[dtype[void]],
+ _NestedSequence[_SupportsArray[dtype[void]]],
]
_ArrayLikeStr_co = _DualArrayLike[
- "dtype[str_]",
+ dtype[str_],
str,
]
_ArrayLikeBytes_co = _DualArrayLike[
- "dtype[bytes_]",
+ dtype[bytes_],
bytes,
]
_ArrayLikeInt = _DualArrayLike[
- "dtype[integer[Any]]",
+ dtype[integer[Any]],
int,
]
@@ -153,6 +159,6 @@ class _UnknownType:
_ArrayLikeUnknown = _DualArrayLike[
- "dtype[_UnknownType]",
+ dtype[_UnknownType],
_UnknownType,
]