diff options
| author | Sebastian Berg <sebastianb@nvidia.com> | 2023-05-08 17:37:12 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-08 17:37:12 +0200 |
| commit | e4d2cdb5158358349d8f4ee3358f75420a38ebf5 (patch) | |
| tree | dc19fc3aba02d5141a6fc07399db8e8e18b31f26 | |
| parent | c7724ee776f3aa447d89170809aace0461ccacf0 (diff) | |
| parent | 2cd27f7c2dcf8599196709d9774960b51eca6a1c (diff) | |
| download | numpy-e4d2cdb5158358349d8f4ee3358f75420a38ebf5.tar.gz | |
Merge pull request #23735 from BvB93/typing_1_25
TYP: Update type annotations for the numpy 1.25 release
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | numpy/__init__.pyi | 32 | ||||
| -rw-r--r-- | numpy/core/einsumfunc.pyi | 53 | ||||
| -rw-r--r-- | numpy/core/fromnumeric.pyi | 4 | ||||
| -rw-r--r-- | numpy/exceptions.pyi | 24 | ||||
| -rw-r--r-- | numpy/ma/__init__.pyi | 1 | ||||
| -rw-r--r-- | numpy/ma/core.pyi | 3 | ||||
| -rw-r--r-- | numpy/typing/tests/data/fail/einsumfunc.pyi | 3 | ||||
| -rw-r--r-- | numpy/typing/tests/data/reveal/einsumfunc.pyi | 5 | ||||
| -rw-r--r-- | numpy/typing/tests/data/reveal/modules.pyi | 2 |
10 files changed, 88 insertions, 40 deletions
diff --git a/.gitignore b/.gitignore index c15a486d9..e5784971e 100644 --- a/.gitignore +++ b/.gitignore @@ -179,6 +179,7 @@ benchmarks/html benchmarks/env benchmarks/numpy benchmarks/_asv_compare.conf.json +test.obj # cythonized files cythonize.dat numpy/random/_mtrand/_mtrand.c diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 8627f6c60..ef1f9046a 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -209,6 +209,8 @@ from numpy import ( random as random, testing as testing, version as version, + exceptions as exceptions, + dtypes as dtypes, ) from numpy.core import defchararray, records @@ -411,6 +413,15 @@ from numpy.core.shape_base import ( vstack as vstack, ) +from numpy.exceptions import ( + ComplexWarning as ComplexWarning, + ModuleDeprecationWarning as ModuleDeprecationWarning, + VisibleDeprecationWarning as VisibleDeprecationWarning, + TooHardError as TooHardError, + DTypePromotionError as DTypePromotionError, + AxisError as AxisError, +) + from numpy.lib import ( emath as emath, ) @@ -664,13 +675,6 @@ test: PytestTester # # Placeholders for classes -# Some of these are aliases; others are wrappers with an identical signature -round_ = around -product = prod -cumproduct = cumprod -sometrue = any -alltrue = all - def show_config() -> None: ... _NdArraySubClass = TypeVar("_NdArraySubClass", bound=ndarray[Any, Any]) @@ -3319,22 +3323,8 @@ class _CopyMode(enum.Enum): NEVER: L[2] # Warnings -class ModuleDeprecationWarning(DeprecationWarning): ... -class VisibleDeprecationWarning(UserWarning): ... -class ComplexWarning(RuntimeWarning): ... class RankWarning(UserWarning): ... -# Errors -class TooHardError(RuntimeError): ... - -class AxisError(ValueError, IndexError): - axis: None | int - ndim: None | int - @overload - def __init__(self, axis: str, ndim: None = ..., msg_prefix: None = ...) -> None: ... - @overload - def __init__(self, axis: int, ndim: int, msg_prefix: None | str = ...) -> None: ... - _CallType = TypeVar("_CallType", bound=_ErrFunc | _SupportsWrite[str]) class errstate(Generic[_CallType], ContextDecorator): diff --git a/numpy/core/einsumfunc.pyi b/numpy/core/einsumfunc.pyi index c811a5783..ad483bb90 100644 --- a/numpy/core/einsumfunc.pyi +++ b/numpy/core/einsumfunc.pyi @@ -5,10 +5,6 @@ from numpy import ( ndarray, dtype, bool_, - unsignedinteger, - signedinteger, - floating, - complexfloating, number, _OrderKACF, ) @@ -18,12 +14,14 @@ from numpy._typing import ( _ArrayLikeInt_co, _ArrayLikeFloat_co, _ArrayLikeComplex_co, + _ArrayLikeObject_co, _DTypeLikeBool, _DTypeLikeUInt, _DTypeLikeInt, _DTypeLikeFloat, _DTypeLikeComplex, _DTypeLikeComplex_co, + _DTypeLikeObject, ) _ArrayType = TypeVar( @@ -132,6 +130,51 @@ def einsum( optimize: _OptimizeKind = ..., ) -> _ArrayType: ... +@overload +def einsum( + subscripts: str | _ArrayLikeInt_co, + /, + *operands: _ArrayLikeObject_co, + out: None = ..., + dtype: None | _DTypeLikeObject = ..., + order: _OrderKACF = ..., + casting: _CastingSafe = ..., + optimize: _OptimizeKind = ..., +) -> Any: ... +@overload +def einsum( + subscripts: str | _ArrayLikeInt_co, + /, + *operands: Any, + casting: _CastingUnsafe, + dtype: None | _DTypeLikeObject = ..., + out: None = ..., + order: _OrderKACF = ..., + optimize: _OptimizeKind = ..., +) -> Any: ... +@overload +def einsum( + subscripts: str | _ArrayLikeInt_co, + /, + *operands: _ArrayLikeObject_co, + out: _ArrayType, + dtype: None | _DTypeLikeObject = ..., + order: _OrderKACF = ..., + casting: _CastingSafe = ..., + optimize: _OptimizeKind = ..., +) -> _ArrayType: ... +@overload +def einsum( + subscripts: str | _ArrayLikeInt_co, + /, + *operands: Any, + out: _ArrayType, + casting: _CastingUnsafe, + dtype: None | _DTypeLikeObject = ..., + order: _OrderKACF = ..., + optimize: _OptimizeKind = ..., +) -> _ArrayType: ... + # NOTE: `einsum_call` is a hidden kwarg unavailable for public use. # It is therefore excluded from the signatures below. # NOTE: In practice the list consists of a `str` (first element) @@ -139,6 +182,6 @@ def einsum( def einsum_path( subscripts: str | _ArrayLikeInt_co, /, - *operands: _ArrayLikeComplex_co, + *operands: _ArrayLikeComplex_co | _DTypeLikeObject, optimize: _OptimizeKind = ..., ) -> tuple[list[Any], str]: ... diff --git a/numpy/core/fromnumeric.pyi b/numpy/core/fromnumeric.pyi index 17b17819d..43d178557 100644 --- a/numpy/core/fromnumeric.pyi +++ b/numpy/core/fromnumeric.pyi @@ -1047,3 +1047,7 @@ def var( *, where: _ArrayLikeBool_co = ..., ) -> _ArrayType: ... + +max = amax +min = amin +round = around diff --git a/numpy/exceptions.pyi b/numpy/exceptions.pyi index 53b7a0c16..c76a0946b 100644 --- a/numpy/exceptions.pyi +++ b/numpy/exceptions.pyi @@ -1,8 +1,18 @@ -from numpy.exceptions import ( - ComplexWarning as ComplexWarning, - ModuleDeprecationWarning as ModuleDeprecationWarning, - VisibleDeprecationWarning as VisibleDeprecationWarning, - TooHardError as TooHardError, - AxisError as AxisError, -) +from typing import overload +__all__: list[str] + +class ComplexWarning(RuntimeWarning): ... +class ModuleDeprecationWarning(DeprecationWarning): ... +class VisibleDeprecationWarning(UserWarning): ... +class TooHardError(RuntimeError): ... +class DTypePromotionError(TypeError): ... + +class AxisError(ValueError, IndexError): + axis: None | int + ndim: None | int + @overload + def __init__(self, axis: str, ndim: None = ..., msg_prefix: None = ...) -> None: ... + @overload + def __init__(self, axis: int, ndim: int, msg_prefix: None | str = ...) -> None: ... + def __str__(self) -> str: ... diff --git a/numpy/ma/__init__.pyi b/numpy/ma/__init__.pyi index 7f5cb56a8..ce72383e5 100644 --- a/numpy/ma/__init__.pyi +++ b/numpy/ma/__init__.pyi @@ -155,7 +155,6 @@ from numpy.ma.core import ( resize as resize, right_shift as right_shift, round as round, - round_ as round_, set_fill_value as set_fill_value, shape as shape, sin as sin, diff --git a/numpy/ma/core.pyi b/numpy/ma/core.pyi index 15f37c422..e94ebce3c 100644 --- a/numpy/ma/core.pyi +++ b/numpy/ma/core.pyi @@ -435,8 +435,7 @@ def size(obj, axis=...): ... def diff(a, /, n=..., axis=..., prepend=..., append=...): ... def where(condition, x=..., y=...): ... def choose(indices, choices, out=..., mode=...): ... -def round_(a, decimals=..., out=...): ... -round = round_ +def round(a, decimals=..., out=...): ... def inner(a, b): ... innerproduct = inner diff --git a/numpy/typing/tests/data/fail/einsumfunc.pyi b/numpy/typing/tests/data/fail/einsumfunc.pyi index f0e3f1e95..2d1f37418 100644 --- a/numpy/typing/tests/data/fail/einsumfunc.pyi +++ b/numpy/typing/tests/data/fail/einsumfunc.pyi @@ -4,12 +4,9 @@ import numpy as np AR_i: np.ndarray[Any, np.dtype[np.int64]] AR_f: np.ndarray[Any, np.dtype[np.float64]] AR_m: np.ndarray[Any, np.dtype[np.timedelta64]] -AR_O: np.ndarray[Any, np.dtype[np.object_]] AR_U: np.ndarray[Any, np.dtype[np.str_]] np.einsum("i,i->i", AR_i, AR_m) # E: incompatible type -np.einsum("i,i->i", AR_O, AR_O) # E: incompatible type np.einsum("i,i->i", AR_f, AR_f, dtype=np.int32) # E: incompatible type -np.einsum("i,i->i", AR_i, AR_i, dtype=np.timedelta64, casting="unsafe") # E: No overload variant np.einsum("i,i->i", AR_i, AR_i, out=AR_U) # E: Value of type variable "_ArrayType" of "einsum" cannot be np.einsum("i,i->i", AR_i, AR_i, out=AR_U, casting="unsafe") # E: No overload variant diff --git a/numpy/typing/tests/data/reveal/einsumfunc.pyi b/numpy/typing/tests/data/reveal/einsumfunc.pyi index d5f930149..5f6415f27 100644 --- a/numpy/typing/tests/data/reveal/einsumfunc.pyi +++ b/numpy/typing/tests/data/reveal/einsumfunc.pyi @@ -1,5 +1,6 @@ from typing import Any import numpy as np +import numpy.typing as npt AR_LIKE_b: list[bool] AR_LIKE_u: list[np.uint32] @@ -7,10 +8,12 @@ AR_LIKE_i: list[int] AR_LIKE_f: list[float] AR_LIKE_c: list[complex] AR_LIKE_U: list[str] +AR_o: npt.NDArray[np.object_] -OUT_f: np.ndarray[Any, np.dtype[np.float64]] +OUT_f: npt.NDArray[np.float64] reveal_type(np.einsum("i,i->i", AR_LIKE_b, AR_LIKE_b)) # E: Any +reveal_type(np.einsum("i,i->i", AR_o, AR_o)) # E: Any reveal_type(np.einsum("i,i->i", AR_LIKE_u, AR_LIKE_u)) # E: Any reveal_type(np.einsum("i,i->i", AR_LIKE_i, AR_LIKE_i)) # E: Any reveal_type(np.einsum("i,i->i", AR_LIKE_f, AR_LIKE_f)) # E: Any diff --git a/numpy/typing/tests/data/reveal/modules.pyi b/numpy/typing/tests/data/reveal/modules.pyi index ba830eb0d..4191c564a 100644 --- a/numpy/typing/tests/data/reveal/modules.pyi +++ b/numpy/typing/tests/data/reveal/modules.pyi @@ -16,6 +16,8 @@ reveal_type(np.random) # E: ModuleType reveal_type(np.rec) # E: ModuleType reveal_type(np.testing) # E: ModuleType reveal_type(np.version) # E: ModuleType +reveal_type(np.exceptions) # E: ModuleType +reveal_type(np.dtypes) # E: ModuleType reveal_type(np.lib.format) # E: ModuleType reveal_type(np.lib.mixins) # E: ModuleType |
