diff options
author | Bas van Beek <b.f.van.beek@vu.nl> | 2021-03-11 14:45:22 +0100 |
---|---|---|
committer | Ralf Gommers <ralf.gommers@gmail.com> | 2021-03-12 16:56:32 +0100 |
commit | 025dcca2cb3c240e0af1d075d24a5bba18421593 (patch) | |
tree | eedd01802d87b8ac48e0d80a9b0fccaa4567173f | |
parent | 335b6cb399ecaa869cf78c49984eb1dcc412b8bc (diff) | |
download | numpy-025dcca2cb3c240e0af1d075d24a5bba18421593.tar.gz |
MAINT: Tighten the type-constraints of the `__array__` dtype parameter
The likes of `np.array(..., dtype=dtype)` will always pass `np.dtype` instances to `__array__(self, dtype=...)`, so that latter should not have to take arbitrary dtype-like objects but only actual `np.dtype` instances.
-rw-r--r-- | numpy/__init__.pyi | 6 | ||||
-rw-r--r-- | numpy/typing/tests/data/pass/arithmetic.py | 2 | ||||
-rw-r--r-- | numpy/typing/tests/data/pass/array_like.py | 12 | ||||
-rw-r--r-- | numpy/typing/tests/data/pass/flatiter.py | 2 | ||||
-rw-r--r-- | numpy/typing/tests/data/pass/ufunclike.py | 4 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/flatiter.py | 2 |
6 files changed, 13 insertions, 15 deletions
diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 9cdc4f2e1..b1ab7c14f 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -853,7 +853,7 @@ class flatiter(Generic[_NdArraySubClass]): @overload def __array__(self: flatiter[ndarray[Any, _DType]], __dtype: None = ...) -> ndarray[Any, _DType]: ... @overload - def __array__(self, __dtype: DTypeLike) -> ndarray[Any, dtype[Any]]: ... + def __array__(self, __dtype: _DType) -> ndarray[Any, _DType]: ... _OrderKACF = Optional[Literal["K", "A", "C", "F"]] _OrderACF = Optional[Literal["A", "C", "F"]] @@ -1362,7 +1362,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __array__(self, __dtype: None = ...) -> ndarray[Any, _DType_co]: ... @overload - def __array__(self, __dtype: DTypeLike) -> ndarray[Any, dtype[Any]]: ... + def __array__(self, __dtype: _DType) -> ndarray[Any, _DType]: ... @property def ctypes(self) -> _ctypes: ... @property @@ -2409,7 +2409,7 @@ class generic(_ArrayOrScalarCommon): @overload def __array__(self: _ScalarType, __dtype: None = ...) -> ndarray[Any, dtype[_ScalarType]]: ... @overload - def __array__(self, __dtype: DTypeLike) -> ndarray[Any, dtype[Any]]: ... + def __array__(self, __dtype: _DType) -> ndarray[Any, _DType]: ... @property def base(self) -> None: ... @property diff --git a/numpy/typing/tests/data/pass/arithmetic.py b/numpy/typing/tests/data/pass/arithmetic.py index 7a297cfc5..62bd79004 100644 --- a/numpy/typing/tests/data/pass/arithmetic.py +++ b/numpy/typing/tests/data/pass/arithmetic.py @@ -25,7 +25,7 @@ i = int(1) class Object: - def __array__(self, dtype: None = None) -> np.ndarray[Any, np.dtype[np.object_]]: + def __array__(self) -> np.ndarray[Any, np.dtype[np.object_]]: ret = np.empty((), dtype=object) ret[()] = self return ret diff --git a/numpy/typing/tests/data/pass/array_like.py b/numpy/typing/tests/data/pass/array_like.py index 563fc08c7..e16d196b6 100644 --- a/numpy/typing/tests/data/pass/array_like.py +++ b/numpy/typing/tests/data/pass/array_like.py @@ -1,7 +1,7 @@ from typing import Any, List, Optional import numpy as np -from numpy.typing import ArrayLike, DTypeLike, _SupportsArray +from numpy.typing import ArrayLike, _SupportsArray x1: ArrayLike = True x2: ArrayLike = 5 @@ -18,20 +18,20 @@ x12: ArrayLike = memoryview(b'foo') class A: - def __array__(self, dtype: DTypeLike = None) -> np.ndarray: + def __array__(self, dtype: Optional[np.dtype] = None) -> np.ndarray: return np.array([1, 2, 3]) x13: ArrayLike = A() scalar: _SupportsArray = np.int64(1) -scalar.__array__(None) +scalar.__array__() array: _SupportsArray = np.array(1) -array.__array__(None) +array.__array__() a: _SupportsArray = A() -a.__array__(None) -a.__array__(dtype=None) +a.__array__() +a.__array__() # Escape hatch for when you mean to make something like an object # array. diff --git a/numpy/typing/tests/data/pass/flatiter.py b/numpy/typing/tests/data/pass/flatiter.py index 4fdf25299..63c839af4 100644 --- a/numpy/typing/tests/data/pass/flatiter.py +++ b/numpy/typing/tests/data/pass/flatiter.py @@ -13,4 +13,4 @@ a[[0, 1, 2]] a[...] a[:] a.__array__() -a.__array__(np.float64) +a.__array__(np.dtype(np.float64)) diff --git a/numpy/typing/tests/data/pass/ufunclike.py b/numpy/typing/tests/data/pass/ufunclike.py index 448ee66bb..7eac89e8f 100644 --- a/numpy/typing/tests/data/pass/ufunclike.py +++ b/numpy/typing/tests/data/pass/ufunclike.py @@ -13,9 +13,7 @@ class Object: def __ge__(self, value: object) -> bool: return True - def __array__( - self, dtype: None = None - ) -> np.ndarray[Any, np.dtype[np.object_]]: + def __array__(self) -> np.ndarray[Any, np.dtype[np.object_]]: ret = np.empty((), dtype=object) ret[()] = self return ret diff --git a/numpy/typing/tests/data/reveal/flatiter.py b/numpy/typing/tests/data/reveal/flatiter.py index 221101ebb..97776dd9f 100644 --- a/numpy/typing/tests/data/reveal/flatiter.py +++ b/numpy/typing/tests/data/reveal/flatiter.py @@ -14,4 +14,4 @@ reveal_type(a[[0, 1, 2]]) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] reveal_type(a[...]) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] reveal_type(a[:]) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] reveal_type(a.__array__()) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] -reveal_type(a.__array__(np.float64)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(a.__array__(np.dtype(np.float64))) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] |