summaryrefslogtreecommitdiff
path: root/numpy/tests/reveal/ndarray_conversion.py
diff options
context:
space:
mode:
authorJosh Wilson <person142@users.noreply.github.com>2020-06-06 15:31:33 -0700
committerJosh Wilson <person142@users.noreply.github.com>2020-06-06 15:31:33 -0700
commit11b95d15f10c2bc652ed19d5e27efa0384396cb8 (patch)
tree8d6f2020f6982fc9f2389796daca1429387f576e /numpy/tests/reveal/ndarray_conversion.py
parenta5d021a1b6f439a19812926bc4d796ef5f346c44 (diff)
downloadnumpy-11b95d15f10c2bc652ed19d5e27efa0384396cb8.tar.gz
ENH: add type stubs from numpy-stubs
Add the type stubs and tests from numpy-stubs. Things this entails: - Copy over the stubs (numpy/__init__.pyi and numpy/core/_internal.pyi) - The only modification made was removing `ndarray.tostring` since it is deprecated - Update some setup.py files to include pyi files - Move the tests from numpy-stubs/tests into numpy/tests - Skip them if mypy is not installed (planning on setting up CI in a future PR) - Add a mypy.ini; use it to configure mypy in the tests - It tells mypy where to find NumPy in the test env - It ignores internal NumPy type errors (since we only want to consider errors from the tests cases) - Some small edits were made to fix test cases that were emitting deprecation warnings - Add numpy/py.typed so that the types are picked up in an installed version of NumPy
Diffstat (limited to 'numpy/tests/reveal/ndarray_conversion.py')
-rw-r--r--numpy/tests/reveal/ndarray_conversion.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/numpy/tests/reveal/ndarray_conversion.py b/numpy/tests/reveal/ndarray_conversion.py
new file mode 100644
index 000000000..411adcf63
--- /dev/null
+++ b/numpy/tests/reveal/ndarray_conversion.py
@@ -0,0 +1,54 @@
+import numpy as np
+
+nd = 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
+
+# tolist
+reveal_type(nd.tolist()) # E: builtins.list[Any]
+
+# itemset does not return a value
+# tostring is pretty simple
+# tobytes is pretty simple
+# tofile does not return a value
+# dump does not return a value
+# 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
+
+# byteswap
+reveal_type(nd.byteswap()) # E: numpy.ndarray
+reveal_type(nd.byteswap(True)) # E: numpy.ndarray
+
+# copy
+reveal_type(nd.copy()) # E: numpy.ndarray
+reveal_type(nd.copy("C")) # E: numpy.ndarray
+
+# 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
+
+# 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
+
+# setflags does not return a value
+# fill does not return a value