Skip to content

Commit

Permalink
Merge pull request numpy#23145 from BvB93/39_typ
Browse files Browse the repository at this point in the history
TYP,MAINT: Remove typing-related Python <3.9 leftovers
  • Loading branch information
charris authored Feb 2, 2023
2 parents 1e58f02 + cc5aaf2 commit 76ee503
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 630 deletions.
9 changes: 3 additions & 6 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -846,8 +846,7 @@ class dtype(Generic[_DTypeScalar_co]):
copy: bool = ...,
) -> dtype[object_]: ...

if sys.version_info >= (3, 9):
def __class_getitem__(self, item: Any) -> GenericAlias: ...
def __class_getitem__(self, item: Any) -> GenericAlias: ...

@overload
def __getitem__(self: dtype[void], key: list[builtins.str]) -> dtype[void]: ...
Expand Down Expand Up @@ -1510,8 +1509,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
order: _OrderKACF = ...,
) -> _ArraySelf: ...

if sys.version_info >= (3, 9):
def __class_getitem__(self, item: Any) -> GenericAlias: ...
def __class_getitem__(self, item: Any) -> GenericAlias: ...

@overload
def __array__(self, dtype: None = ..., /) -> ndarray[Any, _DType_co]: ...
Expand Down Expand Up @@ -2674,8 +2672,7 @@ class number(generic, Generic[_NBit1]): # type: ignore
def real(self: _ArraySelf) -> _ArraySelf: ...
@property
def imag(self: _ArraySelf) -> _ArraySelf: ...
if sys.version_info >= (3, 9):
def __class_getitem__(self, item: Any) -> GenericAlias: ...
def __class_getitem__(self, item: Any) -> GenericAlias: ...
def __int__(self) -> int: ...
def __float__(self) -> float: ...
def __complex__(self) -> complex: ...
Expand Down
6 changes: 1 addition & 5 deletions numpy/_typing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class _8Bit(_16Bit): # type: ignore[misc]
_DTypeLikeComplex_co as _DTypeLikeComplex_co,
)
from ._array_like import (
NDArray as NDArray,
ArrayLike as ArrayLike,
_ArrayLike as _ArrayLike,
_FiniteNestedSequence as _FiniteNestedSequence,
Expand All @@ -201,11 +202,6 @@ class _8Bit(_16Bit): # type: ignore[misc]
_ArrayLikeUnknown as _ArrayLikeUnknown,
_UnknownType as _UnknownType,
)
from ._generic_alias import (
NDArray as NDArray,
_DType as _DType,
_GenericAlias as _GenericAlias,
)

if TYPE_CHECKING:
from ._ufunc import (
Expand Down
2 changes: 1 addition & 1 deletion numpy/_typing/_add_docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import textwrap

from ._generic_alias import NDArray
from ._array_like import NDArray

_docstrings_list = []

Expand Down
56 changes: 31 additions & 25 deletions numpy/_typing/_array_like.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -25,8 +23,11 @@

_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
Expand Down Expand Up @@ -61,8 +62,8 @@ def __array_function__(

# 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:
Expand Down Expand Up @@ -90,57 +91,62 @@ def __array_function__(
# `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,
]

Expand All @@ -153,6 +159,6 @@ class _UnknownType:


_ArrayLikeUnknown = _DualArrayLike[
"dtype[_UnknownType]",
dtype[_UnknownType],
_UnknownType,
]
2 changes: 1 addition & 1 deletion numpy/_typing/_callable.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ from ._scalars import (
_NumberLike_co,
)
from . import NBitBase
from ._generic_alias import NDArray
from ._array_like import NDArray
from ._nested_sequence import _NestedSequence

_T1 = TypeVar("_T1")
Expand Down
Loading

0 comments on commit 76ee503

Please sign in to comment.