diff options
author | BvB93 <43369155+BvB93@users.noreply.github.com> | 2023-02-02 17:46:02 +0100 |
---|---|---|
committer | BvB93 <43369155+BvB93@users.noreply.github.com> | 2023-02-02 18:27:49 +0100 |
commit | c4c523296cdffb4e5f8c4cf6314d0bb177735e47 (patch) | |
tree | bfcc6422e2c7743ee81e3abc9059edf293cb9ffd | |
parent | c1b655fa7d10cb22bee793290b731681afb8b356 (diff) | |
download | numpy-c4c523296cdffb4e5f8c4cf6314d0bb177735e47.tar.gz |
MAINT: Make use of the py39 `__class_getitem__` availability in the standard library
-rw-r--r-- | numpy/_typing/_array_like.py | 53 | ||||
-rw-r--r-- | numpy/_typing/_dtype_like.py | 103 | ||||
-rw-r--r-- | numpy/_typing/_nested_sequence.py | 2 | ||||
-rw-r--r-- | numpy/_typing/_scalars.py | 4 | ||||
-rw-r--r-- | numpy/_typing/_shape.py | 5 |
5 files changed, 84 insertions, 83 deletions
diff --git a/numpy/_typing/_array_like.py b/numpy/_typing/_array_like.py index 67d67ce19..6dbe86c68 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,8 @@ 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]") +_DType = TypeVar("_DType", bound=dtype[Any]) +_DType_co = TypeVar("_DType_co", covariant=True, bound=dtype[Any]) # 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 +59,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: @@ -90,57 +88,62 @@ ArrayLike = _DualArrayLike[ # `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 +156,6 @@ class _UnknownType: _ArrayLikeUnknown = _DualArrayLike[ - "dtype[_UnknownType]", + dtype[_UnknownType], _UnknownType, ] diff --git a/numpy/_typing/_dtype_like.py b/numpy/_typing/_dtype_like.py index e92e17dd2..207a99c56 100644 --- a/numpy/_typing/_dtype_like.py +++ b/numpy/_typing/_dtype_like.py @@ -1,10 +1,8 @@ +from collections.abc import Sequence from typing import ( Any, - List, Sequence, - Tuple, Union, - Type, TypeVar, Protocol, TypedDict, @@ -14,7 +12,6 @@ from typing import ( import numpy as np from ._shape import _ShapeLike -from ._generic_alias import _DType as DType from ._char_codes import ( _BoolCodes, @@ -59,7 +56,7 @@ from ._char_codes import ( ) _SCT = TypeVar("_SCT", bound=np.generic) -_DType_co = TypeVar("_DType_co", covariant=True, bound=DType[Any]) +_DType_co = TypeVar("_DType_co", covariant=True, bound=np.dtype[Any]) _DTypeLikeNested = Any # TODO: wait for support for recursive types @@ -89,41 +86,41 @@ class _SupportsDType(Protocol[_DType_co]): # A subset of `npt.DTypeLike` that can be parametrized w.r.t. `np.generic` _DTypeLike = Union[ - "np.dtype[_SCT]", - Type[_SCT], - _SupportsDType["np.dtype[_SCT]"], + np.dtype[_SCT], + type[_SCT], + _SupportsDType[np.dtype[_SCT]], ] # Would create a dtype[np.void] _VoidDTypeLike = Union[ # (flexible_dtype, itemsize) - Tuple[_DTypeLikeNested, int], + tuple[_DTypeLikeNested, int], # (fixed_dtype, shape) - Tuple[_DTypeLikeNested, _ShapeLike], + tuple[_DTypeLikeNested, _ShapeLike], # [(field_name, field_dtype, field_shape), ...] # # The type here is quite broad because NumPy accepts quite a wide # range of inputs inside the list; see the tests for some # examples. - List[Any], + list[Any], # {'names': ..., 'formats': ..., 'offsets': ..., 'titles': ..., # 'itemsize': ...} _DTypeDict, # (base_dtype, new_dtype) - Tuple[_DTypeLikeNested, _DTypeLikeNested], + tuple[_DTypeLikeNested, _DTypeLikeNested], ] # Anything that can be coerced into numpy.dtype. # Reference: https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html DTypeLike = Union[ - DType[Any], + np.dtype[Any], # default data type (float64) None, # array-scalar types and generic types - Type[Any], # NOTE: We're stuck with `Type[Any]` due to object dtypes + type[Any], # NOTE: We're stuck with `type[Any]` due to object dtypes # anything with a dtype attribute - _SupportsDType[DType[Any]], + _SupportsDType[np.dtype[Any]], # character codes, type strings or comma-separated fields, e.g., 'float64' str, _VoidDTypeLike, @@ -139,16 +136,16 @@ DTypeLike = Union[ # Aliases for commonly used dtype-like objects. # Note that the precision of `np.number` subclasses is ignored herein. _DTypeLikeBool = Union[ - Type[bool], - Type[np.bool_], - DType[np.bool_], - _SupportsDType[DType[np.bool_]], + type[bool], + type[np.bool_], + np.dtype[np.bool_], + _SupportsDType[np.dtype[np.bool_]], _BoolCodes, ] _DTypeLikeUInt = Union[ - Type[np.unsignedinteger], - DType[np.unsignedinteger], - _SupportsDType[DType[np.unsignedinteger]], + type[np.unsignedinteger], + np.dtype[np.unsignedinteger], + _SupportsDType[np.dtype[np.unsignedinteger]], _UInt8Codes, _UInt16Codes, _UInt32Codes, @@ -161,10 +158,10 @@ _DTypeLikeUInt = Union[ _ULongLongCodes, ] _DTypeLikeInt = Union[ - Type[int], - Type[np.signedinteger], - DType[np.signedinteger], - _SupportsDType[DType[np.signedinteger]], + type[int], + type[np.signedinteger], + np.dtype[np.signedinteger], + _SupportsDType[np.dtype[np.signedinteger]], _Int8Codes, _Int16Codes, _Int32Codes, @@ -177,10 +174,10 @@ _DTypeLikeInt = Union[ _LongLongCodes, ] _DTypeLikeFloat = Union[ - Type[float], - Type[np.floating], - DType[np.floating], - _SupportsDType[DType[np.floating]], + type[float], + type[np.floating], + np.dtype[np.floating], + _SupportsDType[np.dtype[np.floating]], _Float16Codes, _Float32Codes, _Float64Codes, @@ -190,10 +187,10 @@ _DTypeLikeFloat = Union[ _LongDoubleCodes, ] _DTypeLikeComplex = Union[ - Type[complex], - Type[np.complexfloating], - DType[np.complexfloating], - _SupportsDType[DType[np.complexfloating]], + type[complex], + type[np.complexfloating], + np.dtype[np.complexfloating], + _SupportsDType[np.dtype[np.complexfloating]], _Complex64Codes, _Complex128Codes, _CSingleCodes, @@ -201,42 +198,42 @@ _DTypeLikeComplex = Union[ _CLongDoubleCodes, ] _DTypeLikeDT64 = Union[ - Type[np.timedelta64], - DType[np.timedelta64], - _SupportsDType[DType[np.timedelta64]], + type[np.timedelta64], + np.dtype[np.timedelta64], + _SupportsDType[np.dtype[np.timedelta64]], _TD64Codes, ] _DTypeLikeTD64 = Union[ - Type[np.datetime64], - DType[np.datetime64], - _SupportsDType[DType[np.datetime64]], + type[np.datetime64], + np.dtype[np.datetime64], + _SupportsDType[np.dtype[np.datetime64]], _DT64Codes, ] _DTypeLikeStr = Union[ - Type[str], - Type[np.str_], - DType[np.str_], - _SupportsDType[DType[np.str_]], + type[str], + type[np.str_], + np.dtype[np.str_], + _SupportsDType[np.dtype[np.str_]], _StrCodes, ] _DTypeLikeBytes = Union[ - Type[bytes], - Type[np.bytes_], - DType[np.bytes_], - _SupportsDType[DType[np.bytes_]], + type[bytes], + type[np.bytes_], + np.dtype[np.bytes_], + _SupportsDType[np.dtype[np.bytes_]], _BytesCodes, ] _DTypeLikeVoid = Union[ - Type[np.void], - DType[np.void], - _SupportsDType[DType[np.void]], + type[np.void], + np.dtype[np.void], + _SupportsDType[np.dtype[np.void]], _VoidCodes, _VoidDTypeLike, ] _DTypeLikeObject = Union[ type, - DType[np.object_], - _SupportsDType[DType[np.object_]], + np.dtype[np.object_], + _SupportsDType[np.dtype[np.object_]], _ObjectCodes, ] diff --git a/numpy/_typing/_nested_sequence.py b/numpy/_typing/_nested_sequence.py index 789bf3844..4b6cafc51 100644 --- a/numpy/_typing/_nested_sequence.py +++ b/numpy/_typing/_nested_sequence.py @@ -2,9 +2,9 @@ from __future__ import annotations +from collections.abc import Iterator from typing import ( Any, - Iterator, overload, TypeVar, Protocol, diff --git a/numpy/_typing/_scalars.py b/numpy/_typing/_scalars.py index 516b996dc..1ea88e957 100644 --- a/numpy/_typing/_scalars.py +++ b/numpy/_typing/_scalars.py @@ -1,4 +1,4 @@ -from typing import Union, Tuple, Any +from typing import Union, Any import numpy as np @@ -27,4 +27,4 @@ _ScalarLike_co = Union[ ] # `_VoidLike_co` is technically not a scalar, but it's close enough -_VoidLike_co = Union[Tuple[Any, ...], np.void] +_VoidLike_co = Union[tuple[Any, ...], np.void] diff --git a/numpy/_typing/_shape.py b/numpy/_typing/_shape.py index c28859b19..4f1204e47 100644 --- a/numpy/_typing/_shape.py +++ b/numpy/_typing/_shape.py @@ -1,6 +1,7 @@ -from typing import Sequence, Tuple, Union, SupportsIndex +from collections.abc import Sequence +from typing import Union, SupportsIndex -_Shape = Tuple[int, ...] +_Shape = tuple[int, ...] # Anything that can be coerced to a shape tuple _ShapeLike = Union[SupportsIndex, Sequence[SupportsIndex]] |