Skip to content

Commit 5c57fe0

Browse files
JonathanPlasseZac-HD
authored andcommitted
Separate Numpy revealed types tests
1 parent 806d752 commit 5c57fe0

File tree

3 files changed

+170
-240
lines changed

3 files changed

+170
-240
lines changed

whole-repo-tests/revealed_types.py

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
NUMPY_REVEALED_TYPES = [
2+
(
3+
'arrays(dtype=np.dtype("int32"), shape=1)',
4+
"ndarray[Any, dtype[signedinteger[_32Bit]]]",
5+
),
6+
(
7+
"arrays(dtype=np.dtype(int), shape=1)",
8+
"ndarray[Any, dtype[signedinteger[Any]]]",
9+
),
10+
(
11+
"boolean_dtypes()",
12+
"dtype[bool_]",
13+
),
14+
(
15+
"unsigned_integer_dtypes(sizes=8)",
16+
"dtype[unsignedinteger[_8Bit]]",
17+
),
18+
(
19+
"unsigned_integer_dtypes(sizes=16)",
20+
"dtype[unsignedinteger[_16Bit]]",
21+
),
22+
(
23+
"unsigned_integer_dtypes(sizes=32)",
24+
"dtype[unsignedinteger[_32Bit]]",
25+
),
26+
(
27+
"unsigned_integer_dtypes(sizes=64)",
28+
"dtype[unsignedinteger[_64Bit]]",
29+
),
30+
(
31+
"unsigned_integer_dtypes()",
32+
"dtype[unsignedinteger[Any]]",
33+
),
34+
(
35+
"unsigned_integer_dtypes(sizes=(8, 16))",
36+
"dtype[unsignedinteger[Any]]",
37+
),
38+
(
39+
"integer_dtypes(sizes=8)",
40+
"dtype[signedinteger[_8Bit]]",
41+
),
42+
(
43+
"integer_dtypes(sizes=16)",
44+
"dtype[signedinteger[_16Bit]]",
45+
),
46+
(
47+
"integer_dtypes(sizes=32)",
48+
"dtype[signedinteger[_32Bit]]",
49+
),
50+
(
51+
"integer_dtypes(sizes=64)",
52+
"dtype[signedinteger[_64Bit]]",
53+
),
54+
(
55+
"integer_dtypes()",
56+
"dtype[signedinteger[Any]]",
57+
),
58+
(
59+
"integer_dtypes(sizes=(8, 16))",
60+
"dtype[signedinteger[Any]]",
61+
),
62+
(
63+
"floating_dtypes(sizes=16)",
64+
"dtype[floating[_16Bit]]",
65+
),
66+
(
67+
"floating_dtypes(sizes=32)",
68+
"dtype[floating[_32Bit]]",
69+
),
70+
(
71+
"floating_dtypes(sizes=64)",
72+
"dtype[floating[_64Bit]]",
73+
),
74+
(
75+
"floating_dtypes(sizes=128)",
76+
"dtype[floating[_128Bit]]",
77+
),
78+
(
79+
"floating_dtypes()",
80+
"dtype[floating[Any]]",
81+
),
82+
(
83+
"floating_dtypes(sizes=(16, 32))",
84+
"dtype[floating[Any]]",
85+
),
86+
(
87+
"complex_number_dtypes(sizes=64)",
88+
"dtype[complexfloating[_32Bit, _32Bit]]",
89+
),
90+
(
91+
"complex_number_dtypes(sizes=128)",
92+
"dtype[complexfloating[_64Bit, _64Bit]]",
93+
),
94+
(
95+
"complex_number_dtypes(sizes=256)",
96+
"dtype[complexfloating[_128Bit, _128Bit]]",
97+
),
98+
(
99+
"complex_number_dtypes()",
100+
"dtype[complexfloating[Any, Any]]",
101+
),
102+
(
103+
"complex_number_dtypes(sizes=(64, 128))",
104+
"dtype[complexfloating[Any, Any]]",
105+
),
106+
(
107+
"integer_array_indices(shape=(2, 3))",
108+
"Tuple[ndarray[Any, dtype[signedinteger[Any]]], ...]",
109+
),
110+
(
111+
'integer_array_indices(shape=(2, 3), dtype=np.dtype("int32"))',
112+
"Tuple[ndarray[Any, dtype[signedinteger[_32Bit]]], ...]",
113+
),
114+
(
115+
'integer_array_indices(shape=(2, 3), dtype=np.dtype("uint8"))',
116+
"Tuple[ndarray[Any, dtype[unsignedinteger[_8Bit]]], ...]",
117+
),
118+
]

whole-repo-tests/test_mypy.py

+26-120
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from hypothesistooling.projects.hypothesispython import PYTHON_SRC
1717
from hypothesistooling.scripts import pip_tool, tool_path
1818

19+
from .revealed_types import NUMPY_REVEALED_TYPES
20+
1921
PYTHON_VERSIONS = ["3.8", "3.9", "3.10", "3.11"]
2022

2123

@@ -61,6 +63,7 @@ def get_mypy_analysed_type(fname):
6163
)
6264
.replace("numpy._typing.", "")
6365
.replace("numpy.", "")
66+
.replace("tuple", "Tuple")
6467
)
6568

6669

@@ -127,133 +130,36 @@ def convert_lines():
127130
"tuples(text(), text(), text(), text(), text(), text())",
128131
"tuple[Any, ...]",
129132
),
130-
(
131-
'arrays(dtype=np.dtype("int32"), shape=1)',
132-
"ndarray[Any, dtype[signedinteger[_32Bit]]]",
133-
),
134-
(
135-
"arrays(dtype=np.dtype(int), shape=1)",
136-
"ndarray[Any, dtype[signedinteger[Any]]]",
137-
),
138-
(
139-
"boolean_dtypes()",
140-
"dtype[bool_]",
141-
),
142-
(
143-
"unsigned_integer_dtypes(sizes=8)",
144-
"dtype[unsignedinteger[_8Bit]]",
145-
),
146-
(
147-
"unsigned_integer_dtypes(sizes=16)",
148-
"dtype[unsignedinteger[_16Bit]]",
149-
),
150-
(
151-
"unsigned_integer_dtypes(sizes=32)",
152-
"dtype[unsignedinteger[_32Bit]]",
153-
),
154-
(
155-
"unsigned_integer_dtypes(sizes=64)",
156-
"dtype[unsignedinteger[_64Bit]]",
157-
),
158-
(
159-
"unsigned_integer_dtypes()",
160-
"dtype[unsignedinteger[Any]]",
161-
),
162-
(
163-
"unsigned_integer_dtypes(sizes=(8, 16))",
164-
"dtype[unsignedinteger[Any]]",
165-
),
166-
(
167-
"integer_dtypes(sizes=8)",
168-
"dtype[signedinteger[_8Bit]]",
169-
),
170-
(
171-
"integer_dtypes(sizes=16)",
172-
"dtype[signedinteger[_16Bit]]",
173-
),
174-
(
175-
"integer_dtypes(sizes=32)",
176-
"dtype[signedinteger[_32Bit]]",
177-
),
178-
(
179-
"integer_dtypes(sizes=64)",
180-
"dtype[signedinteger[_64Bit]]",
181-
),
182-
(
183-
"integer_dtypes()",
184-
"dtype[signedinteger[Any]]",
185-
),
186-
(
187-
"integer_dtypes(sizes=(8, 16))",
188-
"dtype[signedinteger[Any]]",
189-
),
190-
(
191-
"floating_dtypes(sizes=16)",
192-
"dtype[floating[_16Bit]]",
193-
),
194-
(
195-
"floating_dtypes(sizes=32)",
196-
"dtype[floating[_32Bit]]",
197-
),
198-
(
199-
"floating_dtypes(sizes=64)",
200-
"dtype[floating[_64Bit]]",
201-
),
202-
(
203-
"floating_dtypes(sizes=128)",
204-
"dtype[floating[_128Bit]]",
205-
),
206-
(
207-
"floating_dtypes()",
208-
"dtype[floating[Any]]",
209-
),
210-
(
211-
"floating_dtypes(sizes=(16, 32))",
212-
"dtype[floating[Any]]",
213-
),
214-
(
215-
"complex_number_dtypes(sizes=64)",
216-
"dtype[complexfloating[_32Bit, _32Bit]]",
217-
),
218-
(
219-
"complex_number_dtypes(sizes=128)",
220-
"dtype[complexfloating[_64Bit, _64Bit]]",
221-
),
222-
(
223-
"complex_number_dtypes(sizes=256)",
224-
"dtype[complexfloating[_128Bit, _128Bit]]",
225-
),
226-
(
227-
"complex_number_dtypes()",
228-
"dtype[complexfloating[Any, Any]]",
229-
),
230-
(
231-
"complex_number_dtypes(sizes=(64, 128))",
232-
"dtype[complexfloating[Any, Any]]",
233-
),
234-
(
235-
"integer_array_indices(shape=(2, 3))",
236-
"tuple[ndarray[Any, dtype[signedinteger[Any]]], ...]",
237-
),
238-
(
239-
'integer_array_indices(shape=(2, 3), dtype=np.dtype("int32"))',
240-
"tuple[ndarray[Any, dtype[signedinteger[_32Bit]]], ...]",
241-
),
242-
(
243-
'integer_array_indices(shape=(2, 3), dtype=np.dtype("uint8"))',
244-
"tuple[ndarray[Any, dtype[unsignedinteger[_8Bit]]], ...]",
245-
),
246133
# Note: keep this in sync with the equivalent test for Pyright
247134
],
248135
)
249136
def test_revealed_types(tmp_path, val, expect):
250137
"""Check that Mypy picks up the expected `X` in SearchStrategy[`X`]."""
251138
f = tmp_path / "check.py"
252139
f.write_text(
253-
"import numpy as np\n"
254-
"from hypothesis.extra.numpy import *\n"
255-
"from hypothesis.strategies import *\n"
256-
f"reveal_type({val})\n",
140+
textwrap.dedent(
141+
f"""
142+
from hypothesis.strategies import *
143+
reveal_type({val})
144+
"""
145+
),
146+
encoding="utf-8",
147+
)
148+
typ = get_mypy_analysed_type(f)
149+
assert typ == f"SearchStrategy[{expect}]"
150+
151+
152+
@pytest.mark.parametrize("val,expect", NUMPY_REVEALED_TYPES)
153+
def test_numpy_revealed_types(tmp_path, val, expect):
154+
f = tmp_path / "check.py"
155+
f.write_text(
156+
textwrap.dedent(
157+
f"""
158+
import numpy as np
159+
"from hypothesis.extra.numpy import *
160+
reveal_type({val})
161+
"""
162+
),
257163
encoding="utf-8",
258164
)
259165
typ = get_mypy_analysed_type(f)

0 commit comments

Comments
 (0)