summaryrefslogtreecommitdiff
path: root/numpy/tests
diff options
context:
space:
mode:
authorBas van Beek <43369155+BvB93@users.noreply.github.com>2020-09-02 19:44:26 +0200
committerGitHub <noreply@github.com>2020-09-02 20:44:26 +0300
commit5b37a4b5b640d0c1f9714f1200309a25a54b7999 (patch)
tree7a5f21b4676f20f9ee219539d0d7b699378af390 /numpy/tests
parent6eaf8906c4dccc60592dedd0ec9a7aae0e7db851 (diff)
downloadnumpy-5b37a4b5b640d0c1f9714f1200309a25a54b7999.tar.gz
ENH: Added missing methods to `np.flatiter` (#17180)
* MAINT: Added missing methods to `np.flatiter` * DOC: Added a comment about weird `flatiter.__getitem__` behavior Contrary to `ndarray.__getitem__` its counterpart in `flatiter` does not accept objects with the `__array__` or `__index__` protocols; boolean indexing is just plain broken (gh-17175)
Diffstat (limited to 'numpy/tests')
-rw-r--r--numpy/tests/typing/fail/flatiter.py25
-rw-r--r--numpy/tests/typing/pass/flatiter.py14
-rw-r--r--numpy/tests/typing/reveal/flatiter.py14
3 files changed, 53 insertions, 0 deletions
diff --git a/numpy/tests/typing/fail/flatiter.py b/numpy/tests/typing/fail/flatiter.py
new file mode 100644
index 000000000..e8a82344f
--- /dev/null
+++ b/numpy/tests/typing/fail/flatiter.py
@@ -0,0 +1,25 @@
+from typing import Any
+
+import numpy as np
+from numpy.typing import DtypeLike, _SupportsArray
+
+
+class Index:
+ def __index__(self) -> int:
+ ...
+
+
+a: "np.flatiter[np.ndarray]"
+supports_array: _SupportsArray
+
+a.base = Any # E: Property "base" defined in "flatiter" is read-only
+a.coords = Any # E: Property "coords" defined in "flatiter" is read-only
+a.index = Any # E: Property "index" defined in "flatiter" is read-only
+a.copy(order='C') # E: Unexpected keyword argument
+
+# NOTE: Contrary to `ndarray.__getitem__` its counterpart in `flatiter`
+# does not accept objects with the `__array__` or `__index__` protocols;
+# boolean indexing is just plain broken (gh-17175)
+a[np.bool_()] # E: No overload variant of "__getitem__"
+a[Index()] # E: No overload variant of "__getitem__"
+a[supports_array] # E: No overload variant of "__getitem__"
diff --git a/numpy/tests/typing/pass/flatiter.py b/numpy/tests/typing/pass/flatiter.py
new file mode 100644
index 000000000..93c15f601
--- /dev/null
+++ b/numpy/tests/typing/pass/flatiter.py
@@ -0,0 +1,14 @@
+import numpy as np
+
+a = np.random.rand(5).flat
+
+a.base
+a.copy()
+a.coords
+a.index
+iter(a)
+next(a)
+a[0]
+a[[0, 1, 2]]
+a[...]
+a[:]
diff --git a/numpy/tests/typing/reveal/flatiter.py b/numpy/tests/typing/reveal/flatiter.py
new file mode 100644
index 000000000..56cdc7a0e
--- /dev/null
+++ b/numpy/tests/typing/reveal/flatiter.py
@@ -0,0 +1,14 @@
+import numpy as np
+
+a: "np.flatiter[np.ndarray]"
+
+reveal_type(a.base) # E: numpy.ndarray*
+reveal_type(a.copy()) # E: numpy.ndarray*
+reveal_type(a.coords) # E: tuple[builtins.int]
+reveal_type(a.index) # E: int
+reveal_type(iter(a)) # E: Iterator[numpy.generic*]
+reveal_type(next(a)) # E: numpy.generic
+reveal_type(a[0]) # E: numpy.generic
+reveal_type(a[[0, 1, 2]]) # E: numpy.ndarray*
+reveal_type(a[...]) # E: numpy.ndarray*
+reveal_type(a[:]) # E: numpy.ndarray*