diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/__init__.pyi | 117 | ||||
-rw-r--r-- | numpy/core/src/multiarray/dtypemeta.c | 13 | ||||
-rw-r--r-- | numpy/core/tests/test_deprecations.py | 35 | ||||
-rw-r--r-- | numpy/core/tests/test_half.py | 6 | ||||
-rw-r--r-- | numpy/core/tests/test_numeric.py | 67 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 4 | ||||
-rw-r--r-- | numpy/distutils/intelccompiler.py | 2 | ||||
-rw-r--r-- | numpy/lib/tests/test_regression.py | 3 | ||||
-rw-r--r-- | numpy/typing/__init__.py | 14 | ||||
-rw-r--r-- | numpy/typing/_array_like.py | 6 | ||||
-rw-r--r-- | numpy/typing/_callable.py | 46 | ||||
-rw-r--r-- | numpy/typing/_char_codes.py | 100 | ||||
-rw-r--r-- | numpy/typing/_dtype_like.py | 7 | ||||
-rw-r--r-- | numpy/typing/_extended_precision.py | 24 | ||||
-rw-r--r-- | numpy/typing/_shape.py | 4 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/ndarray_conversion.py | 53 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/scalars.py | 12 | ||||
-rw-r--r-- | numpy/typing/tests/test_generic_alias.py | 4 |
18 files changed, 279 insertions, 238 deletions
diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 0555b0151..6bf9d49a8 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -1238,9 +1238,6 @@ class _ArrayOrScalarCommon: def copy(self: _ArraySelf, order: _OrderKACF = ...) -> _ArraySelf: ... def dump(self, file: str) -> None: ... def dumps(self) -> bytes: ... - def getfield( - self: _ArraySelf, dtype: DTypeLike, offset: int = ... - ) -> _ArraySelf: ... def tobytes(self, order: _OrderKACF = ...) -> bytes: ... # NOTE: `tostring()` is deprecated and therefore excluded # def tostring(self, order=...): ... @@ -1249,14 +1246,6 @@ class _ArrayOrScalarCommon: ) -> None: ... # generics and 0d arrays return builtin scalars def tolist(self) -> Any: ... - @overload - def view(self, type: Type[_NdArraySubClass]) -> _NdArraySubClass: ... - @overload - def view(self: _ArraySelf, dtype: DTypeLike = ...) -> _ArraySelf: ... - @overload - def view( - self, dtype: DTypeLike, type: Type[_NdArraySubClass] - ) -> _NdArraySubClass: ... # TODO: Add proper signatures def __getitem__(self, key) -> Any: ... @@ -1638,6 +1627,12 @@ _T_co = TypeVar("_T_co", covariant=True) _2Tuple = Tuple[_T, _T] _Casting = L["no", "equiv", "safe", "same_kind", "unsafe"] +_DTypeLike = Union[ + dtype[_ScalarType], + Type[_ScalarType], + _SupportsDType[dtype[_ScalarType]], +] + _ArrayUInt_co = NDArray[Union[bool_, unsignedinteger[Any]]] _ArrayInt_co = NDArray[Union[bool_, integer[Any]]] _ArrayFloat_co = NDArray[Union[bool_, integer[Any], floating[Any]]] @@ -1875,6 +1870,53 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): self, *shape: SupportsIndex, order: _OrderACF = ... ) -> ndarray[Any, _DType_co]: ... + @overload + def astype( + self, + dtype: _DTypeLike[_ScalarType], + order: _OrderKACF = ..., + casting: _Casting = ..., + subok: bool = ..., + copy: bool = ..., + ) -> NDArray[_ScalarType]: ... + @overload + def astype( + self, + dtype: DTypeLike, + order: _OrderKACF = ..., + casting: _Casting = ..., + subok: bool = ..., + copy: bool = ..., + ) -> NDArray[Any]: ... + + @overload + def view(self: _ArraySelf) -> _ArraySelf: ... + @overload + def view(self, type: Type[_NdArraySubClass]) -> _NdArraySubClass: ... + @overload + def view(self, dtype: _DTypeLike[_ScalarType]) -> NDArray[_ScalarType]: ... + @overload + def view(self, dtype: DTypeLike) -> NDArray[Any]: ... + @overload + def view( + self, + dtype: DTypeLike, + type: Type[_NdArraySubClass], + ) -> _NdArraySubClass: ... + + @overload + def getfield( + self, + dtype: _DTypeLike[_ScalarType], + offset: SupportsIndex = ... + ) -> NDArray[_ScalarType]: ... + @overload + def getfield( + self, + dtype: DTypeLike, + offset: SupportsIndex = ... + ) -> NDArray[Any]: ... + # Dispatch to the underlying `generic` via protocols def __int__( self: ndarray[Any, dtype[SupportsInt]], # type: ignore[type-var] @@ -2847,6 +2889,59 @@ class generic(_ArrayOrScalarCommon): def byteswap(self: _ScalarType, inplace: L[False] = ...) -> _ScalarType: ... @property def flat(self: _ScalarType) -> flatiter[ndarray[Any, dtype[_ScalarType]]]: ... + + @overload + def astype( + self, + dtype: _DTypeLike[_ScalarType], + order: _OrderKACF = ..., + casting: _Casting = ..., + subok: bool = ..., + copy: bool = ..., + ) -> _ScalarType: ... + @overload + def astype( + self, + dtype: DTypeLike, + order: _OrderKACF = ..., + casting: _Casting = ..., + subok: bool = ..., + copy: bool = ..., + ) -> Any: ... + + # NOTE: `view` will perform a 0D->scalar cast, + # thus the array `type` is irrelevant to the output type + @overload + def view( + self: _ScalarType, + type: Type[ndarray[Any, Any]] = ..., + ) -> _ScalarType: ... + @overload + def view( + self, + dtype: _DTypeLike[_ScalarType], + type: Type[ndarray[Any, Any]] = ..., + ) -> _ScalarType: ... + @overload + def view( + self, + dtype: DTypeLike, + type: Type[ndarray[Any, Any]] = ..., + ) -> Any: ... + + @overload + def getfield( + self, + dtype: _DTypeLike[_ScalarType], + offset: SupportsIndex = ... + ) -> _ScalarType: ... + @overload + def getfield( + self, + dtype: DTypeLike, + offset: SupportsIndex = ... + ) -> Any: ... + def item( self, __args: Union[L[0], Tuple[()], Tuple[L[0]]] = ..., diff --git a/numpy/core/src/multiarray/dtypemeta.c b/numpy/core/src/multiarray/dtypemeta.c index 40ca9ee2a..4ee721964 100644 --- a/numpy/core/src/multiarray/dtypemeta.c +++ b/numpy/core/src/multiarray/dtypemeta.c @@ -415,19 +415,6 @@ string_unicode_common_dtype(PyArray_DTypeMeta *cls, PyArray_DTypeMeta *other) Py_INCREF(Py_NotImplemented); return (PyArray_DTypeMeta *)Py_NotImplemented; } - if (other->type_num != NPY_STRING && other->type_num != NPY_UNICODE) { - /* Deprecated 2020-12-19, NumPy 1.21. */ - if (DEPRECATE_FUTUREWARNING( - "Promotion of numbers and bools to strings is deprecated. " - "In the future, code such as `np.concatenate((['string'], [0]))` " - "will raise an error, while `np.asarray(['string', 0])` will " - "return an array with `dtype=object`. To avoid the warning " - "while retaining a string result use `dtype='U'` (or 'S'). " - "To get an array of Python objects use `dtype=object`. " - "(Warning added in NumPy 1.21)") < 0) { - return NULL; - } - } /* * The builtin types are ordered by complexity (aside from object) here. * Arguably, we should not consider numbers and strings "common", but diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index ffe0147b2..42e632e4a 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -1105,41 +1105,6 @@ class TestNoseDecoratorsDeprecated(_DeprecationTestCase): self.assert_deprecated(_test_parametrize) -class TestStringPromotion(_DeprecationTestCase): - # Deprecated 2020-12-19, NumPy 1.21 - warning_cls = FutureWarning - message = "Promotion of numbers and bools to strings is deprecated." - - @pytest.mark.parametrize("dtype", "?bhilqpBHILQPefdgFDG") - @pytest.mark.parametrize("string_dt", ["S", "U"]) - def test_deprecated(self, dtype, string_dt): - self.assert_deprecated(lambda: np.promote_types(dtype, string_dt)) - - # concatenate has to be able to promote to find the result dtype: - arr1 = np.ones(3, dtype=dtype) - arr2 = np.ones(3, dtype=string_dt) - self.assert_deprecated(lambda: np.concatenate((arr1, arr2), axis=0)) - self.assert_deprecated(lambda: np.concatenate((arr1, arr2), axis=None)) - - self.assert_deprecated(lambda: np.array([arr1[0], arr2[0]])) - - @pytest.mark.parametrize("dtype", "?bhilqpBHILQPefdgFDG") - @pytest.mark.parametrize("string_dt", ["S", "U"]) - def test_not_deprecated(self, dtype, string_dt): - # The ufunc type resolvers run into this, but giving a futurewarning - # here is unnecessary (it ends up as an error anyway), so test that - # no warning is given: - arr1 = np.ones(3, dtype=dtype) - arr2 = np.ones(3, dtype=string_dt) - - # Adding two arrays uses result_type normally, which would fail: - with pytest.raises(TypeError): - self.assert_not_deprecated(lambda: arr1 + arr2) - # np.equal uses a different type resolver: - with pytest.raises(TypeError): - self.assert_not_deprecated(lambda: np.equal(arr1, arr2)) - - class TestSingleElementSignature(_DeprecationTestCase): # Deprecated 2021-04-01, NumPy 1.21 message = r"The use of a length 1" diff --git a/numpy/core/tests/test_half.py b/numpy/core/tests/test_half.py index 449a01d21..1b6fd21e1 100644 --- a/numpy/core/tests/test_half.py +++ b/numpy/core/tests/test_half.py @@ -71,10 +71,8 @@ class TestHalf: def test_half_conversion_to_string(self, string_dt): # Currently uses S/U32 (which is sufficient for float32) expected_dt = np.dtype(f"{string_dt}32") - with pytest.warns(FutureWarning): - assert np.promote_types(np.float16, string_dt) == expected_dt - with pytest.warns(FutureWarning): - assert np.promote_types(string_dt, np.float16) == expected_dt + assert np.promote_types(np.float16, string_dt) == expected_dt + assert np.promote_types(string_dt, np.float16) == expected_dt arr = np.ones(3, dtype=np.float16).astype(string_dt) assert arr.dtype == expected_dt diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index aba90ece5..f5113150e 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -848,12 +848,10 @@ class TestTypes: assert_equal(np.promote_types('<i8', '<i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>i8'), np.dtype('i8')) - with pytest.warns(FutureWarning, - match="Promotion of numbers and bools to strings"): - assert_equal(np.promote_types('>i8', '>U16'), np.dtype('U21')) - assert_equal(np.promote_types('<i8', '<U16'), np.dtype('U21')) - assert_equal(np.promote_types('>U16', '>i8'), np.dtype('U21')) - assert_equal(np.promote_types('<U16', '<i8'), np.dtype('U21')) + assert_equal(np.promote_types('>i8', '>U16'), np.dtype('U21')) + assert_equal(np.promote_types('<i8', '<U16'), np.dtype('U21')) + assert_equal(np.promote_types('>U16', '>i8'), np.dtype('U21')) + assert_equal(np.promote_types('<U16', '<i8'), np.dtype('U21')) assert_equal(np.promote_types('<S5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>S5', '>U8'), np.dtype('U8')) @@ -901,37 +899,32 @@ class TestTypes: S = string_dtype - with pytest.warns(FutureWarning, - match="Promotion of numbers and bools to strings") as record: - # Promote numeric with unsized string: - assert_equal(promote_types('bool', S), np.dtype(S+'5')) - assert_equal(promote_types('b', S), np.dtype(S+'4')) - assert_equal(promote_types('u1', S), np.dtype(S+'3')) - assert_equal(promote_types('u2', S), np.dtype(S+'5')) - assert_equal(promote_types('u4', S), np.dtype(S+'10')) - assert_equal(promote_types('u8', S), np.dtype(S+'20')) - assert_equal(promote_types('i1', S), np.dtype(S+'4')) - assert_equal(promote_types('i2', S), np.dtype(S+'6')) - assert_equal(promote_types('i4', S), np.dtype(S+'11')) - assert_equal(promote_types('i8', S), np.dtype(S+'21')) - # Promote numeric with sized string: - assert_equal(promote_types('bool', S+'1'), np.dtype(S+'5')) - assert_equal(promote_types('bool', S+'30'), np.dtype(S+'30')) - assert_equal(promote_types('b', S+'1'), np.dtype(S+'4')) - assert_equal(promote_types('b', S+'30'), np.dtype(S+'30')) - assert_equal(promote_types('u1', S+'1'), np.dtype(S+'3')) - assert_equal(promote_types('u1', S+'30'), np.dtype(S+'30')) - assert_equal(promote_types('u2', S+'1'), np.dtype(S+'5')) - assert_equal(promote_types('u2', S+'30'), np.dtype(S+'30')) - assert_equal(promote_types('u4', S+'1'), np.dtype(S+'10')) - assert_equal(promote_types('u4', S+'30'), np.dtype(S+'30')) - assert_equal(promote_types('u8', S+'1'), np.dtype(S+'20')) - assert_equal(promote_types('u8', S+'30'), np.dtype(S+'30')) - # Promote with object: - assert_equal(promote_types('O', S+'30'), np.dtype('O')) - - assert len(record) == 22 # each string promotion gave one warning - + # Promote numeric with unsized string: + assert_equal(promote_types('bool', S), np.dtype(S+'5')) + assert_equal(promote_types('b', S), np.dtype(S+'4')) + assert_equal(promote_types('u1', S), np.dtype(S+'3')) + assert_equal(promote_types('u2', S), np.dtype(S+'5')) + assert_equal(promote_types('u4', S), np.dtype(S+'10')) + assert_equal(promote_types('u8', S), np.dtype(S+'20')) + assert_equal(promote_types('i1', S), np.dtype(S+'4')) + assert_equal(promote_types('i2', S), np.dtype(S+'6')) + assert_equal(promote_types('i4', S), np.dtype(S+'11')) + assert_equal(promote_types('i8', S), np.dtype(S+'21')) + # Promote numeric with sized string: + assert_equal(promote_types('bool', S+'1'), np.dtype(S+'5')) + assert_equal(promote_types('bool', S+'30'), np.dtype(S+'30')) + assert_equal(promote_types('b', S+'1'), np.dtype(S+'4')) + assert_equal(promote_types('b', S+'30'), np.dtype(S+'30')) + assert_equal(promote_types('u1', S+'1'), np.dtype(S+'3')) + assert_equal(promote_types('u1', S+'30'), np.dtype(S+'30')) + assert_equal(promote_types('u2', S+'1'), np.dtype(S+'5')) + assert_equal(promote_types('u2', S+'30'), np.dtype(S+'30')) + assert_equal(promote_types('u4', S+'1'), np.dtype(S+'10')) + assert_equal(promote_types('u4', S+'30'), np.dtype(S+'30')) + assert_equal(promote_types('u8', S+'1'), np.dtype(S+'20')) + assert_equal(promote_types('u8', S+'30'), np.dtype(S+'30')) + # Promote with object: + assert_equal(promote_types('O', S+'30'), np.dtype('O')) @pytest.mark.parametrize(["dtype1", "dtype2"], [[np.dtype("V6"), np.dtype("V10")], diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index dbfb75c9a..312d0683d 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -782,9 +782,7 @@ class TestRegression: # Ticket #514 s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" t = [] - with pytest.warns(FutureWarning, - match="Promotion of numbers and bools to strings"): - np.hstack((t, s)) + np.hstack((t, s)) def test_arr_transpose(self): # Ticket #516 diff --git a/numpy/distutils/intelccompiler.py b/numpy/distutils/intelccompiler.py index 0388ad577..0fa1c11dd 100644 --- a/numpy/distutils/intelccompiler.py +++ b/numpy/distutils/intelccompiler.py @@ -58,7 +58,7 @@ class IntelEM64TCCompiler(UnixCCompiler): v = self.get_version() mpopt = 'openmp' if v and v < '15' else 'qopenmp' - self.cc_exe = ('icc -m64 -fPIC -fp-model strict -O3 ' + self.cc_exe = ('icc -std=c99 -m64 -fPIC -fp-model strict -O3 ' '-fomit-frame-pointer -{}').format(mpopt) compiler = self.cc_exe diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index 94fac7ef0..373226277 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -64,8 +64,7 @@ class TestRegression: def test_mem_string_concat(self): # Ticket #469 x = np.array([]) - with pytest.warns(FutureWarning): - np.append(x, 'asdasd\tasdasd') + np.append(x, 'asdasd\tasdasd') def test_poly_div(self): # Ticket #553 diff --git a/numpy/typing/__init__.py b/numpy/typing/__init__.py index 252123a19..19424169a 100644 --- a/numpy/typing/__init__.py +++ b/numpy/typing/__init__.py @@ -164,7 +164,7 @@ API # NOTE: The API section will be appended with additional entries # further down in this file -from typing import TYPE_CHECKING, List +from typing import TYPE_CHECKING, List, Any if TYPE_CHECKING: # typing_extensions is always available when type-checking @@ -376,14 +376,14 @@ if TYPE_CHECKING: _GUFunc_Nin2_Nout1, ) else: - _UFunc_Nin1_Nout1 = NotImplemented - _UFunc_Nin2_Nout1 = NotImplemented - _UFunc_Nin1_Nout2 = NotImplemented - _UFunc_Nin2_Nout2 = NotImplemented - _GUFunc_Nin2_Nout1 = NotImplemented + _UFunc_Nin1_Nout1 = Any + _UFunc_Nin2_Nout1 = Any + _UFunc_Nin1_Nout2 = Any + _UFunc_Nin2_Nout2 = Any + _GUFunc_Nin2_Nout1 = Any # Clean up the namespace -del TYPE_CHECKING, final, List +del TYPE_CHECKING, final, List, Any if __doc__ is not None: from ._add_docstring import _docstrings diff --git a/numpy/typing/_array_like.py b/numpy/typing/_array_like.py index 2b823ecc0..3bdbed8f8 100644 --- a/numpy/typing/_array_like.py +++ b/numpy/typing/_array_like.py @@ -1,7 +1,7 @@ from __future__ import annotations import sys -from typing import Any, Sequence, TYPE_CHECKING, Union, TypeVar +from typing import Any, Sequence, TYPE_CHECKING, Union, TypeVar, Generic from numpy import ( ndarray, @@ -34,7 +34,7 @@ _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: +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. @@ -43,7 +43,7 @@ if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS: class _SupportsArray(Protocol[_DType_co]): def __array__(self) -> ndarray[Any, _DType_co]: ... else: - _SupportsArray = Any + class _SupportsArray(Generic[_DType_co]): ... # TODO: Wait for support for recursive types _NestedSequence = Union[ diff --git a/numpy/typing/_callable.py b/numpy/typing/_callable.py index 54f9b1425..8f911da3b 100644 --- a/numpy/typing/_callable.py +++ b/numpy/typing/_callable.py @@ -53,7 +53,7 @@ if sys.version_info >= (3, 8): elif _HAS_TYPING_EXTENSIONS: from typing_extensions import Protocol -if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS: +if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS or sys.version_info >= (3, 8): _T1 = TypeVar("_T1") _T2 = TypeVar("_T2") _2Tuple = Tuple[_T1, _T1] @@ -332,25 +332,25 @@ if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS: def __call__(self, __other: _T2) -> NDArray[bool_]: ... else: - _BoolOp = NotImplemented - _BoolBitOp = NotImplemented - _BoolSub = NotImplemented - _BoolTrueDiv = NotImplemented - _BoolMod = NotImplemented - _BoolDivMod = NotImplemented - _TD64Div = NotImplemented - _IntTrueDiv = NotImplemented - _UnsignedIntOp = NotImplemented - _UnsignedIntBitOp = NotImplemented - _UnsignedIntMod = NotImplemented - _UnsignedIntDivMod = NotImplemented - _SignedIntOp = NotImplemented - _SignedIntBitOp = NotImplemented - _SignedIntMod = NotImplemented - _SignedIntDivMod = NotImplemented - _FloatOp = NotImplemented - _FloatMod = NotImplemented - _FloatDivMod = NotImplemented - _ComplexOp = NotImplemented - _NumberOp = NotImplemented - _ComparisonOp = NotImplemented + _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 diff --git a/numpy/typing/_char_codes.py b/numpy/typing/_char_codes.py index 6b33f995d..22ee168e9 100644 --- a/numpy/typing/_char_codes.py +++ b/numpy/typing/_char_codes.py @@ -8,7 +8,7 @@ if sys.version_info >= (3, 8): elif _HAS_TYPING_EXTENSIONS: from typing_extensions import Literal -if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS: +if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS or sys.version_info >= (3, 8): _BoolCodes = Literal["?", "=?", "<?", ">?", "bool", "bool_", "bool8"] _UInt8Codes = Literal["uint8", "u1", "=u1", "<u1", ">u1"] @@ -120,52 +120,52 @@ if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS: ] else: - _BoolCodes = NotImplemented - - _UInt8Codes = NotImplemented - _UInt16Codes = NotImplemented - _UInt32Codes = NotImplemented - _UInt64Codes = NotImplemented - - _Int8Codes = NotImplemented - _Int16Codes = NotImplemented - _Int32Codes = NotImplemented - _Int64Codes = NotImplemented - - _Float16Codes = NotImplemented - _Float32Codes = NotImplemented - _Float64Codes = NotImplemented - - _Complex64Codes = NotImplemented - _Complex128Codes = NotImplemented - - _ByteCodes = NotImplemented - _ShortCodes = NotImplemented - _IntCCodes = NotImplemented - _IntPCodes = NotImplemented - _IntCodes = NotImplemented - _LongLongCodes = NotImplemented - - _UByteCodes = NotImplemented - _UShortCodes = NotImplemented - _UIntCCodes = NotImplemented - _UIntPCodes = NotImplemented - _UIntCodes = NotImplemented - _ULongLongCodes = NotImplemented - - _HalfCodes = NotImplemented - _SingleCodes = NotImplemented - _DoubleCodes = NotImplemented - _LongDoubleCodes = NotImplemented - - _CSingleCodes = NotImplemented - _CDoubleCodes = NotImplemented - _CLongDoubleCodes = NotImplemented - - _StrCodes = NotImplemented - _BytesCodes = NotImplemented - _VoidCodes = NotImplemented - _ObjectCodes = NotImplemented - - _DT64Codes = NotImplemented - _TD64Codes = NotImplemented + _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 diff --git a/numpy/typing/_dtype_like.py b/numpy/typing/_dtype_like.py index 405cc4a3c..b2ce3adb4 100644 --- a/numpy/typing/_dtype_like.py +++ b/numpy/typing/_dtype_like.py @@ -11,9 +11,6 @@ if sys.version_info >= (3, 8): from typing import Protocol, TypedDict elif _HAS_TYPING_EXTENSIONS: from typing_extensions import Protocol, TypedDict - -if sys.version_info >= (3, 9): - from types import GenericAlias else: from ._generic_alias import _GenericAlias as GenericAlias @@ -62,7 +59,7 @@ 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: +if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS or sys.version_info >= (3, 8): # Mandatory keys class _DTypeDictBase(TypedDict): names: Sequence[str] @@ -81,7 +78,7 @@ if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS: def dtype(self) -> _DType_co: ... else: - _DTypeDict = NotImplemented + _DTypeDict = Any class _SupportsDType: ... _SupportsDType = GenericAlias(_SupportsDType, _DType_co) diff --git a/numpy/typing/_extended_precision.py b/numpy/typing/_extended_precision.py index 3f1ce2038..0900bc659 100644 --- a/numpy/typing/_extended_precision.py +++ b/numpy/typing/_extended_precision.py @@ -28,15 +28,15 @@ if TYPE_CHECKING: complex256 = np.complexfloating[_128Bit, _128Bit] complex512 = np.complexfloating[_256Bit, _256Bit] else: - uint128 = NotImplemented - uint256 = NotImplemented - int128 = NotImplemented - int256 = NotImplemented - float80 = NotImplemented - float96 = NotImplemented - float128 = NotImplemented - float256 = NotImplemented - complex160 = NotImplemented - complex192 = NotImplemented - complex256 = NotImplemented - complex512 = NotImplemented + uint128 = Any + uint256 = Any + int128 = Any + int256 = Any + float80 = Any + float96 = Any + float128 = Any + float256 = Any + complex160 = Any + complex192 = Any + complex256 = Any + complex512 = Any diff --git a/numpy/typing/_shape.py b/numpy/typing/_shape.py index 0742be8a9..75698f3d3 100644 --- a/numpy/typing/_shape.py +++ b/numpy/typing/_shape.py @@ -1,5 +1,5 @@ import sys -from typing import Sequence, Tuple, Union +from typing import Sequence, Tuple, Union, Any from . import _HAS_TYPING_EXTENSIONS @@ -8,7 +8,7 @@ if sys.version_info >= (3, 8): elif _HAS_TYPING_EXTENSIONS: from typing_extensions import SupportsIndex else: - SupportsIndex = NotImplemented + SupportsIndex = Any _Shape = Tuple[int, ...] diff --git a/numpy/typing/tests/data/reveal/ndarray_conversion.py b/numpy/typing/tests/data/reveal/ndarray_conversion.py index 4ee637b75..03f2faf43 100644 --- a/numpy/typing/tests/data/reveal/ndarray_conversion.py +++ b/numpy/typing/tests/data/reveal/ndarray_conversion.py @@ -1,12 +1,13 @@ import numpy as np +import numpy.typing as npt -nd = np.array([[1, 2], [3, 4]]) +nd: npt.NDArray[np.int_] = np.array([[1, 2], [3, 4]]) # item -reveal_type(nd.item()) # E: Any -reveal_type(nd.item(1)) # E: Any -reveal_type(nd.item(0, 1)) # E: Any -reveal_type(nd.item((0, 1))) # E: Any +reveal_type(nd.item()) # E: int +reveal_type(nd.item(1)) # E: int +reveal_type(nd.item(0, 1)) # E: int +reveal_type(nd.item((0, 1))) # E: int # tolist reveal_type(nd.tolist()) # E: Any @@ -19,36 +20,32 @@ reveal_type(nd.tolist()) # E: Any # dumps is pretty simple # astype -reveal_type(nd.astype("float")) # E: numpy.ndarray -reveal_type(nd.astype(float)) # E: numpy.ndarray -reveal_type(nd.astype(float, "K")) # E: numpy.ndarray -reveal_type(nd.astype(float, "K", "unsafe")) # E: numpy.ndarray -reveal_type(nd.astype(float, "K", "unsafe", True)) # E: numpy.ndarray -reveal_type(nd.astype(float, "K", "unsafe", True, True)) # E: numpy.ndarray +reveal_type(nd.astype("float")) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(nd.astype(float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(nd.astype(np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(nd.astype(np.float64, "K")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(nd.astype(np.float64, "K", "unsafe")) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(nd.astype(np.float64, "K", "unsafe", True)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(nd.astype(np.float64, "K", "unsafe", True, True)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] # byteswap -reveal_type(nd.byteswap()) # E: numpy.ndarray -reveal_type(nd.byteswap(True)) # E: numpy.ndarray +reveal_type(nd.byteswap()) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(nd.byteswap(True)) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] # copy -reveal_type(nd.copy()) # E: numpy.ndarray -reveal_type(nd.copy("C")) # E: numpy.ndarray +reveal_type(nd.copy()) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(nd.copy("C")) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] -# view -class SubArray(np.ndarray): - pass - - -reveal_type(nd.view()) # E: numpy.ndarray -reveal_type(nd.view(np.int64)) # E: numpy.ndarray -# replace `Any` with `numpy.matrix` when `matrix` will be added to stubs -reveal_type(nd.view(np.int64, np.matrix)) # E: Any -reveal_type(nd.view(np.int64, SubArray)) # E: SubArray +reveal_type(nd.view()) # E: numpy.ndarray[Any, numpy.dtype[{int_}]] +reveal_type(nd.view(np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(nd.view(float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(nd.view(np.float64, np.matrix)) # E: numpy.matrix[Any, Any] # getfield -reveal_type(nd.getfield("float")) # E: numpy.ndarray -reveal_type(nd.getfield(float)) # E: numpy.ndarray -reveal_type(nd.getfield(float, 8)) # E: numpy.ndarray +reveal_type(nd.getfield("float")) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(nd.getfield(float)) # E: numpy.ndarray[Any, numpy.dtype[Any]] +reveal_type(nd.getfield(np.float64)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] +reveal_type(nd.getfield(np.float64, 8)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] # setflags does not return a value # fill does not return a value diff --git a/numpy/typing/tests/data/reveal/scalars.py b/numpy/typing/tests/data/reveal/scalars.py index d98388422..c081a5c67 100644 --- a/numpy/typing/tests/data/reveal/scalars.py +++ b/numpy/typing/tests/data/reveal/scalars.py @@ -114,3 +114,15 @@ reveal_type(f8.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]] reveal_type(c16.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]] reveal_type(U.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]] reveal_type(S.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]] + +reveal_type(i8.astype(float)) # E: Any +reveal_type(i8.astype(np.float64)) # E: {float64} + +reveal_type(i8.view()) # E: {int64} +reveal_type(i8.view(np.float64)) # E: {float64} +reveal_type(i8.view(float)) # E: Any +reveal_type(i8.view(np.float64, np.ndarray)) # E: {float64} + +reveal_type(i8.getfield(float)) # E: Any +reveal_type(i8.getfield(np.float64)) # E: {float64} +reveal_type(i8.getfield(np.float64, 8)) # E: {float64} diff --git a/numpy/typing/tests/test_generic_alias.py b/numpy/typing/tests/test_generic_alias.py index 27afe3927..538d7eae5 100644 --- a/numpy/typing/tests/test_generic_alias.py +++ b/numpy/typing/tests/test_generic_alias.py @@ -21,8 +21,8 @@ if sys.version_info >= (3, 9): NDArray_ref = types.GenericAlias(np.ndarray, (Any, DType_ref)) FuncType = Callable[[Union[_GenericAlias, types.GenericAlias]], Any] else: - DType_ref = NotImplemented - NDArray_ref = NotImplemented + DType_ref = Any + NDArray_ref = Any FuncType = Callable[[_GenericAlias], Any] GETATTR_NAMES = sorted(set(dir(np.ndarray)) - _GenericAlias._ATTR_EXCEPTIONS) |