diff options
Diffstat (limited to 'numpy/typing/tests')
-rw-r--r-- | numpy/typing/tests/data/fail/einsumfunc.pyi | 3 | ||||
-rw-r--r-- | numpy/typing/tests/data/fail/numerictypes.pyi | 2 | ||||
-rw-r--r-- | numpy/typing/tests/data/pass/dtype.py | 2 | ||||
-rw-r--r-- | numpy/typing/tests/data/pass/ndarray_misc.py | 2 | ||||
-rw-r--r-- | numpy/typing/tests/data/pass/numerictypes.py | 9 | ||||
-rw-r--r-- | numpy/typing/tests/data/pass/simple.py | 4 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/einsumfunc.pyi | 5 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/modules.pyi | 2 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/npyio.pyi | 10 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/numerictypes.pyi | 2 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/scalars.pyi | 6 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/testing.pyi | 2 | ||||
-rw-r--r-- | numpy/typing/tests/test_generic_alias.py | 188 | ||||
-rw-r--r-- | numpy/typing/tests/test_runtime.py | 6 | ||||
-rw-r--r-- | numpy/typing/tests/test_typing.py | 2 |
15 files changed, 21 insertions, 224 deletions
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/fail/numerictypes.pyi b/numpy/typing/tests/data/fail/numerictypes.pyi index a5c2814ef..ce5662d5e 100644 --- a/numpy/typing/tests/data/fail/numerictypes.pyi +++ b/numpy/typing/tests/data/fail/numerictypes.pyi @@ -9,5 +9,3 @@ np.maximum_sctype(1) # E: No overload variant np.issubsctype(1, np.int64) # E: incompatible type np.issubdtype(1, np.int64) # E: incompatible type - -np.find_common_type(np.int64, np.int64) # E: incompatible type diff --git a/numpy/typing/tests/data/pass/dtype.py b/numpy/typing/tests/data/pass/dtype.py index e849cfdd4..6ec44e6b0 100644 --- a/numpy/typing/tests/data/pass/dtype.py +++ b/numpy/typing/tests/data/pass/dtype.py @@ -15,7 +15,7 @@ np.dtype({"names": ["a", "b"], "formats": [int, float]}) np.dtype({"names": ["a"], "formats": [int], "titles": [object]}) np.dtype({"names": ["a"], "formats": [int], "titles": [object()]}) -np.dtype([("name", np.unicode_, 16), ("grades", np.float64, (2,)), ("age", "int32")]) +np.dtype([("name", np.str_, 16), ("grades", np.float64, (2,)), ("age", "int32")]) np.dtype( { diff --git a/numpy/typing/tests/data/pass/ndarray_misc.py b/numpy/typing/tests/data/pass/ndarray_misc.py index 19a1af9e2..6beacc5d7 100644 --- a/numpy/typing/tests/data/pass/ndarray_misc.py +++ b/numpy/typing/tests/data/pass/ndarray_misc.py @@ -150,7 +150,7 @@ A.argpartition([0]) A.diagonal() A.dot(1) -A.dot(1, out=B0) +A.dot(1, out=B2) A.nonzero() diff --git a/numpy/typing/tests/data/pass/numerictypes.py b/numpy/typing/tests/data/pass/numerictypes.py index 7f1dd0945..63b6ad0e2 100644 --- a/numpy/typing/tests/data/pass/numerictypes.py +++ b/numpy/typing/tests/data/pass/numerictypes.py @@ -8,7 +8,7 @@ np.issctype("S8") np.obj2sctype(list) np.obj2sctype(list, default=None) -np.obj2sctype(list, default=np.string_) +np.obj2sctype(list, default=np.bytes_) np.issubclass_(np.int32, int) np.issubclass_(np.float64, float) @@ -17,17 +17,12 @@ np.issubclass_(np.float64, (int, float)) np.issubsctype("int64", int) np.issubsctype(np.array([1]), np.array([1])) -np.issubdtype("S1", np.string_) +np.issubdtype("S1", np.bytes_) np.issubdtype(np.float64, np.float32) np.sctype2char("S1") np.sctype2char(list) -np.find_common_type([], [np.int64, np.float32, complex]) -np.find_common_type((), (np.int64, np.float32, complex)) -np.find_common_type([np.int64, np.float32], []) -np.find_common_type([np.float32], [np.int64, np.float64]) - np.cast[int] np.cast["i8"] np.cast[np.int64] diff --git a/numpy/typing/tests/data/pass/simple.py b/numpy/typing/tests/data/pass/simple.py index 03ca3e83f..801168702 100644 --- a/numpy/typing/tests/data/pass/simple.py +++ b/numpy/typing/tests/data/pass/simple.py @@ -33,13 +33,13 @@ np.dtype(two_tuples_dtype) three_tuples_dtype = [("R", "u1", 2)] np.dtype(three_tuples_dtype) -mixed_tuples_dtype = [("R", "u1"), ("G", np.unicode_, 1)] +mixed_tuples_dtype = [("R", "u1"), ("G", np.str_, 1)] np.dtype(mixed_tuples_dtype) shape_tuple_dtype = [("R", "u1", (2, 2))] np.dtype(shape_tuple_dtype) -shape_like_dtype = [("R", "u1", (2, 2)), ("G", np.unicode_, 1)] +shape_like_dtype = [("R", "u1", (2, 2)), ("G", np.str_, 1)] np.dtype(shape_like_dtype) object_dtype = [("field1", object)] 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 diff --git a/numpy/typing/tests/data/reveal/npyio.pyi b/numpy/typing/tests/data/reveal/npyio.pyi index 68605cf94..2c62d8d21 100644 --- a/numpy/typing/tests/data/reveal/npyio.pyi +++ b/numpy/typing/tests/data/reveal/npyio.pyi @@ -75,13 +75,13 @@ reveal_type(np.fromregex(str_path, re.compile("test"), dtype=np.str_, encoding=" reveal_type(np.fromregex(pathlib_path, "test", np.float64)) # E: ndarray[Any, dtype[{float64}]] reveal_type(np.fromregex(bytes_reader, "test", np.float64)) # E: ndarray[Any, dtype[{float64}]] -reveal_type(np.genfromtxt(bytes_file)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.genfromtxt(bytes_file)) # E: ndarray[Any, dtype[Any]] reveal_type(np.genfromtxt(pathlib_path, dtype=np.str_)) # E: ndarray[Any, dtype[str_]] reveal_type(np.genfromtxt(str_path, dtype=str, skip_header=2)) # E: ndarray[Any, dtype[Any]] -reveal_type(np.genfromtxt(str_file, comments="test")) # E: ndarray[Any, dtype[{float64}]] -reveal_type(np.genfromtxt(str_path, delimiter="\n")) # E: ndarray[Any, dtype[{float64}]] -reveal_type(np.genfromtxt(str_path, ndmin=2)) # E: ndarray[Any, dtype[{float64}]] -reveal_type(np.genfromtxt(["1", "2", "3"], ndmin=2)) # E: ndarray[Any, dtype[{float64}]] +reveal_type(np.genfromtxt(str_file, comments="test")) # E: ndarray[Any, dtype[Any]] +reveal_type(np.genfromtxt(str_path, delimiter="\n")) # E: ndarray[Any, dtype[Any]] +reveal_type(np.genfromtxt(str_path, ndmin=2)) # E: ndarray[Any, dtype[Any]] +reveal_type(np.genfromtxt(["1", "2", "3"], ndmin=2)) # E: ndarray[Any, dtype[Any]] reveal_type(np.recfromtxt(bytes_file)) # E: recarray[Any, dtype[record]] reveal_type(np.recfromtxt(pathlib_path, usemask=True)) # E: ma.mrecords.MaskedRecords[Any, dtype[void]] diff --git a/numpy/typing/tests/data/reveal/numerictypes.pyi b/numpy/typing/tests/data/reveal/numerictypes.pyi index e1857557d..d4399e2b1 100644 --- a/numpy/typing/tests/data/reveal/numerictypes.pyi +++ b/numpy/typing/tests/data/reveal/numerictypes.pyi @@ -21,8 +21,6 @@ reveal_type(np.issubclass_(1, 1)) # E: Literal[False] reveal_type(np.sctype2char("S8")) # E: str reveal_type(np.sctype2char(list)) # E: str -reveal_type(np.find_common_type([np.int64], [np.int64])) # E: dtype[Any] - reveal_type(np.cast[int]) # E: _CastFunc reveal_type(np.cast["i8"]) # E: _CastFunc reveal_type(np.cast[np.int64]) # E: _CastFunc diff --git a/numpy/typing/tests/data/reveal/scalars.pyi b/numpy/typing/tests/data/reveal/scalars.pyi index b7fc75acc..965aa5ace 100644 --- a/numpy/typing/tests/data/reveal/scalars.pyi +++ b/numpy/typing/tests/data/reveal/scalars.pyi @@ -1,4 +1,3 @@ -import sys import numpy as np b: np.bool_ @@ -151,8 +150,7 @@ reveal_type(round(u8, 3)) # E: {uint64} reveal_type(round(f8)) # E: int 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(f8.__ceil__()) # E: int +reveal_type(f8.__floor__()) # E: int reveal_type(i8.is_integer()) # E: Literal[True] diff --git a/numpy/typing/tests/data/reveal/testing.pyi b/numpy/typing/tests/data/reveal/testing.pyi index 5440af800..5c35731d3 100644 --- a/numpy/typing/tests/data/reveal/testing.pyi +++ b/numpy/typing/tests/data/reveal/testing.pyi @@ -113,7 +113,6 @@ reveal_type(np.testing.rundocs()) # E: None reveal_type(np.testing.rundocs("test.py")) # E: None reveal_type(np.testing.rundocs(Path("test.py"), raise_on_error=True)) # E: None -@np.testing.raises(RuntimeError, RuntimeWarning) def func3(a: int) -> bool: ... reveal_type(func3) # E: def (a: builtins.int) -> builtins.bool @@ -175,4 +174,3 @@ reveal_type(np.testing.assert_no_gc_cycles(func3, 5)) # E: None reveal_type(np.testing.break_cycles()) # E: None reveal_type(np.testing.TestCase()) # E: unittest.case.TestCase -reveal_type(np.testing.run_module_suite(file_to_run="numpy/tests/test_matlib.py")) # E: None diff --git a/numpy/typing/tests/test_generic_alias.py b/numpy/typing/tests/test_generic_alias.py deleted file mode 100644 index 861babd4b..000000000 --- a/numpy/typing/tests/test_generic_alias.py +++ /dev/null @@ -1,188 +0,0 @@ -from __future__ import annotations - -import sys -import copy -import types -import pickle -import weakref -from typing import TypeVar, Any, Union, Callable - -import pytest -import numpy as np -from numpy._typing._generic_alias import _GenericAlias -from typing_extensions import Unpack - -ScalarType = TypeVar("ScalarType", bound=np.generic, covariant=True) -T1 = TypeVar("T1") -T2 = TypeVar("T2") -DType = _GenericAlias(np.dtype, (ScalarType,)) -NDArray = _GenericAlias(np.ndarray, (Any, DType)) - -# NOTE: The `npt._GenericAlias` *class* isn't quite stable on python >=3.11. -# This is not a problem during runtime (as it's 3.8-exclusive), but we still -# need it for the >=3.9 in order to verify its semantics match -# `types.GenericAlias` replacement. xref numpy/numpy#21526 -if sys.version_info >= (3, 9): - DType_ref = types.GenericAlias(np.dtype, (ScalarType,)) - NDArray_ref = types.GenericAlias(np.ndarray, (Any, DType_ref)) - FuncType = Callable[["_GenericAlias | types.GenericAlias"], Any] -else: - DType_ref = Any - NDArray_ref = Any - FuncType = Callable[["_GenericAlias"], Any] - -GETATTR_NAMES = sorted(set(dir(np.ndarray)) - _GenericAlias._ATTR_EXCEPTIONS) - -BUFFER = np.array([1], dtype=np.int64) -BUFFER.setflags(write=False) - -def _get_subclass_mro(base: type) -> tuple[type, ...]: - class Subclass(base): # type: ignore[misc,valid-type] - pass - return Subclass.__mro__[1:] - - -class TestGenericAlias: - """Tests for `numpy._typing._generic_alias._GenericAlias`.""" - - @pytest.mark.parametrize("name,func", [ - ("__init__", lambda n: n), - ("__init__", lambda n: _GenericAlias(np.ndarray, Any)), - ("__init__", lambda n: _GenericAlias(np.ndarray, (Any,))), - ("__init__", lambda n: _GenericAlias(np.ndarray, (Any, Any))), - ("__init__", lambda n: _GenericAlias(np.ndarray, T1)), - ("__init__", lambda n: _GenericAlias(np.ndarray, (T1,))), - ("__init__", lambda n: _GenericAlias(np.ndarray, (T1, T2))), - ("__origin__", lambda n: n.__origin__), - ("__args__", lambda n: n.__args__), - ("__parameters__", lambda n: n.__parameters__), - ("__mro_entries__", lambda n: n.__mro_entries__([object])), - ("__hash__", lambda n: hash(n)), - ("__repr__", lambda n: repr(n)), - ("__getitem__", lambda n: n[np.float64]), - ("__getitem__", lambda n: n[ScalarType][np.float64]), - ("__getitem__", lambda n: n[Union[np.int64, ScalarType]][np.float64]), - ("__getitem__", lambda n: n[Union[T1, T2]][np.float32, np.float64]), - ("__eq__", lambda n: n == n), - ("__ne__", lambda n: n != np.ndarray), - ("__call__", lambda n: n((1,), np.int64, BUFFER)), - ("__call__", lambda n: n(shape=(1,), dtype=np.int64, buffer=BUFFER)), - ("subclassing", lambda n: _get_subclass_mro(n)), - ("pickle", lambda n: n == pickle.loads(pickle.dumps(n))), - ]) - def test_pass(self, name: str, func: FuncType) -> None: - """Compare `types.GenericAlias` with its numpy-based backport. - - Checker whether ``func`` runs as intended and that both `GenericAlias` - and `_GenericAlias` return the same result. - - """ - value = func(NDArray) - - if sys.version_info >= (3, 9): - value_ref = func(NDArray_ref) - assert value == value_ref - - @pytest.mark.parametrize("name,func", [ - ("__copy__", lambda n: n == copy.copy(n)), - ("__deepcopy__", lambda n: n == copy.deepcopy(n)), - ]) - def test_copy(self, name: str, func: FuncType) -> None: - value = func(NDArray) - - # xref bpo-45167 - GE_398 = ( - sys.version_info[:2] == (3, 9) and sys.version_info >= (3, 9, 8) - ) - if GE_398 or sys.version_info >= (3, 10, 1): - value_ref = func(NDArray_ref) - assert value == value_ref - - def test_dir(self) -> None: - value = dir(NDArray) - if sys.version_info < (3, 9): - return - - # A number attributes only exist in `types.GenericAlias` in >= 3.11 - if sys.version_info < (3, 11, 0, "beta", 3): - value.remove("__typing_unpacked_tuple_args__") - if sys.version_info < (3, 11, 0, "beta", 1): - value.remove("__unpacked__") - assert value == dir(NDArray_ref) - - @pytest.mark.parametrize("name,func,dev_version", [ - ("__iter__", lambda n: len(list(n)), ("beta", 1)), - ("__iter__", lambda n: next(iter(n)), ("beta", 1)), - ("__unpacked__", lambda n: n.__unpacked__, ("beta", 1)), - ("Unpack", lambda n: Unpack[n], ("beta", 1)), - - # The right operand should now have `__unpacked__ = True`, - # and they are thus now longer equivalent - ("__ne__", lambda n: n != next(iter(n)), ("beta", 1)), - - # >= beta3 - ("__typing_unpacked_tuple_args__", - lambda n: n.__typing_unpacked_tuple_args__, ("beta", 3)), - - # >= beta4 - ("__class__", lambda n: n.__class__ == type(n), ("beta", 4)), - ]) - def test_py311_features( - self, - name: str, - func: FuncType, - dev_version: tuple[str, int], - ) -> None: - """Test Python 3.11 features.""" - value = func(NDArray) - - if sys.version_info >= (3, 11, 0, *dev_version): - value_ref = func(NDArray_ref) - assert value == value_ref - - def test_weakref(self) -> None: - """Test ``__weakref__``.""" - value = weakref.ref(NDArray)() - - if sys.version_info >= (3, 9, 1): # xref bpo-42332 - value_ref = weakref.ref(NDArray_ref)() - assert value == value_ref - - @pytest.mark.parametrize("name", GETATTR_NAMES) - def test_getattr(self, name: str) -> None: - """Test that `getattr` wraps around the underlying type, - aka ``__origin__``. - - """ - value = getattr(NDArray, name) - value_ref1 = getattr(np.ndarray, name) - - if sys.version_info >= (3, 9): - value_ref2 = getattr(NDArray_ref, name) - assert value == value_ref1 == value_ref2 - else: - assert value == value_ref1 - - @pytest.mark.parametrize("name,exc_type,func", [ - ("__getitem__", TypeError, lambda n: n[()]), - ("__getitem__", TypeError, lambda n: n[Any, Any]), - ("__getitem__", TypeError, lambda n: n[Any][Any]), - ("isinstance", TypeError, lambda n: isinstance(np.array(1), n)), - ("issublass", TypeError, lambda n: issubclass(np.ndarray, n)), - ("setattr", AttributeError, lambda n: setattr(n, "__origin__", int)), - ("setattr", AttributeError, lambda n: setattr(n, "test", int)), - ("getattr", AttributeError, lambda n: getattr(n, "test")), - ]) - def test_raise( - self, - name: str, - exc_type: type[BaseException], - func: FuncType, - ) -> None: - """Test operations that are supposed to raise.""" - with pytest.raises(exc_type): - func(NDArray) - - if sys.version_info >= (3, 9): - with pytest.raises(exc_type): - func(NDArray_ref) diff --git a/numpy/typing/tests/test_runtime.py b/numpy/typing/tests/test_runtime.py index 44d069006..c32c5db32 100644 --- a/numpy/typing/tests/test_runtime.py +++ b/numpy/typing/tests/test_runtime.py @@ -2,7 +2,6 @@ from __future__ import annotations -import sys from typing import ( get_type_hints, Union, @@ -24,10 +23,7 @@ class TypeTup(NamedTuple): origin: None | type -if sys.version_info >= (3, 9): - NDArrayTup = TypeTup(npt.NDArray, npt.NDArray.__args__, np.ndarray) -else: - NDArrayTup = TypeTup(npt.NDArray, (), None) +NDArrayTup = TypeTup(npt.NDArray, npt.NDArray.__args__, np.ndarray) TYPES = { "ArrayLike": TypeTup(npt.ArrayLike, npt.ArrayLike.__args__, Union), diff --git a/numpy/typing/tests/test_typing.py b/numpy/typing/tests/test_typing.py index 5011339b5..bcaaf5250 100644 --- a/numpy/typing/tests/test_typing.py +++ b/numpy/typing/tests/test_typing.py @@ -431,7 +431,7 @@ def test_extended_precision() -> None: output_mypy = OUTPUT_MYPY assert path in output_mypy - with open(path, "r") as f: + with open(path) as f: expression_list = f.readlines() for _msg in output_mypy[path]: |