-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathcheck-ignore.test
More file actions
302 lines (246 loc) · 6.41 KB
/
check-ignore.test
File metadata and controls
302 lines (246 loc) · 6.41 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
[case testIgnoreTypeError]
x = 1
x() # type: ignore
x() # E: "int" not callable
[case testIgnoreUndefinedName]
x = 1
y # type: ignore
z # E: Name "z" is not defined
[case testIgnoreImportError]
import xyz_m # type: ignore
xyz_m.foo
1() # E: "int" not callable
[case testIgnoreImportFromError]
from xyz_m import a, b # type: ignore
a.foo
b()
1() # E: "int" not callable
[case testIgnoreImportFromErrorMultiline]
from xyz_m import ( # type: ignore
a, b
)
a.foo
b()
1() # E: "int" not callable
[case testIgnoreImportAllError]
from xyz_m import * # type: ignore
x # E: Name "x" is not defined
1() # E: "int" not callable
[case testIgnoreImportBadModule_no_native_parse]
import m # type: ignore
from m import a # type: ignore
[file m.py]
+
[out]
tmp/m.py:1: error: Invalid syntax
[case testIgnoreAppliesOnlyToMissing]
import a # type: ignore
import b # type: ignore
reveal_type(a.foo) # N: Revealed type is "Any"
reveal_type(b.foo) # N: Revealed type is "builtins.int"
a.bar()
b.bar() # E: Module has no attribute "bar"
[file b.py]
foo = 3
[builtins fixtures/module_all.pyi]
[out]
[case testIgnoreImportStarFromBadModule_no_native_parse]
from m import * # type: ignore
[file m.py]
+
[out]
tmp/m.py:1: error: Invalid syntax
[case testIgnoreAssignmentTypeError]
x = 1
if int():
x = '' # type: ignore
if int():
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[case testIgnoreInvalidOverride]
class A:
def f(self) -> int: pass
class B(A):
def f(self) -> str: pass # type: ignore
[case testIgnoreMissingModuleAttribute]
import m
m.x = object # type: ignore
m.f() # type: ignore
m.y # E: Module has no attribute "y"
[file m.py]
[builtins fixtures/module.pyi]
[case testIgnoreTypeInferenceError]
x = [] # type: ignore
y = x
x.append(1)
[builtins fixtures/list.pyi]
[case testIgnoreTypeInferenceError2]
def f() -> None: pass
x = f() # type: ignore
y = x
x = 1
[builtins fixtures/list.pyi]
[case testIgnoreTypeInferenceErrorAndMultipleAssignment]
x, y = [], [] # type: ignore
z = x
z = y
[builtins fixtures/list.pyi]
[case testIgnoreSomeStarImportErrors]
from m1 import *
from m2 import * # type: ignore
# We should still import things that don't conflict.
y() # E: "str" not callable
z() # E: "int" not callable
x() # E: "int" not callable
[file m1.py]
x = 1
y = ''
[file m2.py]
x = ''
z = 1
[case testIgnoredModuleDefinesBaseClass1]
from m import B # type: ignore
class C(B):
def f(self) -> None:
self.f(1) # E: Too many arguments for "f" of "C"
self.g(1)
[out]
[case testIgnoredModuleDefinesBaseClass2]
import m # type: ignore
class C(m.B):
def f(self) -> None: ...
c = C()
c.f(1) # E: Too many arguments for "f" of "C"
c.g(1)
c.x = 1
[out]
[case testIgnoredModuleDefinesBaseClassAndClassAttribute]
import m # type: ignore
class C(m.B):
@staticmethod
def f() -> None: pass
C.f(1) # E: Too many arguments for "f" of "C"
C.g(1)
C.x = 1
[builtins fixtures/staticmethod.pyi]
[out]
[case testIgnoredModuleDefinesBaseClassWithInheritance1]
from m import B # type: ignore
class C: pass
class D(C, B):
def f(self) -> None:
self.f(1) # E: Too many arguments for "f" of "D"
self.g(1)
[out]
[case testIgnoredModuleDefinesBaseClassWithInheritance2]
from m import B # type: ignore
class C(B): pass
class D(C):
def f(self) -> None:
self.f(1) # E: Too many arguments for "f" of "D"
self.g(1)
[out]
[case testIgnoreWithFollowingIndentedComment]
if 1: # type: ignore
# blah
pass
[out]
[case testIgnoreTooManyTypeArguments]
from typing import TypeVar, Generic
T = TypeVar('T')
U = TypeVar('U')
class Base(Generic[T, U]):
pass
class PartialBase(Base[T, int], Generic[T]):
pass
class Child(PartialBase[str, int]): # type: ignore
pass
def foo(x: Base[str, int]) -> None: pass
foo(Child())
def bar(x: Base[str, str]) -> None: pass
bar(Child())
[out]
main:19: error: Argument 1 to "bar" has incompatible type "Child"; expected "Base[str, str]"
[case testTypeIgnoreLineNumberWithinFile]
import m
pass # type: ignore
m.f(kw=1)
[file m.py]
pass
def f() -> None: pass
[out]
main:3: error: Unexpected keyword argument "kw" for "f"
main:3: note: "f" defined in "m"
[case testIgnoreUnexpectedKeywordArgument]
import m
m.f(kw=1) # type: ignore
[file m.py]
def f() -> None: pass
[out]
[case testCannotIgnoreBlockingError]
yield # type: ignore # E: "yield" outside function
[case testIgnoreWholeModule1]
# type: ignore
if True:
IGNORE
[case testIgnoreWholeModule2]
# type: ignore
@d
class C: ...
IGNORE
[case testIgnoreWholeModule3]
# type: ignore
@d
def f(): ...
IGNORE
[case testIgnoreWholeModule4]
# type: ignore
import MISSING
[case testDontIgnoreWholeModule1]
if True:
# type: ignore
ERROR # E: Name "ERROR" is not defined
ERROR # E: Name "ERROR" is not defined
[case testDontIgnoreWholeModule2]
@d # type: ignore
class C: ...
ERROR # E: Name "ERROR" is not defined
[case testDontIgnoreWholeModule3]
@d # type: ignore
def f(): ...
ERROR # E: Name "ERROR" is not defined
[case testIgnoreInsideFunctionDoesntAffectWhole]
# flags: --disallow-untyped-defs
def f(): # E: Function is missing a return type annotation
42 + 'no way' # type: ignore
return 0
[case testIgnoreInsideClassDoesntAffectWhole]
import six
class M(type): pass
@six.add_metaclass(M)
class CD(six.with_metaclass(M)): # E: Multiple metaclass definitions
42 + 'no way' # type: ignore
[builtins fixtures/tuple.pyi]
[case testUnusedIgnoreCodeOrder]
# flags: --warn-unused-ignores
5 # type: ignore[import, steven] # E: Unused "type: ignore[import, steven]" comment
-- User ordering of codes is preserved
5 # type: ignore[steven, import] # E: Unused "type: ignore[steven, import]" comment
-- Spacing is not preserved
5 # type: ignore[ steven, import ] # E: Unused "type: ignore[steven, import]" comment
-- Make sure it works as intended in more complex situations
1 + "ok" + "ok".foo # type: ignore[ operator,steven,attr-defined, import] # E: Unused "type: ignore[steven, import]" comment
[case testUnusedIgnoreTryExcept]
# flags: --warn-unused-ignores
try:
import foo # type: ignore # E: Unused "type: ignore" comment
import bar # type: ignore[import] # E: Unused "type: ignore" comment
import foobar # type: ignore[unused-ignore]
import barfoo # type: ignore[import,unused-ignore]
import missing # type: ignore[import,unused-ignore]
except Exception:
pass
[file foo.py]
[file bar.py]
[file foobar.py]
[file barfoo.py]
[builtins fixtures/exception.pyi]