Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.

Commit caef625

Browse files
authored
TST: also run the code in "pass" (#70)
* TST: also run the code in "pass" It seems fair that the code in pass should also run; i.e. that directory shouldn't contain mypy false positives. Add a test that runs the modules in that directory and fix the resulting runtime errors. * MAINT: bump minimum NumPy version
1 parent 44de2bb commit caef625

File tree

6 files changed

+61
-39
lines changed

6 files changed

+61
-39
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def find_stubs(package):
2222
packages=["numpy-stubs"],
2323
# PEP 561 requires these
2424
install_requires=[
25-
"numpy>=1.14.0",
25+
"numpy>=1.16.0",
2626
'typing_extensions>=3.7.4; python_version<"3.8"',
2727
],
2828
package_data=find_stubs("numpy-stubs"),

tests/pass/ndarray_conversion.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
import tempfile
2+
13
import numpy as np
24

35
nd = np.array([[1, 2], [3, 4]])
6+
scalar_array = np.array(1)
47

58
# item
6-
nd.item() # `nd` should be one-element in runtime
9+
scalar_array.item()
710
nd.item(1)
811
nd.item(0, 1)
912
nd.item((0, 1))
1013

1114
# tolist is pretty simple
1215

1316
# itemset
14-
nd.itemset(3) # `nd` should be one-element in runtime
17+
scalar_array.itemset(3)
1518
nd.itemset(3, 0)
1619
nd.itemset((0, 0), 3)
1720

@@ -25,14 +28,15 @@
2528
nd.tobytes(None)
2629

2730
# tofile
28-
nd.tofile("a.txt")
29-
nd.tofile(open("a.txt", mode="bw"))
31+
with tempfile.NamedTemporaryFile(suffix=".txt") as tmp:
32+
nd.tofile(tmp.name)
33+
nd.tofile(tmp.name, "")
34+
nd.tofile(tmp.name, sep="")
3035

31-
nd.tofile("a.txt", "")
32-
nd.tofile("a.txt", sep="")
36+
nd.tofile(tmp.name, "", "%s")
37+
nd.tofile(tmp.name, format="%s")
3338

34-
nd.tofile("a.txt", "", "%s")
35-
nd.tofile("a.txt", format="%s")
39+
nd.tofile(tmp)
3640

3741
# dump is pretty simple
3842
# dumps is pretty simple
@@ -69,11 +73,13 @@
6973
nd.view(type=np.matrix)
7074

7175
# getfield
72-
nd.getfield("float")
73-
nd.getfield(float)
76+
complex_array = np.array([[1 + 1j, 0], [0, 1 - 1j]], dtype=np.complex128)
77+
78+
complex_array.getfield("float")
79+
complex_array.getfield(float)
7480

75-
nd.getfield("float", 8)
76-
nd.getfield(float, offset=8)
81+
complex_array.getfield("float", 8)
82+
complex_array.getfield(float, offset=8)
7783

7884
# setflags
7985
nd.setflags()
+29-24
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,47 @@
11
import numpy as np
22

3-
nd = np.array([[1, 2], [3, 4]])
3+
nd1 = np.array([[1, 2], [3, 4]])
44

55
# reshape
6-
nd.reshape()
7-
nd.reshape(4)
8-
nd.reshape(2, 2)
9-
nd.reshape((2, 2))
6+
nd1.reshape(4)
7+
nd1.reshape(2, 2)
8+
nd1.reshape((2, 2))
109

11-
nd.reshape((2, 2), order="C")
12-
nd.reshape(4, order="C")
10+
nd1.reshape((2, 2), order="C")
11+
nd1.reshape(4, order="C")
1312

1413
# resize
15-
nd.resize()
16-
nd.resize(4)
17-
nd.resize(2, 2)
18-
nd.resize((2, 2))
14+
nd1.resize()
15+
nd1.resize(4)
16+
nd1.resize(2, 2)
17+
nd1.resize((2, 2))
1918

20-
nd.resize((2, 2), refcheck=True)
21-
nd.resize(4, refcheck=True)
19+
nd1.resize((2, 2), refcheck=True)
20+
nd1.resize(4, refcheck=True)
21+
22+
nd2 = np.array([[1, 2], [3, 4]])
2223

2324
# transpose
24-
nd.transpose()
25-
nd.transpose(1, 0)
26-
nd.transpose((1, 0))
25+
nd2.transpose()
26+
nd2.transpose(1, 0)
27+
nd2.transpose((1, 0))
2728

2829
# swapaxes
29-
nd.swapaxes(0, 1)
30+
nd2.swapaxes(0, 1)
3031

3132
# flatten
32-
nd.flatten()
33-
nd.flatten("C")
33+
nd2.flatten()
34+
nd2.flatten("C")
3435

3536
# ravel
36-
nd.ravel()
37-
nd.ravel("C")
37+
nd2.ravel()
38+
nd2.ravel("C")
3839

3940
# squeeze
40-
nd.squeeze()
41-
nd.squeeze(0)
42-
nd.squeeze((0, 2))
41+
nd2.squeeze()
42+
43+
nd3 = np.array([[1, 2]])
44+
nd3.squeeze(0)
45+
46+
nd4 = np.array([[[1, 2]]])
47+
nd4.squeeze((0, 1))

tests/pass/scalars.py

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ def __float__(self):
1717
return 4.0
1818

1919

20-
np.complex32(3)
2120
np.complex64(3j)
2221
np.complex64(C())
2322

tests/pass/simple.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ def iterable_func(x):
114114

115115
array / 1
116116
1 / array
117-
array /= 1
117+
float_array = np.array([1.0, 2.0])
118+
float_array /= 1
118119

119120
array // 1
120121
1 // array

tests/test_stubs.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import importlib.util
2+
import itertools
13
import os
24
import re
35
from collections import defaultdict
@@ -86,3 +88,12 @@ def test_reveal(path):
8688
assert "Revealed type is" in error_line
8789
marker = lines[lineno - 1].split("# E:")[-1].strip()
8890
assert marker in error_line
91+
92+
93+
@pytest.mark.parametrize("path", get_test_cases(PASS_DIR))
94+
def test_code_runs(path):
95+
path_without_extension, _ = os.path.splitext(path)
96+
dirname, filename = path.split(os.sep)[-2:]
97+
spec = importlib.util.spec_from_file_location(f"{dirname}.{filename}", path)
98+
test_module = importlib.util.module_from_spec(spec)
99+
spec.loader.exec_module(test_module)

0 commit comments

Comments
 (0)