Debian Codegraph @

test-data/unit/check-fastparse.test

raw
1 [case testFastParseSyntaxError_no_native_parse]
2
3 1 + # E: Invalid syntax
4
5 [case testFastParseTypeCommentSyntaxError_no_native_parse]
6
7 x = None # type: a : b # E: Syntax error in type comment "a : b"
8
9 [case testFastParseInvalidTypeComment]
10
11 x = None # type: a + b # E: Invalid type comment or annotation
12
13 -- Function type comments are attributed to the function def line.
14 -- This happens in both parsers.
15 [case testFastParseFunctionAnnotationSyntaxError_no_native_parse]
16
17 def f(): # E: Syntax error in type comment "None -> None" # N: Suggestion: wrap argument types in parentheses
18 # type: None -> None
19 pass
20
21 [case testFastParseFunctionAnnotationSyntaxErrorSpaces_no_native_parse]
22
23 def f(): # E: Syntax error in type comment "None -> None" # N: Suggestion: wrap argument types in parentheses
24 # type: None -> None
25 pass
26
27 [case testFastParseInvalidFunctionAnnotation]
28
29 def f(x): # E: Invalid type comment or annotation
30 # type: (a + b) -> None
31 pass
32
33 [case testFastParseInvalidTypes3]
34 # All of these should not crash
35 from typing import Callable, Tuple, Iterable
36
37 x: Tuple[int, str].x # E: Invalid type comment or annotation
38 a: Iterable[x].x # E: Invalid type comment or annotation
39 b: Tuple[x][x] # E: Invalid type comment or annotation
40 c: Iterable[x][x] # E: Invalid type comment or annotation
41 d: Callable[..., int][x] # E: Invalid type comment or annotation
42 e: Callable[..., int].x # E: Invalid type comment or annotation
43
44 f = None # type: Tuple[int, str].x # E: Invalid type comment or annotation
45 g = None # type: Iterable[x].x # E: Invalid type comment or annotation
46 h = None # type: Tuple[x][x] # E: Invalid type comment or annotation
47 i = None # type: Iterable[x][x] # E: Invalid type comment or annotation
48 j = None # type: Callable[..., int][x] # E: Invalid type comment or annotation
49 k = None # type: Callable[..., int].x # E: Invalid type comment or annotation
50
51 def f1(x: Tuple[int, str].x) -> None: pass # E: Invalid type comment or annotation
52 def f2(x: Iterable[x].x) -> None: pass # E: Invalid type comment or annotation
53 def f3(x: Tuple[x][x]) -> None: pass # E: Invalid type comment or annotation
54 def f4(x: Iterable[x][x]) -> None: pass # E: Invalid type comment or annotation
55 def f5(x: Callable[..., int][x]) -> None: pass # E: Invalid type comment or annotation
56 def f6(x: Callable[..., int].x) -> None: pass # E: Invalid type comment or annotation
57
58 [case testFastParseTypeWithIgnore]
59 def f(x, # type: x # type: ignore
60 ):
61 # type: (...) -> None
62 pass
63
64 [case testFastParseVariableTypeWithIgnore]
65
66 x = 1 # type: str # type: ignore
67
68 [case testFastParseVariableTypeWithIgnoreNoSpace]
69
70 x = 1 # type: str #type:ignore
71
72 [case testFastParseVariableTypeWithIgnoreAndComment]
73
74 x = 1 # type: str # type: ignore # comment
75
76 [case testFastParseTypeWithIgnoreWithStmt]
77 with open('test', 'r') as f: # type: int # type: ignore
78 pass
79
80 [case testFastParseTypeWithIgnoreForStmt]
81 for i in (1, 2, 3, 100): # type: str # type: ignore
82 pass
83 [builtins fixtures/tuple.pyi]
84
85 [case testFastParseVariableCommentThenIgnore]
86 a="test" # type: int #comment # type: ignore # E: Incompatible types in assignment (expression has type "str", variable has type "int")
87
88 [case testFastParseProperty]
89
90 class C:
91 @property
92 def x(self) -> str: pass
93 @x.setter
94 def x(self, value: str) -> None: pass
95 [builtins fixtures/property.pyi]
96
97 [case testFastParseConditionalProperty]
98
99 class C:
100 if bool():
101 @property
102 def x(self) -> str: pass
103 @x.setter
104 def x(self, value: str) -> None: pass
105 [builtins fixtures/property.pyi]
106
107 [case testFastParsePerArgumentAnnotations]
108 # flags: --implicit-optional
109
110 class A: pass
111 class B: pass
112 class C: pass
113 class D: pass
114 class E: pass
115 class F: pass
116 def f(a, # type: A
117 b = None, # type: B
118 *args, # type: C
119 d = None, # type: D
120 e, # type: E
121 **kwargs # type: F
122 ):
123 reveal_type(a) # N: Revealed type is "__main__.A"
124 reveal_type(b) # N: Revealed type is "__main__.B | None"
125 reveal_type(args) # N: Revealed type is "builtins.tuple[__main__.C, ...]"
126 reveal_type(d) # N: Revealed type is "__main__.D | None"
127 reveal_type(e) # N: Revealed type is "__main__.E"
128 reveal_type(kwargs) # N: Revealed type is "builtins.dict[builtins.str, __main__.F]"
129 [builtins fixtures/dict.pyi]
130 [out]
131
132 [case testFastParsePerArgumentAnnotationsWithReturn]
133 # flags: --implicit-optional
134
135 class A: pass
136 class B: pass
137 class C: pass
138 class D: pass
139 class E: pass
140 class F: pass
141 def f(a, # type: A
142 b = None, # type: B
143 *args, # type: C
144 d = None, # type: D
145 e, # type: E
146 **kwargs # type: F
147 ):
148 # type: (...) -> int
149 reveal_type(a) # N: Revealed type is "__main__.A"
150 reveal_type(b) # N: Revealed type is "__main__.B | None"
151 reveal_type(args) # N: Revealed type is "builtins.tuple[__main__.C, ...]"
152 reveal_type(d) # N: Revealed type is "__main__.D | None"
153 reveal_type(e) # N: Revealed type is "__main__.E"
154 reveal_type(kwargs) # N: Revealed type is "builtins.dict[builtins.str, __main__.F]"
155 return "not an int" # E: Incompatible return value type (got "str", expected "int")
156 [builtins fixtures/dict.pyi]
157 [out]
158
159 [case testFastParsePerArgumentAnnotationsWithAnnotatedBareStar_no_native_parse]
160
161 def f(*, # type: int # E: Bare * has associated type comment
162 x # type: str
163 ):
164 # type: (...) -> int
165 pass
166 [builtins fixtures/dict.pyi]
167 [out]
168
169 [case testFastParsePerArgumentAnnotationsWithReturnAndBareStar]
170
171 def f(*,
172 x # type: str
173 ):
174 # type: (...) -> int
175 reveal_type(x) # N: Revealed type is "builtins.str"
176 return "not an int" # E: Incompatible return value type (got "str", expected "int")
177 [builtins fixtures/dict.pyi]
178 [out]
179
180 [case testFasterParseTooManyArgumentsAnnotation]
181 def f(): # E: Type signature has too many parameters
182 # type: (int) -> None
183 pass
184
185 f()
186 f(1) # E: Too many arguments for "f"
187
188 [case testFasterParseTooFewArgumentsAnnotation]
189 def f(x, y): # E: Type signature has too few parameters
190 # type: (int) -> None
191 x()
192 y()
193
194 f(1, 2)
195 f(1) # E: Missing positional argument "y" in call to "f"
196
197 [case testFasterParseTypeErrorCustom]
198
199 from typing import TypeVar, Generic
200 T = TypeVar('T')
201 class Foo(Generic[T]):
202 pass
203
204 def f(a: Foo(int)) -> int:
205 pass
206 [out]
207 main:7: error: Invalid type comment or annotation
208 main:7: note: Suggestion: use Foo[...] instead of Foo(...)
209
210 [case testFasterParseCallWithKeywordArgs]
211
212 def Foo(sort: bool) -> type:
213 return int
214
215 def f(a: Foo(sort=True)) -> int:
216 pass
217 [out]
218 main:5: error: Invalid type comment or annotation
219 main:5: note: Cannot use a function call in a type annotation
220
221 [case testFastParseMatMul]
222
223 from typing import Any
224 x = None # type: Any
225 x @ 1
226 x @= 1
227
228 [case testFastParserShowsMultipleErrors]
229 def f(x): # E: Type signature has too few parameters
230 # type: () -> None
231 pass
232 def g(): # E: Type signature has too many parameters
233 # type: (int) -> None
234 pass
235
236 [case testFastParseMalformedAssert]
237
238 assert 1, 2
239 assert (1, 2) # E: Assertion is always true, perhaps remove parentheses?
240 assert (1, 2), 3 # E: Assertion is always true, perhaps remove parentheses?
241 assert (1,) # E: Assertion is always true, perhaps remove parentheses?
242 assert ()
243 [builtins fixtures/tuple.pyi]
244
245 [case testFastParseAssertMessage]
246
247 assert 1
248 assert 1, 2
249 assert 1, 1+2
250 assert 1, 1+'test' # E: Unsupported operand types for + ("int" and "str")
251 assert 1, f() # E: Name "f" is not defined
252
253 [case testFastParserConsistentFunctionTypes]
254
255 def f1(x, y, z):
256 # type: (int, int, int) -> int
257 pass
258
259 def f2(x, # type: int # E: Function has duplicate type signatures
260 y, # type: int
261 z # type: int
262 ):
263 # type: (int, int, int) -> int
264 pass
265
266 def f3(x, # type: int
267 y, # type: int
268 z # type: int
269 ):
270 # type: (...) -> int
271 pass
272
273 def f4(x, y, z):
274 # type: (int, int, int) -> int
275 pass
276
277 def f5(x) -> int: # E: Function has duplicate type signatures
278 # type: (int) -> int
279 pass
280
281 def f6(x: int, y: int, z: int):
282 # type: (...) -> int
283 pass
284
285 def f7(x: int): # E: Function has duplicate type signatures
286 # type: (int) -> int
287 pass
288
289 [case testFastParserDuplicateNames_no_native_parse]
290
291 def f(x, y, z):
292 pass
293
294 def g(x, y, x): # E: Duplicate parameter "x" in function definition
295 pass
296
297 def h(x, y, *x): # E: Duplicate parameter "x" in function definition
298 pass
299
300 def i(x, y, *z, **z): # E: Duplicate parameter "z" in function definition
301 pass
302
303 def j(x: int, y: int, *, x: int = 3): # E: Duplicate parameter "x" in function definition
304 pass
305
306 def k(*, y, z, y): # E: Duplicate parameter "y" in function definition
307 pass
308
309 lambda x, y, x: ... # E: Duplicate parameter "x" in function definition
310
311 [case testNoCrashOnImportFromStar]
312 from pack import *
313 [file pack/__init__.py]
314 from . import *
315
316 [case testNoCrashOnImportFromStarNested]
317
318 import blamodule
319 [file blamodule/__init__.py]
320 from . import command
321 from . import backends
322
323 [file blamodule/backends/__init__.py]
324 from .Bla import Bla
325 Bla().method()
326
327 [file blamodule/backends/Bla.py]
328 from .. import *
329
330 class Bla:
331 def method(self) -> str:
332 return command.call()
333
334 [file blamodule/command.py]
335 def call() -> str: pass
336
337 [builtins fixtures/module.pyi]
338
339 [case testInvalidEscapeSequenceWarningsSuppressed]
340 # flags: --python-version 3.15
341 # Test that SyntaxWarnings for invalid escape sequences are suppressed
342 # when parsing potential type expressions containing regex patterns or
343 # similar strings. Callable arguments are always potential type expressions.
344 from typing import TypeForm
345
346 def identity(typx: TypeForm) -> TypeForm:
347 return typx
348
349 # This should not generate SyntaxWarning despite invalid escape sequence
350 identity(r"re\.match") # E: Argument 1 to "identity" has incompatible type "str"; expected "TypeForm[Any]"
351 [builtins fixtures/tuple.pyi]
352 [typing fixtures/typing-full.pyi]