From e0b23266a2bb0b6d07b9d76df2223a1b339aeb14 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Thu, 29 Jul 2021 13:42:38 +0200 Subject: TST: Add typing tests for `np.lib.npyio` --- numpy/typing/tests/data/fail/modules.py | 1 - numpy/typing/tests/data/fail/npyio.py | 31 ++++++++++++++ numpy/typing/tests/data/reveal/npyio.py | 71 +++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 numpy/typing/tests/data/fail/npyio.py create mode 100644 numpy/typing/tests/data/reveal/npyio.py (limited to 'numpy/typing') diff --git a/numpy/typing/tests/data/fail/modules.py b/numpy/typing/tests/data/fail/modules.py index 7b9309329..59e724f22 100644 --- a/numpy/typing/tests/data/fail/modules.py +++ b/numpy/typing/tests/data/fail/modules.py @@ -12,7 +12,6 @@ np.math # E: Module has no attribute # Public sub-modules that are not imported to their parent module by default; # e.g. one must first execute `import numpy.lib.recfunctions` np.lib.recfunctions # E: Module has no attribute -np.ma.mrecords # E: Module has no attribute np.__NUMPY_SETUP__ # E: Module has no attribute np.__deprecated_attrs__ # E: Module has no attribute diff --git a/numpy/typing/tests/data/fail/npyio.py b/numpy/typing/tests/data/fail/npyio.py new file mode 100644 index 000000000..89c511c1c --- /dev/null +++ b/numpy/typing/tests/data/fail/npyio.py @@ -0,0 +1,31 @@ +import pathlib +from typing import IO + +import numpy.typing as npt +import numpy as np + +str_path: str +bytes_path: bytes +pathlib_path: pathlib.Path +str_file: IO[str] +AR_i8: npt.NDArray[np.int64] + +np.load(str_file) # E: incompatible type + +np.save(bytes_path, AR_i8) # E: incompatible type +np.save(str_file, AR_i8) # E: incompatible type + +np.savez(bytes_path, AR_i8) # E: incompatible type +np.savez(str_file, AR_i8) # E: incompatible type + +np.savez_compressed(bytes_path, AR_i8) # E: incompatible type +np.savez_compressed(str_file, AR_i8) # E: incompatible type + +np.loadtxt(bytes_path) # E: No overload variant + +np.fromregex(bytes_path, ".", np.int64) # E: No overload variant +np.fromregex(pathlib_path, ".", np.int64) # E: No overload variant + +np.recfromtxt(bytes_path) # E: No overload variant + +np.recfromcsv(bytes_path) # E: No overload variant diff --git a/numpy/typing/tests/data/reveal/npyio.py b/numpy/typing/tests/data/reveal/npyio.py new file mode 100644 index 000000000..36c0c540b --- /dev/null +++ b/numpy/typing/tests/data/reveal/npyio.py @@ -0,0 +1,71 @@ +import re +import pathlib +from typing import IO, List + +import numpy.typing as npt +import numpy as np + +str_path: str +pathlib_path: pathlib.Path +str_file: IO[str] +bytes_file: IO[bytes] + +bag_obj: np.lib.npyio.BagObj[int] +npz_file: np.lib.npyio.NpzFile + +AR_i8: npt.NDArray[np.int64] +AR_LIKE_f8: List[float] + +reveal_type(bag_obj.a) # E: int +reveal_type(bag_obj.b) # E: int + +reveal_type(npz_file.zip) # E: zipfile.ZipFile +reveal_type(npz_file.fid) # E: Union[None, typing.IO[builtins.str]] +reveal_type(npz_file.files) # E: list[builtins.str] +reveal_type(npz_file.allow_pickle) # E: bool +reveal_type(npz_file.pickle_kwargs) # E: Union[None, typing.Mapping[builtins.str, Any]] +reveal_type(npz_file.f) # E: numpy.lib.npyio.BagObj[numpy.lib.npyio.NpzFile] +reveal_type(npz_file["test"]) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(len(npz_file)) # E: int +with npz_file as f: + reveal_type(f) # E: numpy.lib.npyio.NpzFile + +reveal_type(np.load(bytes_file)) # E: Any +reveal_type(np.load(pathlib_path, allow_pickle=True)) # E: Any +reveal_type(np.load(str_path, encoding="bytes")) # E: Any + +reveal_type(np.save(bytes_file, AR_LIKE_f8)) # E: None +reveal_type(np.save(pathlib_path, AR_i8, allow_pickle=True)) # E: None +reveal_type(np.save(str_path, AR_LIKE_f8)) # E: None + +reveal_type(np.savez(bytes_file, AR_LIKE_f8)) # E: None +reveal_type(np.savez(pathlib_path, ar1=AR_i8, ar2=AR_i8)) # E: None +reveal_type(np.savez(str_path, AR_LIKE_f8, ar1=AR_i8)) # E: None + +reveal_type(np.savez_compressed(bytes_file, AR_LIKE_f8)) # E: None +reveal_type(np.savez_compressed(pathlib_path, ar1=AR_i8, ar2=AR_i8)) # E: None +reveal_type(np.savez_compressed(str_path, AR_LIKE_f8, ar1=AR_i8)) # E: None + +reveal_type(np.loadtxt(bytes_file)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.loadtxt(pathlib_path, dtype=np.str_)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] +reveal_type(np.loadtxt(str_path, dtype=str, skiprows=2)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.loadtxt(str_file, comments="test")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.loadtxt(str_path, delimiter="\n")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.loadtxt(str_path, ndmin=2)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] + +reveal_type(np.fromregex(bytes_file, "test", np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.fromregex(str_file, b"test", dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.fromregex(str_path, re.compile("test"), dtype=np.str_, encoding="utf8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] + +reveal_type(np.genfromtxt(bytes_file)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.genfromtxt(pathlib_path, dtype=np.str_)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] +reveal_type(np.genfromtxt(str_path, dtype=str, skiprows=2)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.genfromtxt(str_file, comments="test")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.genfromtxt(str_path, delimiter="\n")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.genfromtxt(str_path, ndmin=2)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] + +reveal_type(np.recfromtxt(bytes_file)) # E: numpy.recarray[Any, numpy.dtype[numpy.void]] +reveal_type(np.recfromtxt(pathlib_path, usemask=True)) # E: numpy.ma.mrecords.MaskedRecords[Any, numpy.dtype[numpy.void]] + +reveal_type(np.recfromcsv(bytes_file)) # E: numpy.recarray[Any, numpy.dtype[numpy.void]] +reveal_type(np.recfromcsv(pathlib_path, usemask=True)) # E: numpy.ma.mrecords.MaskedRecords[Any, numpy.dtype[numpy.void]] -- cgit v1.2.1 From 9a649c3f0400861b5181e9d4087322662b01d280 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 16 Aug 2021 18:58:48 +0200 Subject: ENH: Allow `np.fromregex` to accept `os.PathLike` implementations --- numpy/typing/tests/data/fail/npyio.py | 1 - numpy/typing/tests/data/reveal/npyio.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/typing') diff --git a/numpy/typing/tests/data/fail/npyio.py b/numpy/typing/tests/data/fail/npyio.py index 89c511c1c..8edabf2b3 100644 --- a/numpy/typing/tests/data/fail/npyio.py +++ b/numpy/typing/tests/data/fail/npyio.py @@ -24,7 +24,6 @@ np.savez_compressed(str_file, AR_i8) # E: incompatible type np.loadtxt(bytes_path) # E: No overload variant np.fromregex(bytes_path, ".", np.int64) # E: No overload variant -np.fromregex(pathlib_path, ".", np.int64) # E: No overload variant np.recfromtxt(bytes_path) # E: No overload variant diff --git a/numpy/typing/tests/data/reveal/npyio.py b/numpy/typing/tests/data/reveal/npyio.py index 36c0c540b..05005eb1c 100644 --- a/numpy/typing/tests/data/reveal/npyio.py +++ b/numpy/typing/tests/data/reveal/npyio.py @@ -56,6 +56,7 @@ reveal_type(np.loadtxt(str_path, ndmin=2)) # E: numpy.ndarray[Any, numpy.dtype[ reveal_type(np.fromregex(bytes_file, "test", np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.fromregex(str_file, b"test", dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] reveal_type(np.fromregex(str_path, re.compile("test"), dtype=np.str_, encoding="utf8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] +reveal_type(np.fromregex(pathlib_path, "test", np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.genfromtxt(bytes_file)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.genfromtxt(pathlib_path, dtype=np.str_)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -- cgit v1.2.1 From 614057af8c2c50d2008936d9fa5b8e40f3b1a61b Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Mon, 16 Aug 2021 20:32:59 +0200 Subject: TST: Add typing tests for `np.lib.stride_tricks` --- numpy/typing/tests/data/fail/stride_tricks.py | 9 ++++++++ numpy/typing/tests/data/reveal/stride_tricks.py | 28 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 numpy/typing/tests/data/fail/stride_tricks.py create mode 100644 numpy/typing/tests/data/reveal/stride_tricks.py (limited to 'numpy/typing') diff --git a/numpy/typing/tests/data/fail/stride_tricks.py b/numpy/typing/tests/data/fail/stride_tricks.py new file mode 100644 index 000000000..f2bfba743 --- /dev/null +++ b/numpy/typing/tests/data/fail/stride_tricks.py @@ -0,0 +1,9 @@ +import numpy as np +import numpy.typing as npt + +AR_f8: npt.NDArray[np.float64] + +np.lib.stride_tricks.as_strided(AR_f8, shape=8) # E: No overload variant +np.lib.stride_tricks.as_strided(AR_f8, strides=8) # E: No overload variant + +np.lib.stride_tricks.sliding_window_view(AR_f8, axis=(1,)) # E: No overload variant diff --git a/numpy/typing/tests/data/reveal/stride_tricks.py b/numpy/typing/tests/data/reveal/stride_tricks.py new file mode 100644 index 000000000..152d9cea6 --- /dev/null +++ b/numpy/typing/tests/data/reveal/stride_tricks.py @@ -0,0 +1,28 @@ +from typing import List, Dict, Any +import numpy as np +import numpy.typing as npt + +AR_f8: npt.NDArray[np.float64] +AR_LIKE_f: List[float] +interface_dict: Dict[str, Any] + +reveal_type(np.lib.stride_tricks.DummyArray(interface_dict)) # E: numpy.lib.stride_tricks.DummyArray + +reveal_type(np.lib.stride_tricks.as_strided(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.lib.stride_tricks.as_strided(AR_LIKE_f)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.lib.stride_tricks.as_strided(AR_f8, strides=(1, 5))) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.lib.stride_tricks.as_strided(AR_f8, shape=[9, 20])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] + +reveal_type(np.lib.stride_tricks.sliding_window_view(AR_f8, 5)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.lib.stride_tricks.sliding_window_view(AR_LIKE_f, (1, 5))) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.lib.stride_tricks.sliding_window_view(AR_f8, [9], axis=1)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] + +reveal_type(np.broadcast_to(AR_f8, 5)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.broadcast_to(AR_LIKE_f, (1, 5))) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(np.broadcast_to(AR_f8, [4, 6], subok=True)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] + +reveal_type(np.broadcast_shapes((1, 2), [3, 1], (3, 2))) # E: tuple[builtins.int] +reveal_type(np.broadcast_shapes((6, 7), (5, 6, 1), 7, (5, 1, 7))) # E: tuple[builtins.int] + +reveal_type(np.broadcast_arrays(AR_f8, AR_f8)) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] +reveal_type(np.broadcast_arrays(AR_f8, AR_LIKE_f)) # E: list[numpy.ndarray[Any, numpy.dtype[Any]]] -- cgit v1.2.1 From d0b676b77bfa567eddb925bc31ead24ff0b576b1 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Sat, 14 Aug 2021 21:39:50 +0200 Subject: MAINT: Drop .py code-paths specific to Python 3.7 --- numpy/typing/__init__.py | 19 +- numpy/typing/_array_like.py | 26 +- numpy/typing/_callable.py | 587 ++++++++++++++++++------------------- numpy/typing/_char_codes.py | 282 +++++++----------- numpy/typing/_dtype_like.py | 51 ++-- numpy/typing/_shape.py | 12 +- numpy/typing/tests/test_runtime.py | 10 +- 7 files changed, 417 insertions(+), 570 deletions(-) (limited to 'numpy/typing') diff --git a/numpy/typing/__init__.py b/numpy/typing/__init__.py index d731f00ef..bfa7982c0 100644 --- a/numpy/typing/__init__.py +++ b/numpy/typing/__init__.py @@ -143,24 +143,7 @@ API # NOTE: The API section will be appended with additional entries # further down in this file -from typing import TYPE_CHECKING, List, Any - -if TYPE_CHECKING: - # 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: - _HAS_TYPING_EXTENSIONS = True - -if TYPE_CHECKING: - from typing_extensions import final -else: - def final(f): return f +from typing import TYPE_CHECKING, List, Any, final if not TYPE_CHECKING: __all__ = ["ArrayLike", "DTypeLike", "NBitBase", "NDArray"] diff --git a/numpy/typing/_array_like.py b/numpy/typing/_array_like.py index c562f3c1f..6ea0eb662 100644 --- a/numpy/typing/_array_like.py +++ b/numpy/typing/_array_like.py @@ -1,7 +1,6 @@ from __future__ import annotations -import sys -from typing import Any, Sequence, TYPE_CHECKING, Union, TypeVar, Generic +from typing import Any, Sequence, Protocol, Union, TypeVar from numpy import ( ndarray, dtype, @@ -19,28 +18,19 @@ from numpy import ( str_, bytes_, ) -from . import _HAS_TYPING_EXTENSIONS - -if sys.version_info >= (3, 8): - from typing import Protocol -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 _HAS_TYPING_EXTENSIONS or sys.version_info >= (3, 8): - # 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. - # Concrete implementations of the protocol are responsible for adding - # any and all remaining overloads - class _SupportsArray(Protocol[_DType_co]): - def __array__(self) -> ndarray[Any, _DType_co]: ... -else: - class _SupportsArray(Generic[_DType_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 +# array. +# Concrete implementations of the protocol are responsible for adding +# any and all remaining overloads +class _SupportsArray(Protocol[_DType_co]): + def __array__(self) -> ndarray[Any, _DType_co]: ... # TODO: Wait for support for recursive types _NestedSequence = Union[ diff --git a/numpy/typing/_callable.py b/numpy/typing/_callable.py index 8f911da3b..63a8153af 100644 --- a/numpy/typing/_callable.py +++ b/numpy/typing/_callable.py @@ -10,7 +10,6 @@ See the `Mypy documentation`_ on protocols for more details. from __future__ import annotations -import sys from typing import ( Union, TypeVar, @@ -18,7 +17,7 @@ from typing import ( Any, Tuple, NoReturn, - TYPE_CHECKING, + Protocol, ) from numpy import ( @@ -45,312 +44,282 @@ from ._scalars import ( _FloatLike_co, _NumberLike_co, ) -from . import NBitBase, _HAS_TYPING_EXTENSIONS +from . import NBitBase from ._generic_alias import NDArray -if sys.version_info >= (3, 8): - from typing import Protocol -elif _HAS_TYPING_EXTENSIONS: - from typing_extensions import Protocol - -if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS or sys.version_info >= (3, 8): - _T1 = TypeVar("_T1") - _T2 = TypeVar("_T2") - _2Tuple = Tuple[_T1, _T1] - - _NBit1 = TypeVar("_NBit1", bound=NBitBase) - _NBit2 = TypeVar("_NBit2", bound=NBitBase) - - _IntType = TypeVar("_IntType", bound=integer) - _FloatType = TypeVar("_FloatType", bound=floating) - _NumberType = TypeVar("_NumberType", bound=number) - _NumberType_co = TypeVar("_NumberType_co", covariant=True, bound=number) - _GenericType_co = TypeVar("_GenericType_co", covariant=True, bound=generic) - - class _BoolOp(Protocol[_GenericType_co]): - @overload - def __call__(self, __other: _BoolLike_co) -> _GenericType_co: ... - @overload # platform dependent - def __call__(self, __other: int) -> int_: ... - @overload - def __call__(self, __other: float) -> float64: ... - @overload - def __call__(self, __other: complex) -> complex128: ... - @overload - def __call__(self, __other: _NumberType) -> _NumberType: ... - - class _BoolBitOp(Protocol[_GenericType_co]): - @overload - def __call__(self, __other: _BoolLike_co) -> _GenericType_co: ... - @overload # platform dependent - def __call__(self, __other: int) -> int_: ... - @overload - def __call__(self, __other: _IntType) -> _IntType: ... - - class _BoolSub(Protocol): - # Note that `__other: bool_` is absent here - @overload - def __call__(self, __other: bool) -> NoReturn: ... - @overload # platform dependent - def __call__(self, __other: int) -> int_: ... - @overload - def __call__(self, __other: float) -> float64: ... - @overload - def __call__(self, __other: complex) -> complex128: ... - @overload - def __call__(self, __other: _NumberType) -> _NumberType: ... - - class _BoolTrueDiv(Protocol): - @overload - def __call__(self, __other: float | _IntLike_co) -> float64: ... - @overload - def __call__(self, __other: complex) -> complex128: ... - @overload - def __call__(self, __other: _NumberType) -> _NumberType: ... - - class _BoolMod(Protocol): - @overload - def __call__(self, __other: _BoolLike_co) -> int8: ... - @overload # platform dependent - def __call__(self, __other: int) -> int_: ... - @overload - def __call__(self, __other: float) -> float64: ... - @overload - def __call__(self, __other: _IntType) -> _IntType: ... - @overload - def __call__(self, __other: _FloatType) -> _FloatType: ... - - class _BoolDivMod(Protocol): - @overload - def __call__(self, __other: _BoolLike_co) -> _2Tuple[int8]: ... - @overload # platform dependent - def __call__(self, __other: int) -> _2Tuple[int_]: ... - @overload - def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... - @overload - def __call__(self, __other: _IntType) -> _2Tuple[_IntType]: ... - @overload - def __call__(self, __other: _FloatType) -> _2Tuple[_FloatType]: ... - - class _TD64Div(Protocol[_NumberType_co]): - @overload - def __call__(self, __other: timedelta64) -> _NumberType_co: ... - @overload - def __call__(self, __other: _BoolLike_co) -> NoReturn: ... - @overload - def __call__(self, __other: _FloatLike_co) -> timedelta64: ... - - class _IntTrueDiv(Protocol[_NBit1]): - @overload - def __call__(self, __other: bool) -> floating[_NBit1]: ... - @overload - def __call__(self, __other: int) -> floating[_NBit1 | _NBitInt]: ... - @overload - def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... - @overload - def __call__( - self, __other: complex - ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... - @overload - def __call__(self, __other: integer[_NBit2]) -> floating[_NBit1 | _NBit2]: ... - - class _UnsignedIntOp(Protocol[_NBit1]): - # NOTE: `uint64 + signedinteger -> float64` - @overload - def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ... - @overload - def __call__( - self, __other: int | signedinteger[Any] - ) -> Any: ... - @overload - def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... - @overload - def __call__( - self, __other: complex - ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... - @overload - def __call__( - self, __other: unsignedinteger[_NBit2] - ) -> unsignedinteger[_NBit1 | _NBit2]: ... - - class _UnsignedIntBitOp(Protocol[_NBit1]): - @overload - def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ... - @overload - def __call__(self, __other: int) -> signedinteger[Any]: ... - @overload - def __call__(self, __other: signedinteger[Any]) -> signedinteger[Any]: ... - @overload - def __call__( - self, __other: unsignedinteger[_NBit2] - ) -> unsignedinteger[_NBit1 | _NBit2]: ... - - class _UnsignedIntMod(Protocol[_NBit1]): - @overload - def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ... - @overload - def __call__( - self, __other: int | signedinteger[Any] - ) -> Any: ... - @overload - def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... - @overload - def __call__( - self, __other: unsignedinteger[_NBit2] - ) -> unsignedinteger[_NBit1 | _NBit2]: ... - - class _UnsignedIntDivMod(Protocol[_NBit1]): - @overload - def __call__(self, __other: bool) -> _2Tuple[signedinteger[_NBit1]]: ... - @overload - def __call__( - self, __other: int | signedinteger[Any] - ) -> _2Tuple[Any]: ... - @overload - def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... - @overload - def __call__( - self, __other: unsignedinteger[_NBit2] - ) -> _2Tuple[unsignedinteger[_NBit1 | _NBit2]]: ... - - class _SignedIntOp(Protocol[_NBit1]): - @overload - def __call__(self, __other: bool) -> signedinteger[_NBit1]: ... - @overload - def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ... - @overload - def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... - @overload - def __call__( - self, __other: complex - ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... - @overload - def __call__( - self, __other: signedinteger[_NBit2] - ) -> signedinteger[_NBit1 | _NBit2]: ... - - class _SignedIntBitOp(Protocol[_NBit1]): - @overload - def __call__(self, __other: bool) -> signedinteger[_NBit1]: ... - @overload - def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ... - @overload - def __call__( - self, __other: signedinteger[_NBit2] - ) -> signedinteger[_NBit1 | _NBit2]: ... - - class _SignedIntMod(Protocol[_NBit1]): - @overload - def __call__(self, __other: bool) -> signedinteger[_NBit1]: ... - @overload - def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ... - @overload - def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... - @overload - def __call__( - self, __other: signedinteger[_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[_NBit1 | _NBitInt]]: ... - @overload - def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... - @overload - def __call__( - self, __other: signedinteger[_NBit2] - ) -> _2Tuple[signedinteger[_NBit1 | _NBit2]]: ... - - class _FloatOp(Protocol[_NBit1]): - @overload - def __call__(self, __other: bool) -> floating[_NBit1]: ... - @overload - def __call__(self, __other: int) -> floating[_NBit1 | _NBitInt]: ... - @overload - def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... - @overload - def __call__( - self, __other: complex - ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... - @overload - def __call__( - 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[_NBit1 | _NBitInt]: ... - @overload - def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... - @overload - def __call__( - 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[_NBit1 | _NBitInt]]: ... - @overload - def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... - @overload - def __call__( - 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[_NBit1 | _NBitInt, _NBit1 | _NBitInt]: ... - @overload - def __call__( - self, __other: complex - ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... - @overload - def __call__( - self, - __other: Union[ - integer[_NBit2], - floating[_NBit2], - complexfloating[_NBit2, _NBit2], - ] - ) -> complexfloating[_NBit1 | _NBit2, _NBit1 | _NBit2]: ... - - class _NumberOp(Protocol): - def __call__(self, __other: _NumberLike_co) -> Any: ... - - class _ComparisonOp(Protocol[_T1, _T2]): - @overload - def __call__(self, __other: _T1) -> bool_: ... - @overload - 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 +_T1 = TypeVar("_T1") +_T2 = TypeVar("_T2") +_2Tuple = Tuple[_T1, _T1] + +_NBit1 = TypeVar("_NBit1", bound=NBitBase) +_NBit2 = TypeVar("_NBit2", bound=NBitBase) + +_IntType = TypeVar("_IntType", bound=integer) +_FloatType = TypeVar("_FloatType", bound=floating) +_NumberType = TypeVar("_NumberType", bound=number) +_NumberType_co = TypeVar("_NumberType_co", covariant=True, bound=number) +_GenericType_co = TypeVar("_GenericType_co", covariant=True, bound=generic) + +class _BoolOp(Protocol[_GenericType_co]): + @overload + def __call__(self, __other: _BoolLike_co) -> _GenericType_co: ... + @overload # platform dependent + def __call__(self, __other: int) -> int_: ... + @overload + def __call__(self, __other: float) -> float64: ... + @overload + def __call__(self, __other: complex) -> complex128: ... + @overload + def __call__(self, __other: _NumberType) -> _NumberType: ... + +class _BoolBitOp(Protocol[_GenericType_co]): + @overload + def __call__(self, __other: _BoolLike_co) -> _GenericType_co: ... + @overload # platform dependent + def __call__(self, __other: int) -> int_: ... + @overload + def __call__(self, __other: _IntType) -> _IntType: ... + +class _BoolSub(Protocol): + # Note that `__other: bool_` is absent here + @overload + def __call__(self, __other: bool) -> NoReturn: ... + @overload # platform dependent + def __call__(self, __other: int) -> int_: ... + @overload + def __call__(self, __other: float) -> float64: ... + @overload + def __call__(self, __other: complex) -> complex128: ... + @overload + def __call__(self, __other: _NumberType) -> _NumberType: ... + +class _BoolTrueDiv(Protocol): + @overload + def __call__(self, __other: float | _IntLike_co) -> float64: ... + @overload + def __call__(self, __other: complex) -> complex128: ... + @overload + def __call__(self, __other: _NumberType) -> _NumberType: ... + +class _BoolMod(Protocol): + @overload + def __call__(self, __other: _BoolLike_co) -> int8: ... + @overload # platform dependent + def __call__(self, __other: int) -> int_: ... + @overload + def __call__(self, __other: float) -> float64: ... + @overload + def __call__(self, __other: _IntType) -> _IntType: ... + @overload + def __call__(self, __other: _FloatType) -> _FloatType: ... + +class _BoolDivMod(Protocol): + @overload + def __call__(self, __other: _BoolLike_co) -> _2Tuple[int8]: ... + @overload # platform dependent + def __call__(self, __other: int) -> _2Tuple[int_]: ... + @overload + def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... + @overload + def __call__(self, __other: _IntType) -> _2Tuple[_IntType]: ... + @overload + def __call__(self, __other: _FloatType) -> _2Tuple[_FloatType]: ... + +class _TD64Div(Protocol[_NumberType_co]): + @overload + def __call__(self, __other: timedelta64) -> _NumberType_co: ... + @overload + def __call__(self, __other: _BoolLike_co) -> NoReturn: ... + @overload + def __call__(self, __other: _FloatLike_co) -> timedelta64: ... + +class _IntTrueDiv(Protocol[_NBit1]): + @overload + def __call__(self, __other: bool) -> floating[_NBit1]: ... + @overload + def __call__(self, __other: int) -> floating[_NBit1 | _NBitInt]: ... + @overload + def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... + @overload + def __call__( + self, __other: complex + ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... + @overload + def __call__(self, __other: integer[_NBit2]) -> floating[_NBit1 | _NBit2]: ... + +class _UnsignedIntOp(Protocol[_NBit1]): + # NOTE: `uint64 + signedinteger -> float64` + @overload + def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ... + @overload + def __call__( + self, __other: int | signedinteger[Any] + ) -> Any: ... + @overload + def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... + @overload + def __call__( + self, __other: complex + ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... + @overload + def __call__( + self, __other: unsignedinteger[_NBit2] + ) -> unsignedinteger[_NBit1 | _NBit2]: ... + +class _UnsignedIntBitOp(Protocol[_NBit1]): + @overload + def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ... + @overload + def __call__(self, __other: int) -> signedinteger[Any]: ... + @overload + def __call__(self, __other: signedinteger[Any]) -> signedinteger[Any]: ... + @overload + def __call__( + self, __other: unsignedinteger[_NBit2] + ) -> unsignedinteger[_NBit1 | _NBit2]: ... + +class _UnsignedIntMod(Protocol[_NBit1]): + @overload + def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ... + @overload + def __call__( + self, __other: int | signedinteger[Any] + ) -> Any: ... + @overload + def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... + @overload + def __call__( + self, __other: unsignedinteger[_NBit2] + ) -> unsignedinteger[_NBit1 | _NBit2]: ... + +class _UnsignedIntDivMod(Protocol[_NBit1]): + @overload + def __call__(self, __other: bool) -> _2Tuple[signedinteger[_NBit1]]: ... + @overload + def __call__( + self, __other: int | signedinteger[Any] + ) -> _2Tuple[Any]: ... + @overload + def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... + @overload + def __call__( + self, __other: unsignedinteger[_NBit2] + ) -> _2Tuple[unsignedinteger[_NBit1 | _NBit2]]: ... + +class _SignedIntOp(Protocol[_NBit1]): + @overload + def __call__(self, __other: bool) -> signedinteger[_NBit1]: ... + @overload + def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ... + @overload + def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... + @overload + def __call__( + self, __other: complex + ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... + @overload + def __call__( + self, __other: signedinteger[_NBit2] + ) -> signedinteger[_NBit1 | _NBit2]: ... + +class _SignedIntBitOp(Protocol[_NBit1]): + @overload + def __call__(self, __other: bool) -> signedinteger[_NBit1]: ... + @overload + def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ... + @overload + def __call__( + self, __other: signedinteger[_NBit2] + ) -> signedinteger[_NBit1 | _NBit2]: ... + +class _SignedIntMod(Protocol[_NBit1]): + @overload + def __call__(self, __other: bool) -> signedinteger[_NBit1]: ... + @overload + def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ... + @overload + def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... + @overload + def __call__( + self, __other: signedinteger[_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[_NBit1 | _NBitInt]]: ... + @overload + def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... + @overload + def __call__( + self, __other: signedinteger[_NBit2] + ) -> _2Tuple[signedinteger[_NBit1 | _NBit2]]: ... + +class _FloatOp(Protocol[_NBit1]): + @overload + def __call__(self, __other: bool) -> floating[_NBit1]: ... + @overload + def __call__(self, __other: int) -> floating[_NBit1 | _NBitInt]: ... + @overload + def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... + @overload + def __call__( + self, __other: complex + ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... + @overload + def __call__( + 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[_NBit1 | _NBitInt]: ... + @overload + def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... + @overload + def __call__( + 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[_NBit1 | _NBitInt]]: ... + @overload + def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... + @overload + def __call__( + 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[_NBit1 | _NBitInt, _NBit1 | _NBitInt]: ... + @overload + def __call__( + self, __other: complex + ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... + @overload + def __call__( + self, + __other: Union[ + integer[_NBit2], + floating[_NBit2], + complexfloating[_NBit2, _NBit2], + ] + ) -> complexfloating[_NBit1 | _NBit2, _NBit1 | _NBit2]: ... + +class _NumberOp(Protocol): + def __call__(self, __other: _NumberLike_co) -> Any: ... + +class _ComparisonOp(Protocol[_T1, _T2]): + @overload + def __call__(self, __other: _T1) -> bool_: ... + @overload + def __call__(self, __other: _T2) -> NDArray[bool_]: ... diff --git a/numpy/typing/_char_codes.py b/numpy/typing/_char_codes.py index 22ee168e9..139471084 100644 --- a/numpy/typing/_char_codes.py +++ b/numpy/typing/_char_codes.py @@ -1,171 +1,111 @@ -import sys -from typing import Any, TYPE_CHECKING - -from . import _HAS_TYPING_EXTENSIONS - -if sys.version_info >= (3, 8): - from typing import Literal -elif _HAS_TYPING_EXTENSIONS: - from typing_extensions import Literal - -if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS or sys.version_info >= (3, 8): - _BoolCodes = Literal["?", "=?", "?", "bool", "bool_", "bool8"] - - _UInt8Codes = Literal["uint8", "u1", "=u1", "u1"] - _UInt16Codes = Literal["uint16", "u2", "=u2", "u2"] - _UInt32Codes = Literal["uint32", "u4", "=u4", "u4"] - _UInt64Codes = Literal["uint64", "u8", "=u8", "u8"] - - _Int8Codes = Literal["int8", "i1", "=i1", "i1"] - _Int16Codes = Literal["int16", "i2", "=i2", "i2"] - _Int32Codes = Literal["int32", "i4", "=i4", "i4"] - _Int64Codes = Literal["int64", "i8", "=i8", "i8"] - - _Float16Codes = Literal["float16", "f2", "=f2", "f2"] - _Float32Codes = Literal["float32", "f4", "=f4", "f4"] - _Float64Codes = Literal["float64", "f8", "=f8", "f8"] - - _Complex64Codes = Literal["complex64", "c8", "=c8", "c8"] - _Complex128Codes = Literal["complex128", "c16", "=c16", "c16"] - - _ByteCodes = Literal["byte", "b", "=b", "b"] - _ShortCodes = Literal["short", "h", "=h", "h"] - _IntCCodes = Literal["intc", "i", "=i", "i"] - _IntPCodes = Literal["intp", "int0", "p", "=p", "p"] - _IntCodes = Literal["long", "int", "int_", "l", "=l", "l"] - _LongLongCodes = Literal["longlong", "q", "=q", "q"] - - _UByteCodes = Literal["ubyte", "B", "=B", "B"] - _UShortCodes = Literal["ushort", "H", "=H", "H"] - _UIntCCodes = Literal["uintc", "I", "=I", "I"] - _UIntPCodes = Literal["uintp", "uint0", "P", "=P", "P"] - _UIntCodes = Literal["uint", "L", "=L", "L"] - _ULongLongCodes = Literal["ulonglong", "Q", "=Q", "Q"] - - _HalfCodes = Literal["half", "e", "=e", "e"] - _SingleCodes = Literal["single", "f", "=f", "f"] - _DoubleCodes = Literal["double", "float", "float_", "d", "=d", "d"] - _LongDoubleCodes = Literal["longdouble", "longfloat", "g", "=g", "g"] - - _CSingleCodes = Literal["csingle", "singlecomplex", "F", "=F", "F"] - _CDoubleCodes = Literal["cdouble", "complex", "complex_", "cfloat", "D", "=D", "D"] - _CLongDoubleCodes = Literal["clongdouble", "clongfloat", "longcomplex", "G", "=G", "G"] - - _StrCodes = Literal["str", "str_", "str0", "unicode", "unicode_", "U", "=U", "U"] - _BytesCodes = Literal["bytes", "bytes_", "bytes0", "S", "=S", "S"] - _VoidCodes = Literal["void", "void0", "V", "=V", "V"] - _ObjectCodes = Literal["object", "object_", "O", "=O", "O"] - - _DT64Codes = Literal[ - "datetime64", "=datetime64", "datetime64", - "datetime64[Y]", "=datetime64[Y]", "datetime64[Y]", - "datetime64[M]", "=datetime64[M]", "datetime64[M]", - "datetime64[W]", "=datetime64[W]", "datetime64[W]", - "datetime64[D]", "=datetime64[D]", "datetime64[D]", - "datetime64[h]", "=datetime64[h]", "datetime64[h]", - "datetime64[m]", "=datetime64[m]", "datetime64[m]", - "datetime64[s]", "=datetime64[s]", "datetime64[s]", - "datetime64[ms]", "=datetime64[ms]", "datetime64[ms]", - "datetime64[us]", "=datetime64[us]", "datetime64[us]", - "datetime64[ns]", "=datetime64[ns]", "datetime64[ns]", - "datetime64[ps]", "=datetime64[ps]", "datetime64[ps]", - "datetime64[fs]", "=datetime64[fs]", "datetime64[fs]", - "datetime64[as]", "=datetime64[as]", "datetime64[as]", - "M", "=M", "M", - "M8", "=M8", "M8", - "M8[Y]", "=M8[Y]", "M8[Y]", - "M8[M]", "=M8[M]", "M8[M]", - "M8[W]", "=M8[W]", "M8[W]", - "M8[D]", "=M8[D]", "M8[D]", - "M8[h]", "=M8[h]", "M8[h]", - "M8[m]", "=M8[m]", "M8[m]", - "M8[s]", "=M8[s]", "M8[s]", - "M8[ms]", "=M8[ms]", "M8[ms]", - "M8[us]", "=M8[us]", "M8[us]", - "M8[ns]", "=M8[ns]", "M8[ns]", - "M8[ps]", "=M8[ps]", "M8[ps]", - "M8[fs]", "=M8[fs]", "M8[fs]", - "M8[as]", "=M8[as]", "M8[as]", - ] - _TD64Codes = Literal[ - "timedelta64", "=timedelta64", "timedelta64", - "timedelta64[Y]", "=timedelta64[Y]", "timedelta64[Y]", - "timedelta64[M]", "=timedelta64[M]", "timedelta64[M]", - "timedelta64[W]", "=timedelta64[W]", "timedelta64[W]", - "timedelta64[D]", "=timedelta64[D]", "timedelta64[D]", - "timedelta64[h]", "=timedelta64[h]", "timedelta64[h]", - "timedelta64[m]", "=timedelta64[m]", "timedelta64[m]", - "timedelta64[s]", "=timedelta64[s]", "timedelta64[s]", - "timedelta64[ms]", "=timedelta64[ms]", "timedelta64[ms]", - "timedelta64[us]", "=timedelta64[us]", "timedelta64[us]", - "timedelta64[ns]", "=timedelta64[ns]", "timedelta64[ns]", - "timedelta64[ps]", "=timedelta64[ps]", "timedelta64[ps]", - "timedelta64[fs]", "=timedelta64[fs]", "timedelta64[fs]", - "timedelta64[as]", "=timedelta64[as]", "timedelta64[as]", - "m", "=m", "m", - "m8", "=m8", "m8", - "m8[Y]", "=m8[Y]", "m8[Y]", - "m8[M]", "=m8[M]", "m8[M]", - "m8[W]", "=m8[W]", "m8[W]", - "m8[D]", "=m8[D]", "m8[D]", - "m8[h]", "=m8[h]", "m8[h]", - "m8[m]", "=m8[m]", "m8[m]", - "m8[s]", "=m8[s]", "m8[s]", - "m8[ms]", "=m8[ms]", "m8[ms]", - "m8[us]", "=m8[us]", "m8[us]", - "m8[ns]", "=m8[ns]", "m8[ns]", - "m8[ps]", "=m8[ps]", "m8[ps]", - "m8[fs]", "=m8[fs]", "m8[fs]", - "m8[as]", "=m8[as]", "m8[as]", - ] - -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 +from typing import Literal + +_BoolCodes = Literal["?", "=?", "?", "bool", "bool_", "bool8"] + +_UInt8Codes = Literal["uint8", "u1", "=u1", "u1"] +_UInt16Codes = Literal["uint16", "u2", "=u2", "u2"] +_UInt32Codes = Literal["uint32", "u4", "=u4", "u4"] +_UInt64Codes = Literal["uint64", "u8", "=u8", "u8"] + +_Int8Codes = Literal["int8", "i1", "=i1", "i1"] +_Int16Codes = Literal["int16", "i2", "=i2", "i2"] +_Int32Codes = Literal["int32", "i4", "=i4", "i4"] +_Int64Codes = Literal["int64", "i8", "=i8", "i8"] + +_Float16Codes = Literal["float16", "f2", "=f2", "f2"] +_Float32Codes = Literal["float32", "f4", "=f4", "f4"] +_Float64Codes = Literal["float64", "f8", "=f8", "f8"] + +_Complex64Codes = Literal["complex64", "c8", "=c8", "c8"] +_Complex128Codes = Literal["complex128", "c16", "=c16", "c16"] + +_ByteCodes = Literal["byte", "b", "=b", "b"] +_ShortCodes = Literal["short", "h", "=h", "h"] +_IntCCodes = Literal["intc", "i", "=i", "i"] +_IntPCodes = Literal["intp", "int0", "p", "=p", "p"] +_IntCodes = Literal["long", "int", "int_", "l", "=l", "l"] +_LongLongCodes = Literal["longlong", "q", "=q", "q"] + +_UByteCodes = Literal["ubyte", "B", "=B", "B"] +_UShortCodes = Literal["ushort", "H", "=H", "H"] +_UIntCCodes = Literal["uintc", "I", "=I", "I"] +_UIntPCodes = Literal["uintp", "uint0", "P", "=P", "P"] +_UIntCodes = Literal["uint", "L", "=L", "L"] +_ULongLongCodes = Literal["ulonglong", "Q", "=Q", "Q"] + +_HalfCodes = Literal["half", "e", "=e", "e"] +_SingleCodes = Literal["single", "f", "=f", "f"] +_DoubleCodes = Literal["double", "float", "float_", "d", "=d", "d"] +_LongDoubleCodes = Literal["longdouble", "longfloat", "g", "=g", "g"] + +_CSingleCodes = Literal["csingle", "singlecomplex", "F", "=F", "F"] +_CDoubleCodes = Literal["cdouble", "complex", "complex_", "cfloat", "D", "=D", "D"] +_CLongDoubleCodes = Literal["clongdouble", "clongfloat", "longcomplex", "G", "=G", "G"] + +_StrCodes = Literal["str", "str_", "str0", "unicode", "unicode_", "U", "=U", "U"] +_BytesCodes = Literal["bytes", "bytes_", "bytes0", "S", "=S", "S"] +_VoidCodes = Literal["void", "void0", "V", "=V", "V"] +_ObjectCodes = Literal["object", "object_", "O", "=O", "O"] + +_DT64Codes = Literal[ + "datetime64", "=datetime64", "datetime64", + "datetime64[Y]", "=datetime64[Y]", "datetime64[Y]", + "datetime64[M]", "=datetime64[M]", "datetime64[M]", + "datetime64[W]", "=datetime64[W]", "datetime64[W]", + "datetime64[D]", "=datetime64[D]", "datetime64[D]", + "datetime64[h]", "=datetime64[h]", "datetime64[h]", + "datetime64[m]", "=datetime64[m]", "datetime64[m]", + "datetime64[s]", "=datetime64[s]", "datetime64[s]", + "datetime64[ms]", "=datetime64[ms]", "datetime64[ms]", + "datetime64[us]", "=datetime64[us]", "datetime64[us]", + "datetime64[ns]", "=datetime64[ns]", "datetime64[ns]", + "datetime64[ps]", "=datetime64[ps]", "datetime64[ps]", + "datetime64[fs]", "=datetime64[fs]", "datetime64[fs]", + "datetime64[as]", "=datetime64[as]", "datetime64[as]", + "M", "=M", "M", + "M8", "=M8", "M8", + "M8[Y]", "=M8[Y]", "M8[Y]", + "M8[M]", "=M8[M]", "M8[M]", + "M8[W]", "=M8[W]", "M8[W]", + "M8[D]", "=M8[D]", "M8[D]", + "M8[h]", "=M8[h]", "M8[h]", + "M8[m]", "=M8[m]", "M8[m]", + "M8[s]", "=M8[s]", "M8[s]", + "M8[ms]", "=M8[ms]", "M8[ms]", + "M8[us]", "=M8[us]", "M8[us]", + "M8[ns]", "=M8[ns]", "M8[ns]", + "M8[ps]", "=M8[ps]", "M8[ps]", + "M8[fs]", "=M8[fs]", "M8[fs]", + "M8[as]", "=M8[as]", "M8[as]", +] +_TD64Codes = Literal[ + "timedelta64", "=timedelta64", "timedelta64", + "timedelta64[Y]", "=timedelta64[Y]", "timedelta64[Y]", + "timedelta64[M]", "=timedelta64[M]", "timedelta64[M]", + "timedelta64[W]", "=timedelta64[W]", "timedelta64[W]", + "timedelta64[D]", "=timedelta64[D]", "timedelta64[D]", + "timedelta64[h]", "=timedelta64[h]", "timedelta64[h]", + "timedelta64[m]", "=timedelta64[m]", "timedelta64[m]", + "timedelta64[s]", "=timedelta64[s]", "timedelta64[s]", + "timedelta64[ms]", "=timedelta64[ms]", "timedelta64[ms]", + "timedelta64[us]", "=timedelta64[us]", "timedelta64[us]", + "timedelta64[ns]", "=timedelta64[ns]", "timedelta64[ns]", + "timedelta64[ps]", "=timedelta64[ps]", "timedelta64[ps]", + "timedelta64[fs]", "=timedelta64[fs]", "timedelta64[fs]", + "timedelta64[as]", "=timedelta64[as]", "timedelta64[as]", + "m", "=m", "m", + "m8", "=m8", "m8", + "m8[Y]", "=m8[Y]", "m8[Y]", + "m8[M]", "=m8[M]", "m8[M]", + "m8[W]", "=m8[W]", "m8[W]", + "m8[D]", "=m8[D]", "m8[D]", + "m8[h]", "=m8[h]", "m8[h]", + "m8[m]", "=m8[m]", "m8[m]", + "m8[s]", "=m8[s]", "m8[s]", + "m8[ms]", "=m8[ms]", "m8[ms]", + "m8[us]", "=m8[us]", "m8[us]", + "m8[ns]", "=m8[ns]", "m8[ns]", + "m8[ps]", "=m8[ps]", "m8[ps]", + "m8[fs]", "=m8[fs]", "m8[fs]", + "m8[as]", "=m8[as]", "m8[as]", +] diff --git a/numpy/typing/_dtype_like.py b/numpy/typing/_dtype_like.py index b2ce3adb4..0955f5b18 100644 --- a/numpy/typing/_dtype_like.py +++ b/numpy/typing/_dtype_like.py @@ -1,19 +1,10 @@ -import sys -from typing import Any, List, Sequence, Tuple, Union, Type, TypeVar, TYPE_CHECKING +from typing import Any, List, Sequence, Tuple, Union, Type, TypeVar, Protocol, TypedDict 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 -elif _HAS_TYPING_EXTENSIONS: - from typing_extensions import Protocol, TypedDict -else: - from ._generic_alias import _GenericAlias as GenericAlias - from ._char_codes import ( _BoolCodes, _UInt8Codes, @@ -59,30 +50,22 @@ 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 _HAS_TYPING_EXTENSIONS or sys.version_info >= (3, 8): - # Mandatory keys - class _DTypeDictBase(TypedDict): - names: Sequence[str] - formats: Sequence[_DTypeLikeNested] - - # Mandatory + optional keys - class _DTypeDict(_DTypeDictBase, total=False): - offsets: Sequence[int] - titles: Sequence[Any] # Only `str` elements are usable as indexing aliases, but all objects are legal - itemsize: int - aligned: bool - - # A protocol for anything with the dtype attribute - class _SupportsDType(Protocol[_DType_co]): - @property - def dtype(self) -> _DType_co: ... - -else: - _DTypeDict = Any - - class _SupportsDType: ... - _SupportsDType = GenericAlias(_SupportsDType, _DType_co) - +# Mandatory keys +class _DTypeDictBase(TypedDict): + names: Sequence[str] + formats: Sequence[_DTypeLikeNested] + +# Mandatory + optional keys +class _DTypeDict(_DTypeDictBase, total=False): + offsets: Sequence[int] + titles: Sequence[Any] # Only `str` elements are usable as indexing aliases, but all objects are legal + itemsize: int + aligned: bool + +# A protocol for anything with the dtype attribute +class _SupportsDType(Protocol[_DType_co]): + @property + def dtype(self) -> _DType_co: ... # Would create a dtype[np.void] _VoidDTypeLike = Union[ diff --git a/numpy/typing/_shape.py b/numpy/typing/_shape.py index 75698f3d3..c28859b19 100644 --- a/numpy/typing/_shape.py +++ b/numpy/typing/_shape.py @@ -1,14 +1,4 @@ -import sys -from typing import Sequence, Tuple, Union, Any - -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: - SupportsIndex = Any +from typing import Sequence, Tuple, Union, SupportsIndex _Shape = Tuple[int, ...] diff --git a/numpy/typing/tests/test_runtime.py b/numpy/typing/tests/test_runtime.py index e82b08ac2..151b06bed 100644 --- a/numpy/typing/tests/test_runtime.py +++ b/numpy/typing/tests/test_runtime.py @@ -3,18 +3,12 @@ from __future__ import annotations import sys -from typing import get_type_hints, Union, Tuple, NamedTuple +from typing import get_type_hints, Union, Tuple, NamedTuple, get_args, get_origin import pytest import numpy as np import numpy.typing as npt -try: - from typing_extensions import get_args, get_origin - SKIP = False -except ImportError: - SKIP = True - class TypeTup(NamedTuple): typ: type @@ -36,7 +30,6 @@ TYPES = { @pytest.mark.parametrize("name,tup", TYPES.items(), ids=TYPES.keys()) -@pytest.mark.skipif(SKIP, reason="requires typing-extensions") def test_get_args(name: type, tup: TypeTup) -> None: """Test `typing.get_args`.""" typ, ref = tup.typ, tup.args @@ -45,7 +38,6 @@ def test_get_args(name: type, tup: TypeTup) -> None: @pytest.mark.parametrize("name,tup", TYPES.items(), ids=TYPES.keys()) -@pytest.mark.skipif(SKIP, reason="requires typing-extensions") def test_get_origin(name: type, tup: TypeTup) -> None: """Test `typing.get_origin`.""" typ, ref = tup.typ, tup.origin -- cgit v1.2.1 From 355336119136f620c27f1adcc26074eb9528c159 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Sat, 14 Aug 2021 22:02:29 +0200 Subject: MAINT: Drop .pyi code-paths specific to Python 3.7 --- numpy/typing/_ufunc.pyi | 4 ++-- numpy/typing/tests/data/reveal/arraypad.py | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'numpy/typing') diff --git a/numpy/typing/_ufunc.pyi b/numpy/typing/_ufunc.pyi index be1e654c2..37e6c008f 100644 --- a/numpy/typing/_ufunc.pyi +++ b/numpy/typing/_ufunc.pyi @@ -14,6 +14,8 @@ from typing import ( overload, Tuple, TypeVar, + Literal, + SupportsIndex, ) from numpy import ufunc, _CastingKind, _OrderKACF @@ -24,8 +26,6 @@ from ._scalars import _ScalarLike_co from ._array_like import ArrayLike, _ArrayLikeBool_co, _ArrayLikeInt_co from ._dtype_like import DTypeLike -from typing_extensions import Literal, SupportsIndex - _T = TypeVar("_T") _2Tuple = Tuple[_T, _T] _3Tuple = Tuple[_T, _T, _T] diff --git a/numpy/typing/tests/data/reveal/arraypad.py b/numpy/typing/tests/data/reveal/arraypad.py index ba5577ee0..03c03fb4e 100644 --- a/numpy/typing/tests/data/reveal/arraypad.py +++ b/numpy/typing/tests/data/reveal/arraypad.py @@ -1,5 +1,4 @@ -from typing import List, Any, Mapping, Tuple -from typing_extensions import SupportsIndex +from typing import List, Any, Mapping, Tuple, SupportsIndex import numpy as np import numpy.typing as npt -- cgit v1.2.1 From add26eb860dc1ee85c00d4c93a3ebf728b98448b Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Mon, 30 Aug 2021 15:07:45 +0200 Subject: BLD: Drop typing extension as an (optional) runtime dependency It might return in the future, but as of the moment we don't need it anymore --- numpy/typing/__init__.py | 7 ------ numpy/typing/tests/test_typing_extensions.py | 35 ---------------------------- 2 files changed, 42 deletions(-) delete mode 100644 numpy/typing/tests/test_typing_extensions.py (limited to 'numpy/typing') diff --git a/numpy/typing/__init__.py b/numpy/typing/__init__.py index bfa7982c0..d60ddb5bb 100644 --- a/numpy/typing/__init__.py +++ b/numpy/typing/__init__.py @@ -5,13 +5,6 @@ Typing (:mod:`numpy.typing`) .. versionadded:: 1.20 -.. warning:: - - Some of the types in this module rely on features only present in - the standard library in Python 3.8 and greater. If you want to use - these types in earlier versions of Python, you should install the - typing-extensions_ package. - Large parts of the NumPy API have PEP-484-style type annotations. In addition a number of type aliases are available to users, most prominently the two below: diff --git a/numpy/typing/tests/test_typing_extensions.py b/numpy/typing/tests/test_typing_extensions.py deleted file mode 100644 index f59f222fb..000000000 --- a/numpy/typing/tests/test_typing_extensions.py +++ /dev/null @@ -1,35 +0,0 @@ -"""Tests for the optional typing-extensions dependency.""" - -import sys -import textwrap -import subprocess - -CODE = textwrap.dedent(r""" - import sys - import importlib - - assert "typing_extensions" not in sys.modules - assert "numpy.typing" not in sys.modules - - # Importing `typing_extensions` will now raise an `ImportError` - sys.modules["typing_extensions"] = None - assert importlib.import_module("numpy.typing") -""") - - -def test_no_typing_extensions() -> None: - """Import `numpy.typing` in the absence of typing-extensions. - - Notes - ----- - Ideally, we'd just run the normal typing tests in an environment where - typing-extensions is not installed, but unfortunatelly this is currently - impossible as it is an indirect hard dependency of pytest. - - """ - p = subprocess.run([sys.executable, '-c', CODE], capture_output=True) - if p.returncode: - raise AssertionError( - f"Non-zero return code: {p.returncode!r}\n\n{p.stderr.decode()}" - ) - -- cgit v1.2.1 From 82396851773c37220b0ac543c51b7a896ea75d96 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Tue, 31 Aug 2021 12:05:51 +0200 Subject: STY: Use the PEP 457 positional-only syntax in `numpy.typing` --- numpy/typing/_callable.py | 178 +++++++++++++++++++++++----------------------- numpy/typing/_ufunc.pyi | 24 ++++--- 2 files changed, 102 insertions(+), 100 deletions(-) (limited to 'numpy/typing') diff --git a/numpy/typing/_callable.py b/numpy/typing/_callable.py index 63a8153af..44ad5c291 100644 --- a/numpy/typing/_callable.py +++ b/numpy/typing/_callable.py @@ -62,264 +62,264 @@ _GenericType_co = TypeVar("_GenericType_co", covariant=True, bound=generic) class _BoolOp(Protocol[_GenericType_co]): @overload - def __call__(self, __other: _BoolLike_co) -> _GenericType_co: ... + def __call__(self, other: _BoolLike_co, /) -> _GenericType_co: ... @overload # platform dependent - def __call__(self, __other: int) -> int_: ... + def __call__(self, other: int, /) -> int_: ... @overload - def __call__(self, __other: float) -> float64: ... + def __call__(self, other: float, /) -> float64: ... @overload - def __call__(self, __other: complex) -> complex128: ... + def __call__(self, other: complex, /) -> complex128: ... @overload - def __call__(self, __other: _NumberType) -> _NumberType: ... + def __call__(self, other: _NumberType, /) -> _NumberType: ... class _BoolBitOp(Protocol[_GenericType_co]): @overload - def __call__(self, __other: _BoolLike_co) -> _GenericType_co: ... + def __call__(self, other: _BoolLike_co, /) -> _GenericType_co: ... @overload # platform dependent - def __call__(self, __other: int) -> int_: ... + def __call__(self, other: int, /) -> int_: ... @overload - def __call__(self, __other: _IntType) -> _IntType: ... + def __call__(self, other: _IntType, /) -> _IntType: ... class _BoolSub(Protocol): - # Note that `__other: bool_` is absent here + # Note that `other: bool_` is absent here @overload - def __call__(self, __other: bool) -> NoReturn: ... + def __call__(self, other: bool, /) -> NoReturn: ... @overload # platform dependent - def __call__(self, __other: int) -> int_: ... + def __call__(self, other: int, /) -> int_: ... @overload - def __call__(self, __other: float) -> float64: ... + def __call__(self, other: float, /) -> float64: ... @overload - def __call__(self, __other: complex) -> complex128: ... + def __call__(self, other: complex, /) -> complex128: ... @overload - def __call__(self, __other: _NumberType) -> _NumberType: ... + def __call__(self, other: _NumberType, /) -> _NumberType: ... class _BoolTrueDiv(Protocol): @overload - def __call__(self, __other: float | _IntLike_co) -> float64: ... + def __call__(self, other: float | _IntLike_co, /) -> float64: ... @overload - def __call__(self, __other: complex) -> complex128: ... + def __call__(self, other: complex, /) -> complex128: ... @overload - def __call__(self, __other: _NumberType) -> _NumberType: ... + def __call__(self, other: _NumberType, /) -> _NumberType: ... class _BoolMod(Protocol): @overload - def __call__(self, __other: _BoolLike_co) -> int8: ... + def __call__(self, other: _BoolLike_co, /) -> int8: ... @overload # platform dependent - def __call__(self, __other: int) -> int_: ... + def __call__(self, other: int, /) -> int_: ... @overload - def __call__(self, __other: float) -> float64: ... + def __call__(self, other: float, /) -> float64: ... @overload - def __call__(self, __other: _IntType) -> _IntType: ... + def __call__(self, other: _IntType, /) -> _IntType: ... @overload - def __call__(self, __other: _FloatType) -> _FloatType: ... + def __call__(self, other: _FloatType, /) -> _FloatType: ... class _BoolDivMod(Protocol): @overload - def __call__(self, __other: _BoolLike_co) -> _2Tuple[int8]: ... + def __call__(self, other: _BoolLike_co, /) -> _2Tuple[int8]: ... @overload # platform dependent - def __call__(self, __other: int) -> _2Tuple[int_]: ... + def __call__(self, other: int, /) -> _2Tuple[int_]: ... @overload - def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... + def __call__(self, other: float, /) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... @overload - def __call__(self, __other: _IntType) -> _2Tuple[_IntType]: ... + def __call__(self, other: _IntType, /) -> _2Tuple[_IntType]: ... @overload - def __call__(self, __other: _FloatType) -> _2Tuple[_FloatType]: ... + def __call__(self, other: _FloatType, /) -> _2Tuple[_FloatType]: ... class _TD64Div(Protocol[_NumberType_co]): @overload - def __call__(self, __other: timedelta64) -> _NumberType_co: ... + def __call__(self, other: timedelta64, /) -> _NumberType_co: ... @overload - def __call__(self, __other: _BoolLike_co) -> NoReturn: ... + def __call__(self, other: _BoolLike_co, /) -> NoReturn: ... @overload - def __call__(self, __other: _FloatLike_co) -> timedelta64: ... + def __call__(self, other: _FloatLike_co, /) -> timedelta64: ... class _IntTrueDiv(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> floating[_NBit1]: ... + def __call__(self, other: bool, /) -> floating[_NBit1]: ... @overload - def __call__(self, __other: int) -> floating[_NBit1 | _NBitInt]: ... + def __call__(self, other: int, /) -> floating[_NBit1 | _NBitInt]: ... @overload - def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... + def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ... @overload def __call__( - self, __other: complex + self, other: complex, /, ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... @overload - def __call__(self, __other: integer[_NBit2]) -> floating[_NBit1 | _NBit2]: ... + def __call__(self, other: integer[_NBit2], /) -> floating[_NBit1 | _NBit2]: ... class _UnsignedIntOp(Protocol[_NBit1]): # NOTE: `uint64 + signedinteger -> float64` @overload - def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ... + def __call__(self, other: bool, /) -> unsignedinteger[_NBit1]: ... @overload def __call__( - self, __other: int | signedinteger[Any] + self, other: int | signedinteger[Any], / ) -> Any: ... @overload - def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... + def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ... @overload def __call__( - self, __other: complex + self, other: complex, /, ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... @overload def __call__( - self, __other: unsignedinteger[_NBit2] + self, other: unsignedinteger[_NBit2], / ) -> unsignedinteger[_NBit1 | _NBit2]: ... class _UnsignedIntBitOp(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ... + def __call__(self, other: bool, /) -> unsignedinteger[_NBit1]: ... @overload - def __call__(self, __other: int) -> signedinteger[Any]: ... + def __call__(self, other: int, /) -> signedinteger[Any]: ... @overload - def __call__(self, __other: signedinteger[Any]) -> signedinteger[Any]: ... + def __call__(self, other: signedinteger[Any], /) -> signedinteger[Any]: ... @overload def __call__( - self, __other: unsignedinteger[_NBit2] + self, other: unsignedinteger[_NBit2], / ) -> unsignedinteger[_NBit1 | _NBit2]: ... class _UnsignedIntMod(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ... + def __call__(self, other: bool, /) -> unsignedinteger[_NBit1]: ... @overload def __call__( - self, __other: int | signedinteger[Any] + self, other: int | signedinteger[Any], / ) -> Any: ... @overload - def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... + def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ... @overload def __call__( - self, __other: unsignedinteger[_NBit2] + self, other: unsignedinteger[_NBit2], / ) -> unsignedinteger[_NBit1 | _NBit2]: ... class _UnsignedIntDivMod(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> _2Tuple[signedinteger[_NBit1]]: ... + def __call__(self, other: bool, /) -> _2Tuple[signedinteger[_NBit1]]: ... @overload def __call__( - self, __other: int | signedinteger[Any] + self, other: int | signedinteger[Any], / ) -> _2Tuple[Any]: ... @overload - def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... + def __call__(self, other: float, /) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... @overload def __call__( - self, __other: unsignedinteger[_NBit2] + self, other: unsignedinteger[_NBit2], / ) -> _2Tuple[unsignedinteger[_NBit1 | _NBit2]]: ... class _SignedIntOp(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> signedinteger[_NBit1]: ... + def __call__(self, other: bool, /) -> signedinteger[_NBit1]: ... @overload - def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ... + def __call__(self, other: int, /) -> signedinteger[_NBit1 | _NBitInt]: ... @overload - def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... + def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ... @overload def __call__( - self, __other: complex + self, other: complex, /, ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... @overload def __call__( - self, __other: signedinteger[_NBit2] + self, other: signedinteger[_NBit2], /, ) -> signedinteger[_NBit1 | _NBit2]: ... class _SignedIntBitOp(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> signedinteger[_NBit1]: ... + def __call__(self, other: bool, /) -> signedinteger[_NBit1]: ... @overload - def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ... + def __call__(self, other: int, /) -> signedinteger[_NBit1 | _NBitInt]: ... @overload def __call__( - self, __other: signedinteger[_NBit2] + self, other: signedinteger[_NBit2], /, ) -> signedinteger[_NBit1 | _NBit2]: ... class _SignedIntMod(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> signedinteger[_NBit1]: ... + def __call__(self, other: bool, /) -> signedinteger[_NBit1]: ... @overload - def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ... + def __call__(self, other: int, /) -> signedinteger[_NBit1 | _NBitInt]: ... @overload - def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... + def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ... @overload def __call__( - self, __other: signedinteger[_NBit2] + self, other: signedinteger[_NBit2], /, ) -> signedinteger[_NBit1 | _NBit2]: ... class _SignedIntDivMod(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> _2Tuple[signedinteger[_NBit1]]: ... + def __call__(self, other: bool, /) -> _2Tuple[signedinteger[_NBit1]]: ... @overload - def __call__(self, __other: int) -> _2Tuple[signedinteger[_NBit1 | _NBitInt]]: ... + def __call__(self, other: int, /) -> _2Tuple[signedinteger[_NBit1 | _NBitInt]]: ... @overload - def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... + def __call__(self, other: float, /) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... @overload def __call__( - self, __other: signedinteger[_NBit2] + self, other: signedinteger[_NBit2], /, ) -> _2Tuple[signedinteger[_NBit1 | _NBit2]]: ... class _FloatOp(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> floating[_NBit1]: ... + def __call__(self, other: bool, /) -> floating[_NBit1]: ... @overload - def __call__(self, __other: int) -> floating[_NBit1 | _NBitInt]: ... + def __call__(self, other: int, /) -> floating[_NBit1 | _NBitInt]: ... @overload - def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... + def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ... @overload def __call__( - self, __other: complex + self, other: complex, /, ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... @overload def __call__( - self, __other: integer[_NBit2] | floating[_NBit2] + self, other: integer[_NBit2] | floating[_NBit2], / ) -> floating[_NBit1 | _NBit2]: ... class _FloatMod(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> floating[_NBit1]: ... + def __call__(self, other: bool, /) -> floating[_NBit1]: ... @overload - def __call__(self, __other: int) -> floating[_NBit1 | _NBitInt]: ... + def __call__(self, other: int, /) -> floating[_NBit1 | _NBitInt]: ... @overload - def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ... + def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ... @overload def __call__( - self, __other: integer[_NBit2] | floating[_NBit2] + self, other: integer[_NBit2] | floating[_NBit2], / ) -> floating[_NBit1 | _NBit2]: ... class _FloatDivMod(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> _2Tuple[floating[_NBit1]]: ... + def __call__(self, other: bool, /) -> _2Tuple[floating[_NBit1]]: ... @overload - def __call__(self, __other: int) -> _2Tuple[floating[_NBit1 | _NBitInt]]: ... + def __call__(self, other: int, /) -> _2Tuple[floating[_NBit1 | _NBitInt]]: ... @overload - def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... + def __call__(self, other: float, /) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... @overload def __call__( - self, __other: integer[_NBit2] | floating[_NBit2] + self, other: integer[_NBit2] | floating[_NBit2], / ) -> _2Tuple[floating[_NBit1 | _NBit2]]: ... class _ComplexOp(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> complexfloating[_NBit1, _NBit1]: ... + def __call__(self, other: bool, /) -> complexfloating[_NBit1, _NBit1]: ... @overload - def __call__(self, __other: int) -> complexfloating[_NBit1 | _NBitInt, _NBit1 | _NBitInt]: ... + def __call__(self, other: int, /) -> complexfloating[_NBit1 | _NBitInt, _NBit1 | _NBitInt]: ... @overload def __call__( - self, __other: complex + self, other: complex, /, ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... @overload def __call__( self, - __other: Union[ + other: Union[ integer[_NBit2], floating[_NBit2], complexfloating[_NBit2, _NBit2], - ] + ], /, ) -> complexfloating[_NBit1 | _NBit2, _NBit1 | _NBit2]: ... class _NumberOp(Protocol): - def __call__(self, __other: _NumberLike_co) -> Any: ... + def __call__(self, other: _NumberLike_co, /) -> Any: ... class _ComparisonOp(Protocol[_T1, _T2]): @overload - def __call__(self, __other: _T1) -> bool_: ... + def __call__(self, other: _T1, /) -> bool_: ... @overload - def __call__(self, __other: _T2) -> NDArray[bool_]: ... + def __call__(self, other: _T2, /) -> NDArray[bool_]: ... diff --git a/numpy/typing/_ufunc.pyi b/numpy/typing/_ufunc.pyi index 37e6c008f..1be3500c1 100644 --- a/numpy/typing/_ufunc.pyi +++ b/numpy/typing/_ufunc.pyi @@ -105,8 +105,9 @@ class _UFunc_Nin1_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]): def at( self, - __a: NDArray[Any], - __indices: _ArrayLikeInt_co, + a: NDArray[Any], + indices: _ArrayLikeInt_co, + /, ) -> None: ... class _UFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]): @@ -158,9 +159,10 @@ class _UFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]): def at( self, - __a: NDArray[Any], - __indices: _ArrayLikeInt_co, - __b: ArrayLike, + a: NDArray[Any], + indices: _ArrayLikeInt_co, + b: ArrayLike, + /, ) -> None: ... def reduce( @@ -195,9 +197,9 @@ class _UFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]): @overload def outer( self, - __A: _ScalarLike_co, - __B: _ScalarLike_co, - *, + A: _ScalarLike_co, + B: _ScalarLike_co, + /, *, out: None = ..., where: None | _ArrayLikeBool_co = ..., casting: _CastingKind = ..., @@ -210,9 +212,9 @@ class _UFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]): @overload def outer( # type: ignore[misc] self, - __A: ArrayLike, - __B: ArrayLike, - *, + A: ArrayLike, + B: ArrayLike, + /, *, out: None | NDArray[Any] | Tuple[NDArray[Any]] = ..., where: None | _ArrayLikeBool_co = ..., casting: _CastingKind = ..., -- cgit v1.2.1 From 5d86d8c9cc70f87b4aab10682d101acd8c4fb781 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Tue, 31 Aug 2021 16:38:14 +0200 Subject: TST: Add tests for `np.floating.is_integer` --- numpy/typing/tests/data/fail/scalars.py | 1 - 1 file changed, 1 deletion(-) (limited to 'numpy/typing') diff --git a/numpy/typing/tests/data/fail/scalars.py b/numpy/typing/tests/data/fail/scalars.py index 099418e67..94fe3f71e 100644 --- a/numpy/typing/tests/data/fail/scalars.py +++ b/numpy/typing/tests/data/fail/scalars.py @@ -87,7 +87,6 @@ round(c8) # E: No overload variant c8.__getnewargs__() # E: Invalid self argument f2.__getnewargs__() # E: Invalid self argument -f2.is_integer() # E: Invalid self argument f2.hex() # E: Invalid self argument np.float16.fromhex("0x0.0p+0") # E: Invalid self argument f2.__trunc__() # E: Invalid self argument -- cgit v1.2.1 From 9f11564c455f00fe5faa0a92aa02f1f3f59fc901 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Tue, 31 Aug 2021 17:41:36 +0200 Subject: ENH: Add `integer.is_integer` Match `int.is_integer`, which was added in python/cpython#6121 --- numpy/typing/tests/data/reveal/scalars.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'numpy/typing') diff --git a/numpy/typing/tests/data/reveal/scalars.py b/numpy/typing/tests/data/reveal/scalars.py index c36813004..e83d579e9 100644 --- a/numpy/typing/tests/data/reveal/scalars.py +++ b/numpy/typing/tests/data/reveal/scalars.py @@ -156,3 +156,5 @@ reveal_type(round(f8, 3)) # E: {float64} if sys.version_info >= (3, 9): reveal_type(f8.__ceil__()) # E: int reveal_type(f8.__floor__()) # E: int + +reveal_type(i8.is_integer()) # E: Literal[True] -- cgit v1.2.1 From f384f9ec18713fb3c17b36bd0beecdd06322d1cf Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Thu, 2 Sep 2021 16:27:13 +0200 Subject: TST: Update the IO-related typing tests --- numpy/typing/tests/data/fail/npyio.py | 6 +++--- numpy/typing/tests/data/reveal/npyio.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) (limited to 'numpy/typing') diff --git a/numpy/typing/tests/data/fail/npyio.py b/numpy/typing/tests/data/fail/npyio.py index 8edabf2b3..c91b4c9cb 100644 --- a/numpy/typing/tests/data/fail/npyio.py +++ b/numpy/typing/tests/data/fail/npyio.py @@ -21,10 +21,10 @@ np.savez(str_file, AR_i8) # E: incompatible type np.savez_compressed(bytes_path, AR_i8) # E: incompatible type np.savez_compressed(str_file, AR_i8) # E: incompatible type -np.loadtxt(bytes_path) # E: No overload variant +np.loadtxt(bytes_path) # E: incompatible type np.fromregex(bytes_path, ".", np.int64) # E: No overload variant -np.recfromtxt(bytes_path) # E: No overload variant +np.recfromtxt(bytes_path) # E: incompatible type -np.recfromcsv(bytes_path) # E: No overload variant +np.recfromcsv(bytes_path) # E: incompatible type diff --git a/numpy/typing/tests/data/reveal/npyio.py b/numpy/typing/tests/data/reveal/npyio.py index 05005eb1c..d66201dd3 100644 --- a/numpy/typing/tests/data/reveal/npyio.py +++ b/numpy/typing/tests/data/reveal/npyio.py @@ -16,6 +16,16 @@ npz_file: np.lib.npyio.NpzFile AR_i8: npt.NDArray[np.int64] AR_LIKE_f8: List[float] +class BytesWriter: + def write(self, data: bytes) -> None: ... + +class BytesReader: + def read(self, n: int = ...) -> bytes: ... + def seek(self, offset: int, whence: int = ...) -> int: ... + +bytes_writer: BytesWriter +bytes_reader: BytesReader + reveal_type(bag_obj.a) # E: int reveal_type(bag_obj.b) # E: int @@ -33,18 +43,22 @@ with npz_file as f: reveal_type(np.load(bytes_file)) # E: Any reveal_type(np.load(pathlib_path, allow_pickle=True)) # E: Any reveal_type(np.load(str_path, encoding="bytes")) # E: Any +reveal_type(np.load(bytes_reader)) # E: Any reveal_type(np.save(bytes_file, AR_LIKE_f8)) # E: None reveal_type(np.save(pathlib_path, AR_i8, allow_pickle=True)) # E: None reveal_type(np.save(str_path, AR_LIKE_f8)) # E: None +reveal_type(np.save(bytes_writer, AR_LIKE_f8)) # E: None reveal_type(np.savez(bytes_file, AR_LIKE_f8)) # E: None reveal_type(np.savez(pathlib_path, ar1=AR_i8, ar2=AR_i8)) # E: None reveal_type(np.savez(str_path, AR_LIKE_f8, ar1=AR_i8)) # E: None +reveal_type(np.savez(bytes_writer, AR_LIKE_f8, ar1=AR_i8)) # E: None reveal_type(np.savez_compressed(bytes_file, AR_LIKE_f8)) # E: None reveal_type(np.savez_compressed(pathlib_path, ar1=AR_i8, ar2=AR_i8)) # E: None reveal_type(np.savez_compressed(str_path, AR_LIKE_f8, ar1=AR_i8)) # E: None +reveal_type(np.savez_compressed(bytes_writer, AR_LIKE_f8, ar1=AR_i8)) # E: None reveal_type(np.loadtxt(bytes_file)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.loadtxt(pathlib_path, dtype=np.str_)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] @@ -52,11 +66,13 @@ reveal_type(np.loadtxt(str_path, dtype=str, skiprows=2)) # E: numpy.ndarray[Any reveal_type(np.loadtxt(str_file, comments="test")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.loadtxt(str_path, delimiter="\n")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.loadtxt(str_path, ndmin=2)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.loadtxt(["1", "2", "3"])) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.fromregex(bytes_file, "test", np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.fromregex(str_file, b"test", dtype=float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] reveal_type(np.fromregex(str_path, re.compile("test"), dtype=np.str_, encoding="utf8")) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] reveal_type(np.fromregex(pathlib_path, "test", np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.fromregex(bytes_reader, "test", np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.genfromtxt(bytes_file)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.genfromtxt(pathlib_path, dtype=np.str_)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] @@ -64,9 +80,12 @@ reveal_type(np.genfromtxt(str_path, dtype=str, skiprows=2)) # E: numpy.ndarray[ reveal_type(np.genfromtxt(str_file, comments="test")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.genfromtxt(str_path, delimiter="\n")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.genfromtxt(str_path, ndmin=2)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(np.genfromtxt(["1", "2", "3"], ndmin=2)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(np.recfromtxt(bytes_file)) # E: numpy.recarray[Any, numpy.dtype[numpy.void]] reveal_type(np.recfromtxt(pathlib_path, usemask=True)) # E: numpy.ma.mrecords.MaskedRecords[Any, numpy.dtype[numpy.void]] +reveal_type(np.recfromtxt(["1", "2", "3"])) # E: numpy.recarray[Any, numpy.dtype[numpy.void]] reveal_type(np.recfromcsv(bytes_file)) # E: numpy.recarray[Any, numpy.dtype[numpy.void]] reveal_type(np.recfromcsv(pathlib_path, usemask=True)) # E: numpy.ma.mrecords.MaskedRecords[Any, numpy.dtype[numpy.void]] +reveal_type(np.recfromcsv(["1", "2", "3"])) # E: numpy.recarray[Any, numpy.dtype[numpy.void]] -- cgit v1.2.1