summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2021-05-28 10:28:15 -0600
committerGitHub <noreply@github.com>2021-05-28 10:28:15 -0600
commit0725c47df4388d0e806a2e81d5298973bb3da545 (patch)
treea9123f4964c66a248b9d42ce45c040e8420a79d4
parent4c93c93dbe131359fca93b7543e65c48f5ae0dc1 (diff)
parent757eb4abd1b6823d4d7933c9ad0c75160750833b (diff)
downloadnumpy-0725c47df4388d0e806a2e81d5298973bb3da545.tar.gz
Merge pull request #19118 from BvB93/sty
MAINT: Misc cleaning of `numpy.typing`
-rw-r--r--numpy/typing/__init__.py21
-rw-r--r--numpy/typing/_array_like.py15
-rw-r--r--numpy/typing/_callable.py152
-rw-r--r--numpy/typing/_char_codes.py114
-rw-r--r--numpy/typing/_dtype_like.py77
-rw-r--r--numpy/typing/_generic_alias.py1
-rw-r--r--numpy/typing/_shape.py9
-rw-r--r--numpy/typing/_ufunc.pyi70
-rw-r--r--numpy/typing/mypy_plugin.py4
9 files changed, 231 insertions, 232 deletions
diff --git a/numpy/typing/__init__.py b/numpy/typing/__init__.py
index 1bfdf07ae..04d34f0c7 100644
--- a/numpy/typing/__init__.py
+++ b/numpy/typing/__init__.py
@@ -164,11 +164,19 @@ API
from typing import TYPE_CHECKING, List
if TYPE_CHECKING:
- import sys
- if sys.version_info >= (3, 8):
- from typing import final
+ # typing_extensions is always available when type-checking
+ from typing_extensions import Literal as L
+ _HAS_TYPING_EXTENSIONS: L[True]
+else:
+ try:
+ import typing_extensions
+ except ImportError:
+ _HAS_TYPING_EXTENSIONS = False
else:
- from typing_extensions import final
+ _HAS_TYPING_EXTENSIONS = True
+
+if TYPE_CHECKING:
+ from typing_extensions import final
else:
def final(f): return f
@@ -204,14 +212,14 @@ class NBitBase:
.. code-block:: python
>>> from __future__ import annotations
- >>> from typing import TypeVar, Union, TYPE_CHECKING
+ >>> from typing import TypeVar, TYPE_CHECKING
>>> import numpy as np
>>> import numpy.typing as npt
>>> T1 = TypeVar("T1", bound=npt.NBitBase)
>>> T2 = TypeVar("T2", bound=npt.NBitBase)
- >>> def add(a: np.floating[T1], b: np.integer[T2]) -> np.floating[Union[T1, T2]]:
+ >>> def add(a: np.floating[T1], b: np.integer[T2]) -> np.floating[T1 | T2]:
... return a + b
>>> a = np.float16()
@@ -352,6 +360,7 @@ from ._array_like import (
)
from ._generic_alias import (
NDArray as NDArray,
+ _DType,
_GenericAlias,
)
diff --git a/numpy/typing/_array_like.py b/numpy/typing/_array_like.py
index 2283c98d7..2b823ecc0 100644
--- a/numpy/typing/_array_like.py
+++ b/numpy/typing/_array_like.py
@@ -21,23 +21,20 @@ from numpy import (
bytes_,
)
+from . import _HAS_TYPING_EXTENSIONS
+from ._dtype_like import DTypeLike
+
if sys.version_info >= (3, 8):
from typing import Protocol
- HAVE_PROTOCOL = True
-else:
- try:
- from typing_extensions import Protocol
- except ImportError:
- HAVE_PROTOCOL = False
- else:
- HAVE_PROTOCOL = True
+elif _HAS_TYPING_EXTENSIONS:
+ from typing_extensions import Protocol
_T = TypeVar("_T")
_ScalarType = TypeVar("_ScalarType", bound=generic)
_DType = TypeVar("_DType", bound="dtype[Any]")
_DType_co = TypeVar("_DType_co", covariant=True, bound="dtype[Any]")
-if TYPE_CHECKING or HAVE_PROTOCOL:
+if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS:
# The `_SupportsArray` protocol only cares about the default dtype
# (i.e. `dtype=None` or no `dtype` parameter at all) of the to-be returned
# array.
diff --git a/numpy/typing/_callable.py b/numpy/typing/_callable.py
index 8f838f1ae..54f9b1425 100644
--- a/numpy/typing/_callable.py
+++ b/numpy/typing/_callable.py
@@ -45,21 +45,15 @@ from ._scalars import (
_FloatLike_co,
_NumberLike_co,
)
-from . import NBitBase
+from . import NBitBase, _HAS_TYPING_EXTENSIONS
from ._generic_alias import NDArray
if sys.version_info >= (3, 8):
from typing import Protocol
- HAVE_PROTOCOL = True
-else:
- try:
- from typing_extensions import Protocol
- except ImportError:
- HAVE_PROTOCOL = False
- else:
- HAVE_PROTOCOL = True
-
-if TYPE_CHECKING or HAVE_PROTOCOL:
+elif _HAS_TYPING_EXTENSIONS:
+ from typing_extensions import Protocol
+
+if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS:
_T1 = TypeVar("_T1")
_T2 = TypeVar("_T2")
_2Tuple = Tuple[_T1, _T1]
@@ -108,7 +102,7 @@ if TYPE_CHECKING or HAVE_PROTOCOL:
class _BoolTrueDiv(Protocol):
@overload
- def __call__(self, __other: Union[float, _IntLike_co]) -> float64: ...
+ def __call__(self, __other: float | _IntLike_co) -> float64: ...
@overload
def __call__(self, __other: complex) -> complex128: ...
@overload
@@ -132,7 +126,7 @@ if TYPE_CHECKING or HAVE_PROTOCOL:
@overload # platform dependent
def __call__(self, __other: int) -> _2Tuple[int_]: ...
@overload
- def __call__(self, __other: float) -> _2Tuple[floating[Union[_NBit1, _NBitDouble]]]: ...
+ def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ...
@overload
def __call__(self, __other: _IntType) -> _2Tuple[_IntType]: ...
@overload
@@ -150,15 +144,15 @@ if TYPE_CHECKING or HAVE_PROTOCOL:
@overload
def __call__(self, __other: bool) -> floating[_NBit1]: ...
@overload
- def __call__(self, __other: int) -> floating[Union[_NBit1, _NBitInt]]: ...
+ def __call__(self, __other: int) -> floating[_NBit1 | _NBitInt]: ...
@overload
- def __call__(self, __other: float) -> floating[Union[_NBit1, _NBitDouble]]: ...
+ def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ...
@overload
def __call__(
self, __other: complex
- ) -> complexfloating[Union[_NBit1, _NBitDouble], Union[_NBit1, _NBitDouble]]: ...
+ ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
@overload
- def __call__(self, __other: integer[_NBit2]) -> floating[Union[_NBit1, _NBit2]]: ...
+ def __call__(self, __other: integer[_NBit2]) -> floating[_NBit1 | _NBit2]: ...
class _UnsignedIntOp(Protocol[_NBit1]):
# NOTE: `uint64 + signedinteger -> float64`
@@ -166,18 +160,18 @@ if TYPE_CHECKING or HAVE_PROTOCOL:
def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ...
@overload
def __call__(
- self, __other: Union[int, signedinteger[Any]]
+ self, __other: int | signedinteger[Any]
) -> Any: ...
@overload
- def __call__(self, __other: float) -> floating[Union[_NBit1, _NBitDouble]]: ...
+ def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ...
@overload
def __call__(
self, __other: complex
- ) -> complexfloating[Union[_NBit1, _NBitDouble], Union[_NBit1, _NBitDouble]]: ...
+ ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
@overload
def __call__(
self, __other: unsignedinteger[_NBit2]
- ) -> unsignedinteger[Union[_NBit1, _NBit2]]: ...
+ ) -> unsignedinteger[_NBit1 | _NBit2]: ...
class _UnsignedIntBitOp(Protocol[_NBit1]):
@overload
@@ -189,135 +183,135 @@ if TYPE_CHECKING or HAVE_PROTOCOL:
@overload
def __call__(
self, __other: unsignedinteger[_NBit2]
- ) -> unsignedinteger[Union[_NBit1, _NBit2]]: ...
+ ) -> unsignedinteger[_NBit1 | _NBit2]: ...
class _UnsignedIntMod(Protocol[_NBit1]):
@overload
def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ...
@overload
def __call__(
- self, __other: Union[int, signedinteger[Any]]
+ self, __other: int | signedinteger[Any]
) -> Any: ...
@overload
- def __call__(self, __other: float) -> floating[Union[_NBit1, _NBitDouble]]: ...
+ def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ...
@overload
def __call__(
self, __other: unsignedinteger[_NBit2]
- ) -> unsignedinteger[Union[_NBit1, _NBit2]]: ...
+ ) -> unsignedinteger[_NBit1 | _NBit2]: ...
class _UnsignedIntDivMod(Protocol[_NBit1]):
@overload
def __call__(self, __other: bool) -> _2Tuple[signedinteger[_NBit1]]: ...
@overload
def __call__(
- self, __other: Union[int, signedinteger[Any]]
+ self, __other: int | signedinteger[Any]
) -> _2Tuple[Any]: ...
@overload
- def __call__(self, __other: float) -> _2Tuple[floating[Union[_NBit1, _NBitDouble]]]: ...
+ def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ...
@overload
def __call__(
self, __other: unsignedinteger[_NBit2]
- ) -> _2Tuple[unsignedinteger[Union[_NBit1, _NBit2]]]: ...
+ ) -> _2Tuple[unsignedinteger[_NBit1 | _NBit2]]: ...
class _SignedIntOp(Protocol[_NBit1]):
@overload
def __call__(self, __other: bool) -> signedinteger[_NBit1]: ...
@overload
- def __call__(self, __other: int) -> signedinteger[Union[_NBit1, _NBitInt]]: ...
+ def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ...
@overload
- def __call__(self, __other: float) -> floating[Union[_NBit1, _NBitDouble]]: ...
+ def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ...
@overload
def __call__(
self, __other: complex
- ) -> complexfloating[Union[_NBit1, _NBitDouble], Union[_NBit1, _NBitDouble]]: ...
+ ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
@overload
def __call__(
self, __other: signedinteger[_NBit2]
- ) -> signedinteger[Union[_NBit1, _NBit2]]: ...
+ ) -> signedinteger[_NBit1 | _NBit2]: ...
class _SignedIntBitOp(Protocol[_NBit1]):
@overload
def __call__(self, __other: bool) -> signedinteger[_NBit1]: ...
@overload
- def __call__(self, __other: int) -> signedinteger[Union[_NBit1, _NBitInt]]: ...
+ def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ...
@overload
def __call__(
self, __other: signedinteger[_NBit2]
- ) -> signedinteger[Union[_NBit1, _NBit2]]: ...
+ ) -> signedinteger[_NBit1 | _NBit2]: ...
class _SignedIntMod(Protocol[_NBit1]):
@overload
def __call__(self, __other: bool) -> signedinteger[_NBit1]: ...
@overload
- def __call__(self, __other: int) -> signedinteger[Union[_NBit1, _NBitInt]]: ...
+ def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ...
@overload
- def __call__(self, __other: float) -> floating[Union[_NBit1, _NBitDouble]]: ...
+ def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ...
@overload
def __call__(
self, __other: signedinteger[_NBit2]
- ) -> signedinteger[Union[_NBit1, _NBit2]]: ...
+ ) -> signedinteger[_NBit1 | _NBit2]: ...
class _SignedIntDivMod(Protocol[_NBit1]):
@overload
def __call__(self, __other: bool) -> _2Tuple[signedinteger[_NBit1]]: ...
@overload
- def __call__(self, __other: int) -> _2Tuple[signedinteger[Union[_NBit1, _NBitInt]]]: ...
+ def __call__(self, __other: int) -> _2Tuple[signedinteger[_NBit1 | _NBitInt]]: ...
@overload
- def __call__(self, __other: float) -> _2Tuple[floating[Union[_NBit1, _NBitDouble]]]: ...
+ def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ...
@overload
def __call__(
self, __other: signedinteger[_NBit2]
- ) -> _2Tuple[signedinteger[Union[_NBit1, _NBit2]]]: ...
+ ) -> _2Tuple[signedinteger[_NBit1 | _NBit2]]: ...
class _FloatOp(Protocol[_NBit1]):
@overload
def __call__(self, __other: bool) -> floating[_NBit1]: ...
@overload
- def __call__(self, __other: int) -> floating[Union[_NBit1, _NBitInt]]: ...
+ def __call__(self, __other: int) -> floating[_NBit1 | _NBitInt]: ...
@overload
- def __call__(self, __other: float) -> floating[Union[_NBit1, _NBitDouble]]: ...
+ def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ...
@overload
def __call__(
self, __other: complex
- ) -> complexfloating[Union[_NBit1, _NBitDouble], Union[_NBit1, _NBitDouble]]: ...
+ ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
@overload
def __call__(
- self, __other: Union[integer[_NBit2], floating[_NBit2]]
- ) -> floating[Union[_NBit1, _NBit2]]: ...
+ self, __other: integer[_NBit2] | floating[_NBit2]
+ ) -> floating[_NBit1 | _NBit2]: ...
class _FloatMod(Protocol[_NBit1]):
@overload
def __call__(self, __other: bool) -> floating[_NBit1]: ...
@overload
- def __call__(self, __other: int) -> floating[Union[_NBit1, _NBitInt]]: ...
+ def __call__(self, __other: int) -> floating[_NBit1 | _NBitInt]: ...
@overload
- def __call__(self, __other: float) -> floating[Union[_NBit1, _NBitDouble]]: ...
+ def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ...
@overload
def __call__(
- self, __other: Union[integer[_NBit2], floating[_NBit2]]
- ) -> floating[Union[_NBit1, _NBit2]]: ...
+ self, __other: integer[_NBit2] | floating[_NBit2]
+ ) -> floating[_NBit1 | _NBit2]: ...
class _FloatDivMod(Protocol[_NBit1]):
@overload
def __call__(self, __other: bool) -> _2Tuple[floating[_NBit1]]: ...
@overload
- def __call__(self, __other: int) -> _2Tuple[floating[Union[_NBit1, _NBitInt]]]: ...
+ def __call__(self, __other: int) -> _2Tuple[floating[_NBit1 | _NBitInt]]: ...
@overload
- def __call__(self, __other: float) -> _2Tuple[floating[Union[_NBit1, _NBitDouble]]]: ...
+ def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ...
@overload
def __call__(
- self, __other: Union[integer[_NBit2], floating[_NBit2]]
- ) -> _2Tuple[floating[Union[_NBit1, _NBit2]]]: ...
+ self, __other: integer[_NBit2] | floating[_NBit2]
+ ) -> _2Tuple[floating[_NBit1 | _NBit2]]: ...
class _ComplexOp(Protocol[_NBit1]):
@overload
def __call__(self, __other: bool) -> complexfloating[_NBit1, _NBit1]: ...
@overload
- def __call__(self, __other: int) -> complexfloating[Union[_NBit1, _NBitInt], Union[_NBit1, _NBitInt]]: ...
+ def __call__(self, __other: int) -> complexfloating[_NBit1 | _NBitInt, _NBit1 | _NBitInt]: ...
@overload
def __call__(
- self, __other: Union[float, complex]
- ) -> complexfloating[Union[_NBit1, _NBitDouble], Union[_NBit1, _NBitDouble]]: ...
+ self, __other: complex
+ ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
@overload
def __call__(
self,
@@ -326,7 +320,7 @@ if TYPE_CHECKING or HAVE_PROTOCOL:
floating[_NBit2],
complexfloating[_NBit2, _NBit2],
]
- ) -> complexfloating[Union[_NBit1, _NBit2], Union[_NBit1, _NBit2]]: ...
+ ) -> complexfloating[_NBit1 | _NBit2, _NBit1 | _NBit2]: ...
class _NumberOp(Protocol):
def __call__(self, __other: _NumberLike_co) -> Any: ...
@@ -338,25 +332,25 @@ if TYPE_CHECKING or HAVE_PROTOCOL:
def __call__(self, __other: _T2) -> NDArray[bool_]: ...
else:
- _BoolOp = Any
- _BoolBitOp = Any
- _BoolSub = Any
- _BoolTrueDiv = Any
- _BoolMod = Any
- _BoolDivMod = Any
- _TD64Div = Any
- _IntTrueDiv = Any
- _UnsignedIntOp = Any
- _UnsignedIntBitOp = Any
- _UnsignedIntMod = Any
- _UnsignedIntDivMod = Any
- _SignedIntOp = Any
- _SignedIntBitOp = Any
- _SignedIntMod = Any
- _SignedIntDivMod = Any
- _FloatOp = Any
- _FloatMod = Any
- _FloatDivMod = Any
- _ComplexOp = Any
- _NumberOp = Any
- _ComparisonOp = Any
+ _BoolOp = NotImplemented
+ _BoolBitOp = NotImplemented
+ _BoolSub = NotImplemented
+ _BoolTrueDiv = NotImplemented
+ _BoolMod = NotImplemented
+ _BoolDivMod = NotImplemented
+ _TD64Div = NotImplemented
+ _IntTrueDiv = NotImplemented
+ _UnsignedIntOp = NotImplemented
+ _UnsignedIntBitOp = NotImplemented
+ _UnsignedIntMod = NotImplemented
+ _UnsignedIntDivMod = NotImplemented
+ _SignedIntOp = NotImplemented
+ _SignedIntBitOp = NotImplemented
+ _SignedIntMod = NotImplemented
+ _SignedIntDivMod = NotImplemented
+ _FloatOp = NotImplemented
+ _FloatMod = NotImplemented
+ _FloatDivMod = NotImplemented
+ _ComplexOp = NotImplemented
+ _NumberOp = NotImplemented
+ _ComparisonOp = NotImplemented
diff --git a/numpy/typing/_char_codes.py b/numpy/typing/_char_codes.py
index 6b6f7ae88..6b33f995d 100644
--- a/numpy/typing/_char_codes.py
+++ b/numpy/typing/_char_codes.py
@@ -1,18 +1,14 @@
import sys
from typing import Any, TYPE_CHECKING
+from . import _HAS_TYPING_EXTENSIONS
+
if sys.version_info >= (3, 8):
from typing import Literal
- HAVE_LITERAL = True
-else:
- try:
- from typing_extensions import Literal
- except ImportError:
- HAVE_LITERAL = False
- else:
- HAVE_LITERAL = True
-
-if TYPE_CHECKING or HAVE_LITERAL:
+elif _HAS_TYPING_EXTENSIONS:
+ from typing_extensions import Literal
+
+if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS:
_BoolCodes = Literal["?", "=?", "<?", ">?", "bool", "bool_", "bool8"]
_UInt8Codes = Literal["uint8", "u1", "=u1", "<u1", ">u1"]
@@ -124,52 +120,52 @@ if TYPE_CHECKING or HAVE_LITERAL:
]
else:
- _BoolCodes = Any
-
- _UInt8Codes = Any
- _UInt16Codes = Any
- _UInt32Codes = Any
- _UInt64Codes = Any
-
- _Int8Codes = Any
- _Int16Codes = Any
- _Int32Codes = Any
- _Int64Codes = Any
-
- _Float16Codes = Any
- _Float32Codes = Any
- _Float64Codes = Any
-
- _Complex64Codes = Any
- _Complex128Codes = Any
-
- _ByteCodes = Any
- _ShortCodes = Any
- _IntCCodes = Any
- _IntPCodes = Any
- _IntCodes = Any
- _LongLongCodes = Any
-
- _UByteCodes = Any
- _UShortCodes = Any
- _UIntCCodes = Any
- _UIntPCodes = Any
- _UIntCodes = Any
- _ULongLongCodes = Any
-
- _HalfCodes = Any
- _SingleCodes = Any
- _DoubleCodes = Any
- _LongDoubleCodes = Any
-
- _CSingleCodes = Any
- _CDoubleCodes = Any
- _CLongDoubleCodes = Any
-
- _StrCodes = Any
- _BytesCodes = Any
- _VoidCodes = Any
- _ObjectCodes = Any
-
- _DT64Codes = Any
- _TD64Codes = Any
+ _BoolCodes = NotImplemented
+
+ _UInt8Codes = NotImplemented
+ _UInt16Codes = NotImplemented
+ _UInt32Codes = NotImplemented
+ _UInt64Codes = NotImplemented
+
+ _Int8Codes = NotImplemented
+ _Int16Codes = NotImplemented
+ _Int32Codes = NotImplemented
+ _Int64Codes = NotImplemented
+
+ _Float16Codes = NotImplemented
+ _Float32Codes = NotImplemented
+ _Float64Codes = NotImplemented
+
+ _Complex64Codes = NotImplemented
+ _Complex128Codes = NotImplemented
+
+ _ByteCodes = NotImplemented
+ _ShortCodes = NotImplemented
+ _IntCCodes = NotImplemented
+ _IntPCodes = NotImplemented
+ _IntCodes = NotImplemented
+ _LongLongCodes = NotImplemented
+
+ _UByteCodes = NotImplemented
+ _UShortCodes = NotImplemented
+ _UIntCCodes = NotImplemented
+ _UIntPCodes = NotImplemented
+ _UIntCodes = NotImplemented
+ _ULongLongCodes = NotImplemented
+
+ _HalfCodes = NotImplemented
+ _SingleCodes = NotImplemented
+ _DoubleCodes = NotImplemented
+ _LongDoubleCodes = NotImplemented
+
+ _CSingleCodes = NotImplemented
+ _CDoubleCodes = NotImplemented
+ _CLongDoubleCodes = NotImplemented
+
+ _StrCodes = NotImplemented
+ _BytesCodes = NotImplemented
+ _VoidCodes = NotImplemented
+ _ObjectCodes = NotImplemented
+
+ _DT64Codes = NotImplemented
+ _TD64Codes = NotImplemented
diff --git a/numpy/typing/_dtype_like.py b/numpy/typing/_dtype_like.py
index a41e2f358..405cc4a3c 100644
--- a/numpy/typing/_dtype_like.py
+++ b/numpy/typing/_dtype_like.py
@@ -2,18 +2,20 @@ import sys
from typing import Any, List, Sequence, Tuple, Union, Type, TypeVar, TYPE_CHECKING
import numpy as np
+
+from . import _HAS_TYPING_EXTENSIONS
from ._shape import _ShapeLike
+from ._generic_alias import _DType as DType
if sys.version_info >= (3, 8):
from typing import Protocol, TypedDict
- HAVE_PROTOCOL = True
+elif _HAS_TYPING_EXTENSIONS:
+ from typing_extensions import Protocol, TypedDict
+
+if sys.version_info >= (3, 9):
+ from types import GenericAlias
else:
- try:
- from typing_extensions import Protocol, TypedDict
- except ImportError:
- HAVE_PROTOCOL = False
- else:
- HAVE_PROTOCOL = True
+ from ._generic_alias import _GenericAlias as GenericAlias
from ._char_codes import (
_BoolCodes,
@@ -58,8 +60,9 @@ from ._char_codes import (
)
_DTypeLikeNested = Any # TODO: wait for support for recursive types
+_DType_co = TypeVar("_DType_co", covariant=True, bound=DType[Any])
-if TYPE_CHECKING or HAVE_PROTOCOL:
+if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS:
# Mandatory keys
class _DTypeDictBase(TypedDict):
names: Sequence[str]
@@ -72,16 +75,16 @@ if TYPE_CHECKING or HAVE_PROTOCOL:
itemsize: int
aligned: bool
- _DType_co = TypeVar("_DType_co", covariant=True, bound=np.dtype)
-
# A protocol for anything with the dtype attribute
class _SupportsDType(Protocol[_DType_co]):
@property
def dtype(self) -> _DType_co: ...
else:
- _DTypeDict = Any
- _SupportsDType = Any
+ _DTypeDict = NotImplemented
+
+ class _SupportsDType: ...
+ _SupportsDType = GenericAlias(_SupportsDType, _DType_co)
# Would create a dtype[np.void]
@@ -106,13 +109,13 @@ _VoidDTypeLike = Union[
# Anything that can be coerced into numpy.dtype.
# Reference: https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html
DTypeLike = Union[
- np.dtype,
+ DType[Any],
# default data type (float64)
None,
# array-scalar types and generic types
- type, # TODO: enumerate these when we add type hints for numpy scalars
+ Type[Any], # TODO: enumerate these when we add type hints for numpy scalars
# anything with a dtype attribute
- "_SupportsDType[np.dtype[Any]]",
+ _SupportsDType[DType[Any]],
# character codes, type strings or comma-separated fields, e.g., 'float64'
str,
_VoidDTypeLike,
@@ -130,14 +133,14 @@ DTypeLike = Union[
_DTypeLikeBool = Union[
Type[bool],
Type[np.bool_],
- "np.dtype[np.bool_]",
- "_SupportsDType[np.dtype[np.bool_]]",
+ DType[np.bool_],
+ _SupportsDType[DType[np.bool_]],
_BoolCodes,
]
_DTypeLikeUInt = Union[
Type[np.unsignedinteger],
- "np.dtype[np.unsignedinteger]",
- "_SupportsDType[np.dtype[np.unsignedinteger]]",
+ DType[np.unsignedinteger],
+ _SupportsDType[DType[np.unsignedinteger]],
_UInt8Codes,
_UInt16Codes,
_UInt32Codes,
@@ -152,8 +155,8 @@ _DTypeLikeUInt = Union[
_DTypeLikeInt = Union[
Type[int],
Type[np.signedinteger],
- "np.dtype[np.signedinteger]",
- "_SupportsDType[np.dtype[np.signedinteger]]",
+ DType[np.signedinteger],
+ _SupportsDType[DType[np.signedinteger]],
_Int8Codes,
_Int16Codes,
_Int32Codes,
@@ -168,8 +171,8 @@ _DTypeLikeInt = Union[
_DTypeLikeFloat = Union[
Type[float],
Type[np.floating],
- "np.dtype[np.floating]",
- "_SupportsDType[np.dtype[np.floating]]",
+ DType[np.floating],
+ _SupportsDType[DType[np.floating]],
_Float16Codes,
_Float32Codes,
_Float64Codes,
@@ -181,8 +184,8 @@ _DTypeLikeFloat = Union[
_DTypeLikeComplex = Union[
Type[complex],
Type[np.complexfloating],
- "np.dtype[np.complexfloating]",
- "_SupportsDType[np.dtype[np.complexfloating]]",
+ DType[np.complexfloating],
+ _SupportsDType[DType[np.complexfloating]],
_Complex64Codes,
_Complex128Codes,
_CSingleCodes,
@@ -191,41 +194,41 @@ _DTypeLikeComplex = Union[
]
_DTypeLikeDT64 = Union[
Type[np.timedelta64],
- "np.dtype[np.timedelta64]",
- "_SupportsDType[np.dtype[np.timedelta64]]",
+ DType[np.timedelta64],
+ _SupportsDType[DType[np.timedelta64]],
_TD64Codes,
]
_DTypeLikeTD64 = Union[
Type[np.datetime64],
- "np.dtype[np.datetime64]",
- "_SupportsDType[np.dtype[np.datetime64]]",
+ DType[np.datetime64],
+ _SupportsDType[DType[np.datetime64]],
_DT64Codes,
]
_DTypeLikeStr = Union[
Type[str],
Type[np.str_],
- "np.dtype[np.str_]",
- "_SupportsDType[np.dtype[np.str_]]",
+ DType[np.str_],
+ _SupportsDType[DType[np.str_]],
_StrCodes,
]
_DTypeLikeBytes = Union[
Type[bytes],
Type[np.bytes_],
- "np.dtype[np.bytes_]",
- "_SupportsDType[np.dtype[np.bytes_]]",
+ DType[np.bytes_],
+ _SupportsDType[DType[np.bytes_]],
_BytesCodes,
]
_DTypeLikeVoid = Union[
Type[np.void],
- "np.dtype[np.void]",
- "_SupportsDType[np.dtype[np.void]]",
+ DType[np.void],
+ _SupportsDType[DType[np.void]],
_VoidCodes,
_VoidDTypeLike,
]
_DTypeLikeObject = Union[
type,
- "np.dtype[np.object_]",
- "_SupportsDType[np.dtype[np.object_]]",
+ DType[np.object_],
+ _SupportsDType[DType[np.object_]],
_ObjectCodes,
]
diff --git a/numpy/typing/_generic_alias.py b/numpy/typing/_generic_alias.py
index 68523827a..0d30f54ca 100644
--- a/numpy/typing/_generic_alias.py
+++ b/numpy/typing/_generic_alias.py
@@ -199,6 +199,7 @@ else:
ScalarType = TypeVar("ScalarType", bound=np.generic, covariant=True)
if TYPE_CHECKING:
+ _DType = np.dtype[ScalarType]
NDArray = np.ndarray[Any, np.dtype[ScalarType]]
elif sys.version_info >= (3, 9):
_DType = types.GenericAlias(np.dtype, (ScalarType,))
diff --git a/numpy/typing/_shape.py b/numpy/typing/_shape.py
index b720c3ffc..0742be8a9 100644
--- a/numpy/typing/_shape.py
+++ b/numpy/typing/_shape.py
@@ -1,13 +1,14 @@
import sys
from typing import Sequence, Tuple, Union
+from . import _HAS_TYPING_EXTENSIONS
+
if sys.version_info >= (3, 8):
from typing import SupportsIndex
+elif _HAS_TYPING_EXTENSIONS:
+ from typing_extensions import SupportsIndex
else:
- try:
- from typing_extensions import SupportsIndex
- except ImportError:
- SupportsIndex = NotImplemented
+ SupportsIndex = NotImplemented
_Shape = Tuple[int, ...]
diff --git a/numpy/typing/_ufunc.pyi b/numpy/typing/_ufunc.pyi
index b3b9fa95e..f4fead504 100644
--- a/numpy/typing/_ufunc.pyi
+++ b/numpy/typing/_ufunc.pyi
@@ -11,11 +11,9 @@ from typing import (
Any,
Generic,
List,
- Optional,
overload,
Tuple,
TypeVar,
- Union,
)
from numpy import ufunc, _Casting, _OrderKACF
@@ -82,26 +80,26 @@ class _UFunc_Nin1_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]):
__x1: _ScalarLike_co,
out: None = ...,
*,
- where: Optional[_ArrayLikeBool_co] = ...,
+ where: None | _ArrayLikeBool_co = ...,
casting: _Casting = ...,
order: _OrderKACF = ...,
dtype: DTypeLike = ...,
subok: bool = ...,
- signature: Union[str, _2Tuple[Optional[str]]] = ...,
+ signature: str | _2Tuple[None | str] = ...,
extobj: List[Any] = ...,
) -> Any: ...
@overload
def __call__(
self,
__x1: ArrayLike,
- out: Union[None, NDArray[Any], Tuple[NDArray[Any]]] = ...,
+ out: None | NDArray[Any] | Tuple[NDArray[Any]] = ...,
*,
- where: Optional[_ArrayLikeBool_co] = ...,
+ where: None | _ArrayLikeBool_co = ...,
casting: _Casting = ...,
order: _OrderKACF = ...,
dtype: DTypeLike = ...,
subok: bool = ...,
- signature: Union[str, _2Tuple[Optional[str]]] = ...,
+ signature: str | _2Tuple[None | str] = ...,
extobj: List[Any] = ...,
) -> NDArray[Any]: ...
@@ -134,12 +132,12 @@ class _UFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]):
__x2: _ScalarLike_co,
out: None = ...,
*,
- where: Optional[_ArrayLikeBool_co] = ...,
+ where: None | _ArrayLikeBool_co = ...,
casting: _Casting = ...,
order: _OrderKACF = ...,
dtype: DTypeLike = ...,
subok: bool = ...,
- signature: Union[str, _3Tuple[Optional[str]]] = ...,
+ signature: str | _3Tuple[None | str] = ...,
extobj: List[Any] = ...,
) -> Any: ...
@overload
@@ -147,14 +145,14 @@ class _UFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]):
self,
__x1: ArrayLike,
__x2: ArrayLike,
- out: Union[None, NDArray[Any], Tuple[NDArray[Any]]] = ...,
+ out: None | NDArray[Any] | Tuple[NDArray[Any]] = ...,
*,
- where: Optional[_ArrayLikeBool_co] = ...,
+ where: None | _ArrayLikeBool_co = ...,
casting: _Casting = ...,
order: _OrderKACF = ...,
dtype: DTypeLike = ...,
subok: bool = ...,
- signature: Union[str, _3Tuple[Optional[str]]] = ...,
+ signature: str | _3Tuple[None | str] = ...,
extobj: List[Any] = ...,
) -> NDArray[Any]: ...
@@ -168,9 +166,9 @@ class _UFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]):
def reduce(
self,
array: ArrayLike,
- axis: Optional[_ShapeLike] = ...,
+ axis: None | _ShapeLike = ...,
dtype: DTypeLike = ...,
- out: Optional[NDArray[Any]] = ...,
+ out: None | NDArray[Any] = ...,
keepdims: bool = ...,
initial: Any = ...,
where: _ArrayLikeBool_co = ...,
@@ -181,7 +179,7 @@ class _UFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]):
array: ArrayLike,
axis: SupportsIndex = ...,
dtype: DTypeLike = ...,
- out: Optional[NDArray[Any]] = ...,
+ out: None | NDArray[Any] = ...,
) -> NDArray[Any]: ...
def reduceat(
@@ -190,7 +188,7 @@ class _UFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]):
indices: _ArrayLikeInt_co,
axis: SupportsIndex = ...,
dtype: DTypeLike = ...,
- out: Optional[NDArray[Any]] = ...,
+ out: None | NDArray[Any] = ...,
) -> NDArray[Any]: ...
# Expand `**kwargs` into explicit keyword-only arguments
@@ -201,12 +199,12 @@ class _UFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]):
__B: _ScalarLike_co,
*,
out: None = ...,
- where: Optional[_ArrayLikeBool_co] = ...,
+ where: None | _ArrayLikeBool_co = ...,
casting: _Casting = ...,
order: _OrderKACF = ...,
dtype: DTypeLike = ...,
subok: bool = ...,
- signature: Union[str, _3Tuple[Optional[str]]] = ...,
+ signature: str | _3Tuple[None | str] = ...,
extobj: List[Any] = ...,
) -> Any: ...
@overload
@@ -215,13 +213,13 @@ class _UFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]):
__A: ArrayLike,
__B: ArrayLike,
*,
- out: Union[None, NDArray[Any], Tuple[NDArray[Any]]] = ...,
- where: Optional[_ArrayLikeBool_co] = ...,
+ out: None | NDArray[Any] | Tuple[NDArray[Any]] = ...,
+ where: None | _ArrayLikeBool_co = ...,
casting: _Casting = ...,
order: _OrderKACF = ...,
dtype: DTypeLike = ...,
subok: bool = ...,
- signature: Union[str, _3Tuple[Optional[str]]] = ...,
+ signature: str | _3Tuple[None | str] = ...,
extobj: List[Any] = ...,
) -> NDArray[Any]: ...
@@ -258,28 +256,28 @@ class _UFunc_Nin1_Nout2(ufunc, Generic[_NameType, _NTypes, _IDType]):
__out1: None = ...,
__out2: None = ...,
*,
- where: Optional[_ArrayLikeBool_co] = ...,
+ where: None | _ArrayLikeBool_co = ...,
casting: _Casting = ...,
order: _OrderKACF = ...,
dtype: DTypeLike = ...,
subok: bool = ...,
- signature: Union[str, _3Tuple[Optional[str]]] = ...,
+ signature: str | _3Tuple[None | str] = ...,
extobj: List[Any] = ...,
) -> _2Tuple[Any]: ...
@overload
def __call__(
self,
__x1: ArrayLike,
- __out1: Optional[NDArray[Any]] = ...,
- __out2: Optional[NDArray[Any]] = ...,
+ __out1: None | NDArray[Any] = ...,
+ __out2: None | NDArray[Any] = ...,
*,
out: _2Tuple[NDArray[Any]] = ...,
- where: Optional[_ArrayLikeBool_co] = ...,
+ where: None | _ArrayLikeBool_co = ...,
casting: _Casting = ...,
order: _OrderKACF = ...,
dtype: DTypeLike = ...,
subok: bool = ...,
- signature: Union[str, _3Tuple[Optional[str]]] = ...,
+ signature: str | _3Tuple[None | str] = ...,
extobj: List[Any] = ...,
) -> _2Tuple[NDArray[Any]]: ...
@@ -317,12 +315,12 @@ class _UFunc_Nin2_Nout2(ufunc, Generic[_NameType, _NTypes, _IDType]):
__out1: None = ...,
__out2: None = ...,
*,
- where: Optional[_ArrayLikeBool_co] = ...,
+ where: None | _ArrayLikeBool_co = ...,
casting: _Casting = ...,
order: _OrderKACF = ...,
dtype: DTypeLike = ...,
subok: bool = ...,
- signature: Union[str, _4Tuple[Optional[str]]] = ...,
+ signature: str | _4Tuple[None | str] = ...,
extobj: List[Any] = ...,
) -> _2Tuple[Any]: ...
@overload
@@ -330,16 +328,16 @@ class _UFunc_Nin2_Nout2(ufunc, Generic[_NameType, _NTypes, _IDType]):
self,
__x1: ArrayLike,
__x2: ArrayLike,
- __out1: Optional[NDArray[Any]] = ...,
- __out2: Optional[NDArray[Any]] = ...,
+ __out1: None | NDArray[Any] = ...,
+ __out2: None | NDArray[Any] = ...,
*,
out: _2Tuple[NDArray[Any]] = ...,
- where: Optional[_ArrayLikeBool_co] = ...,
+ where: None | _ArrayLikeBool_co = ...,
casting: _Casting = ...,
order: _OrderKACF = ...,
dtype: DTypeLike = ...,
subok: bool = ...,
- signature: Union[str, _4Tuple[Optional[str]]] = ...,
+ signature: str | _4Tuple[None | str] = ...,
extobj: List[Any] = ...,
) -> _2Tuple[NDArray[Any]]: ...
@@ -384,7 +382,7 @@ class _GUFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]):
order: _OrderKACF = ...,
dtype: DTypeLike = ...,
subok: bool = ...,
- signature: Union[str, _3Tuple[Optional[str]]] = ...,
+ signature: str | _3Tuple[None | str] = ...,
extobj: List[Any] = ...,
axes: List[_2Tuple[SupportsIndex]] = ...,
) -> Any: ...
@@ -393,13 +391,13 @@ class _GUFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]):
self,
__x1: ArrayLike,
__x2: ArrayLike,
- out: Union[NDArray[Any], Tuple[NDArray[Any]]],
+ out: NDArray[Any] | Tuple[NDArray[Any]],
*,
casting: _Casting = ...,
order: _OrderKACF = ...,
dtype: DTypeLike = ...,
subok: bool = ...,
- signature: Union[str, _3Tuple[Optional[str]]] = ...,
+ signature: str | _3Tuple[None | str] = ...,
extobj: List[Any] = ...,
axes: List[_2Tuple[SupportsIndex]] = ...,
) -> NDArray[Any]: ...
diff --git a/numpy/typing/mypy_plugin.py b/numpy/typing/mypy_plugin.py
index 901bf4fb1..100e0d957 100644
--- a/numpy/typing/mypy_plugin.py
+++ b/numpy/typing/mypy_plugin.py
@@ -14,7 +14,7 @@ try:
from mypy.build import PRI_MED
_HookFunc = t.Callable[[AnalyzeTypeContext], Type]
- MYPY_EX: t.Optional[ModuleNotFoundError] = None
+ MYPY_EX: None | ModuleNotFoundError = None
except ModuleNotFoundError as ex:
MYPY_EX = ex
@@ -90,7 +90,7 @@ if t.TYPE_CHECKING or MYPY_EX is None:
class _NumpyPlugin(Plugin):
"""A plugin for assigning platform-specific `numpy.number` precisions."""
- def get_type_analyze_hook(self, fullname: str) -> t.Optional[_HookFunc]:
+ def get_type_analyze_hook(self, fullname: str) -> None | _HookFunc:
"""Set the precision of platform-specific `numpy.number` subclasses.
For example: `numpy.int_`, `numpy.longlong` and `numpy.longdouble`.