-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathrun-imports.test
More file actions
269 lines (213 loc) · 5.33 KB
/
run-imports.test
File metadata and controls
269 lines (213 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# Test cases for imports and related features (compile and run)
[case testImports]
import testmodule
import pkg2.mod
import pkg2.mod2 as mm2
def f(x: int) -> int:
return testmodule.factorial(5)
def g(x: int) -> int:
from welp import foo
return foo(x)
def test_import_basics() -> None:
assert f(5) == 120
assert g(5) == 5
assert "pkg2.mod" not in globals(), "the root module should be in globals!"
assert pkg2.mod.x == 1
assert "mod2" not in globals(), "pkg2.mod2 is aliased to mm2!"
assert mm2.y == 2
def test_import_submodule_within_function() -> None:
import pkg.mod
assert pkg.x == 1
assert pkg.mod.y == 2
assert "pkg.mod" not in globals(), "the root module should be in globals!"
def test_import_as_submodule_within_function() -> None:
import pkg.mod as mm
assert mm.y == 2
assert "pkg.mod" not in globals(), "the root module should be in globals!"
# TODO: Don't add local imports to globals()
#
# def test_local_import_not_in_globals() -> None:
# import nob
# assert 'nob' not in globals()
def test_import_module_without_stub_in_function() -> None:
# 'psutil' must not have a stub in typeshed for this test case
import psutil # type: ignore
# TODO: We shouldn't add local imports to globals()
# assert 'psutil' not in globals()
assert isinstance(psutil.__name__, str)
def test_import_as_module_without_stub_in_function() -> None:
# 'psutil' must not have a stub in typeshed for this test case
import psutil as pp # type: ignore
assert 'psutil' not in globals()
# TODO: We shouldn't add local imports to globals()
# assert 'pp' not in globals()
assert isinstance(pp.__name__, str)
[file testmodule.py]
def factorial(x: int) -> int:
if x == 0:
return 1
else:
return x * factorial(x-1)
[file welp.py]
def foo(x: int) -> int:
return x
[file pkg/__init__.py]
x = 1
[file pkg/mod.py]
y = 2
[file pkg2/__init__.py]
[file pkg2/mod.py]
x = 1
[file pkg2/mod2.py]
y = 2
[file nob.py]
z = 3
[case testImportMissing]
# The unchecked module is configured by the test harness to not be
# picked up by mypy, so we can test that we do that right thing when
# calling library modules without stubs.
import unchecked # type: ignore
import unchecked as lol # type: ignore
assert unchecked.x == 10
assert lol.x == 10
[file unchecked.py]
x = 10
[file driver.py]
import native
[case testFromImport]
from testmodule import g
def f(x: int) -> int:
return g(x)
[file testmodule.py]
def g(x: int) -> int:
return x + 1
[file driver.py]
from native import f
assert f(1) == 2
[case testFromImportWithUntypedModule]
# avoid including an __init__.py and use type: ignore to test what happens
# if mypy can't tell if mod isn't a module
from pkg import mod # type: ignore
def test_import() -> None:
assert mod.h(8) == 24
[file pkg/mod.py]
def h(x):
return x * 3
[case testFromImportWithKnownModule]
from pkg import mod1
from pkg import mod2 as modmod
from pkg.mod2 import g as gg
from pkg.mod3 import h as h2, g as g2
def test_import() -> None:
assert mod1.h(8) == 24
assert modmod.g(1) == 1
assert gg(2) == 2
assert h2(10) == 12
assert g2(10) == 13
[file pkg/__init__.py]
[file pkg/mod1.py]
def h(x: int) -> int:
return x * 3
[file pkg/mod2.py]
def g(x: int) -> int:
return x
[file pkg/mod3.py]
def h(x: int) -> int:
return x + 2
def g(x: int) -> int:
return x + 3
[case testFromImportWithUnKnownModule]
def test_import() -> None:
try:
from pkg import a # type: ignore
except ImportError:
pass
[file pkg/__init__.py]
[case testMultipleFromImportsWithSamePackageButDifferentModules]
from pkg import a
from pkg import b
def test_import() -> None:
assert a.g() == 4
assert b.h() == 39
[file pkg/__init__.py]
[file pkg/a.py]
def g() -> int:
return 4
[file pkg/b.py]
def h() -> int:
return 39
[case testReexport]
# Test that we properly handle accessing values that have been reexported
import a
def f(x: int) -> int:
return a.g(x) + a.foo + a.b.foo
whatever = a.A()
[file a.py]
from b import g as g, A as A, foo as foo
import b
[file b.py]
def g(x: int) -> int:
return x + 1
class A:
pass
foo = 20
[file driver.py]
from native import f, whatever
import b
assert f(20) == 61
assert isinstance(whatever, b.A)
[case testAssignModule]
import a
assert a.x == 20
a.x = 10
[file a.py]
x = 20
[file driver.py]
import native
[case testLazyImport]
import shared
def do_import() -> None:
import a
def test_lazy() -> None:
assert shared.counter == 0
do_import()
assert shared.counter == 1
[file a.py]
import shared
shared.counter += 1
[file shared.py]
counter = 0
[case testDelayedImport]
def test_delayed() -> None:
import a
print("inbetween")
import b
[file a.py]
print("first")
[file b.py]
print("last")
[out]
first
inbetween
last
[case testImportErrorLineNumber]
def test_error() -> None:
try:
import enum
import dataclasses, missing # type: ignore[import]
except ImportError as e:
line = e.__traceback__.tb_lineno # type: ignore[attr-defined]
assert line == 4, f"traceback's line number is {line}, expected 4"
[case testImportGroupIsolation]
def func() -> None:
import second
def test_isolation() -> None:
import first
func()
[file first.py]
print("first")
[file second.py]
print("second")
[out]
first
second