Debian Codegraph @

test-data/unit/check-ignore.test

raw
1 [case testIgnoreTypeError]
2 x = 1
3 x() # type: ignore
4 x() # E: "int" not callable
5
6 [case testIgnoreUndefinedName]
7 x = 1
8 y # type: ignore
9 z # E: Name "z" is not defined
10
11 [case testIgnoreImportError]
12 import xyz_m # type: ignore
13 xyz_m.foo
14 1() # E: "int" not callable
15
16 [case testIgnoreImportFromError]
17 from xyz_m import a, b # type: ignore
18 a.foo
19 b()
20 1() # E: "int" not callable
21
22 [case testIgnoreImportFromErrorMultiline]
23 from xyz_m import ( # type: ignore
24 a, b
25 )
26 a.foo
27 b()
28 1() # E: "int" not callable
29
30 [case testIgnoreImportAllError]
31 from xyz_m import * # type: ignore
32 x # E: Name "x" is not defined
33 1() # E: "int" not callable
34
35 [case testIgnoreImportBadModule_no_native_parse]
36 import m # type: ignore
37 from m import a # type: ignore
38 [file m.py]
39 +
40 [out]
41 tmp/m.py:1: error: Invalid syntax
42
43 [case testIgnoreAppliesOnlyToMissing]
44 import a # type: ignore
45 import b # type: ignore
46 reveal_type(a.foo) # N: Revealed type is "Any"
47 reveal_type(b.foo) # N: Revealed type is "builtins.int"
48 a.bar()
49 b.bar() # E: Module has no attribute "bar"
50
51 [file b.py]
52 foo = 3
53
54 [builtins fixtures/module_all.pyi]
55 [out]
56
57 [case testIgnoreImportStarFromBadModule_no_native_parse]
58 from m import * # type: ignore
59 [file m.py]
60 +
61 [out]
62 tmp/m.py:1: error: Invalid syntax
63
64 [case testIgnoreAssignmentTypeError]
65 x = 1
66 if int():
67 x = '' # type: ignore
68 if int():
69 x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
70
71 [case testIgnoreInvalidOverride]
72 class A:
73 def f(self) -> int: pass
74 class B(A):
75 def f(self) -> str: pass # type: ignore
76
77 [case testIgnoreMissingModuleAttribute]
78 import m
79 m.x = object # type: ignore
80 m.f() # type: ignore
81 m.y # E: Module has no attribute "y"
82 [file m.py]
83 [builtins fixtures/module.pyi]
84
85 [case testIgnoreTypeInferenceError]
86 x = [] # type: ignore
87 y = x
88 x.append(1)
89 [builtins fixtures/list.pyi]
90
91 [case testIgnoreTypeInferenceError2]
92 def f() -> None: pass
93 x = f() # type: ignore
94 y = x
95 x = 1
96 [builtins fixtures/list.pyi]
97
98 [case testIgnoreTypeInferenceErrorAndMultipleAssignment]
99 x, y = [], [] # type: ignore
100 z = x
101 z = y
102 [builtins fixtures/list.pyi]
103
104 [case testIgnoreSomeStarImportErrors]
105 from m1 import *
106 from m2 import * # type: ignore
107 # We should still import things that don't conflict.
108 y() # E: "str" not callable
109 z() # E: "int" not callable
110 x() # E: "int" not callable
111 [file m1.py]
112 x = 1
113 y = ''
114 [file m2.py]
115 x = ''
116 z = 1
117
118 [case testIgnoredModuleDefinesBaseClass1]
119 from m import B # type: ignore
120
121 class C(B):
122 def f(self) -> None:
123 self.f(1) # E: Too many arguments for "f" of "C"
124 self.g(1)
125 [out]
126
127 [case testIgnoredModuleDefinesBaseClass2]
128 import m # type: ignore
129
130 class C(m.B):
131 def f(self) -> None: ...
132
133 c = C()
134 c.f(1) # E: Too many arguments for "f" of "C"
135 c.g(1)
136 c.x = 1
137 [out]
138
139 [case testIgnoredModuleDefinesBaseClassAndClassAttribute]
140 import m # type: ignore
141
142 class C(m.B):
143 @staticmethod
144 def f() -> None: pass
145
146 C.f(1) # E: Too many arguments for "f" of "C"
147 C.g(1)
148 C.x = 1
149 [builtins fixtures/staticmethod.pyi]
150 [out]
151
152 [case testIgnoredModuleDefinesBaseClassWithInheritance1]
153 from m import B # type: ignore
154
155 class C: pass
156 class D(C, B):
157 def f(self) -> None:
158 self.f(1) # E: Too many arguments for "f" of "D"
159 self.g(1)
160 [out]
161
162 [case testIgnoredModuleDefinesBaseClassWithInheritance2]
163 from m import B # type: ignore
164
165 class C(B): pass
166 class D(C):
167 def f(self) -> None:
168 self.f(1) # E: Too many arguments for "f" of "D"
169 self.g(1)
170 [out]
171
172 [case testIgnoreWithFollowingIndentedComment]
173 if 1: # type: ignore
174 # blah
175 pass
176 [out]
177
178 [case testIgnoreTooManyTypeArguments]
179 from typing import TypeVar, Generic
180 T = TypeVar('T')
181 U = TypeVar('U')
182
183 class Base(Generic[T, U]):
184 pass
185
186 class PartialBase(Base[T, int], Generic[T]):
187 pass
188
189 class Child(PartialBase[str, int]): # type: ignore
190 pass
191
192
193 def foo(x: Base[str, int]) -> None: pass
194 foo(Child())
195
196 def bar(x: Base[str, str]) -> None: pass
197 bar(Child())
198 [out]
199 main:19: error: Argument 1 to "bar" has incompatible type "Child"; expected "Base[str, str]"
200
201 [case testTypeIgnoreLineNumberWithinFile]
202 import m
203 pass # type: ignore
204 m.f(kw=1)
205 [file m.py]
206 pass
207 def f() -> None: pass
208 [out]
209 main:3: error: Unexpected keyword argument "kw" for "f"
210 main:3: note: "f" defined in "m"
211
212 [case testIgnoreUnexpectedKeywordArgument]
213 import m
214 m.f(kw=1) # type: ignore
215 [file m.py]
216 def f() -> None: pass
217 [out]
218
219 [case testCannotIgnoreBlockingError]
220 yield # type: ignore # E: "yield" outside function
221
222 [case testIgnoreWholeModule1]
223 # type: ignore
224 if True:
225 IGNORE
226
227 [case testIgnoreWholeModule2]
228 # type: ignore
229 @d
230 class C: ...
231 IGNORE
232
233 [case testIgnoreWholeModule3]
234 # type: ignore
235 @d
236
237 def f(): ...
238 IGNORE
239
240 [case testIgnoreWholeModule4]
241 # type: ignore
242 import MISSING
243
244 [case testDontIgnoreWholeModule1]
245 if True:
246 # type: ignore
247 ERROR # E: Name "ERROR" is not defined
248 ERROR # E: Name "ERROR" is not defined
249
250 [case testDontIgnoreWholeModule2]
251 @d # type: ignore
252 class C: ...
253 ERROR # E: Name "ERROR" is not defined
254
255 [case testDontIgnoreWholeModule3]
256 @d # type: ignore
257
258 def f(): ...
259 ERROR # E: Name "ERROR" is not defined
260
261 [case testIgnoreInsideFunctionDoesntAffectWhole]
262 # flags: --disallow-untyped-defs
263
264 def f(): # E: Function is missing a return type annotation
265 42 + 'no way' # type: ignore
266 return 0
267
268 [case testIgnoreInsideClassDoesntAffectWhole]
269 import six
270 class M(type): pass
271
272 @six.add_metaclass(M)
273 class CD(six.with_metaclass(M)): # E: Multiple metaclass definitions
274 42 + 'no way' # type: ignore
275
276 [builtins fixtures/tuple.pyi]
277
278 [case testUnusedIgnoreCodeOrder]
279 # flags: --warn-unused-ignores
280 5 # type: ignore[import, steven] # E: Unused "type: ignore[import, steven]" comment
281 -- User ordering of codes is preserved
282 5 # type: ignore[steven, import] # E: Unused "type: ignore[steven, import]" comment
283 -- Spacing is not preserved
284 5 # type: ignore[ steven, import ] # E: Unused "type: ignore[steven, import]" comment
285 -- Make sure it works as intended in more complex situations
286 1 + "ok" + "ok".foo # type: ignore[ operator,steven,attr-defined, import] # E: Unused "type: ignore[steven, import]" comment
287
288 [case testUnusedIgnoreTryExcept]
289 # flags: --warn-unused-ignores
290 try:
291 import foo # type: ignore # E: Unused "type: ignore" comment
292 import bar # type: ignore[import] # E: Unused "type: ignore" comment
293 import foobar # type: ignore[unused-ignore]
294 import barfoo # type: ignore[import,unused-ignore]
295 import missing # type: ignore[import,unused-ignore]
296 except Exception:
297 pass
298 [file foo.py]
299 [file bar.py]
300 [file foobar.py]
301 [file barfoo.py]
302 [builtins fixtures/exception.pyi]