/
check-fastparse.test
437 lines (345 loc) · 12.3 KB
/
check-fastparse.test
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
[case testFastParseSyntaxError]
1 + # E: invalid syntax
[case testFastParseTypeCommentSyntaxError]
x = None # type: a : b # E: syntax error in type comment
[case testFastParseInvalidTypeComment]
x = None # type: a + b # E: invalid type comment or annotation
-- Function type comments are attributed to the function def line.
-- This happens in both parsers.
[case testFastParseFunctionAnnotationSyntaxError]
def f(): # E: syntax error in type comment # N: Suggestion: wrap argument types in parentheses
# type: None -> None
pass
[case testFastParseFunctionAnnotationSyntaxErrorSpaces]
def f(): # E: syntax error in type comment # N: Suggestion: wrap argument types in parentheses
# type: None -> None
pass
[case testFastParseInvalidFunctionAnnotation]
def f(x): # E: invalid type comment or annotation
# type: (a + b) -> None
pass
[case testFastParseInvalidTypes2]
# flags: --py2
# All of these should not crash
from typing import Callable, Tuple, Iterable
x = None # type: Tuple[int, str].x # E: invalid type comment or annotation
x = None # type: Iterable[x].x # E: invalid type comment or annotation
x = None # type: Tuple[x][x] # E: invalid type comment or annotation
x = None # type: Iterable[x][x] # E: invalid type comment or annotation
x = None # type: Callable[..., int][x] # E: invalid type comment or annotation
x = None # type: Callable[..., int].x # E: invalid type comment or annotation
x = None # type: Tuple[1] # E: invalid type comment or annotation
def f1(x): # E: invalid type comment or annotation
# type: (Tuple[int, str].x) -> None
pass
def f2(x): # E: invalid type comment or annotation
# type: (Iterable[x].x) -> None
pass
def f3(x): # E: invalid type comment or annotation
# type: (Tuple[x][x]) -> None
pass
def f4(x): # E: invalid type comment or annotation
# type: (Iterable[x][x]) -> None
pass
def f5(x): # E: invalid type comment or annotation
# type: (Callable[..., int][x]) -> None
pass
def f6(x): # E: invalid type comment or annotation
# type: (Callable[..., int].x) -> None
pass
def f7(x): # E: invalid type comment or annotation
# type: (Tuple[1]) -> None
pass
[case testFastParseInvalidTypes3]
# flags: --python-version 3.6
# All of these should not crash
from typing import Callable, Tuple, Iterable
x: Tuple[int, str].x # E: invalid type comment or annotation
x: Iterable[x].x # E: invalid type comment or annotation
x: Tuple[x][x] # E: invalid type comment or annotation
x: Iterable[x][x] # E: invalid type comment or annotation
x: Callable[..., int][x] # E: invalid type comment or annotation
x: Callable[..., int].x # E: invalid type comment or annotation
x: Tuple[1] # E: invalid type comment or annotation
x = None # type: Tuple[int, str].x # E: invalid type comment or annotation
x = None # type: Iterable[x].x # E: invalid type comment or annotation
x = None # type: Tuple[x][x] # E: invalid type comment or annotation
x = None # type: Iterable[x][x] # E: invalid type comment or annotation
x = None # type: Callable[..., int][x] # E: invalid type comment or annotation
x = None # type: Callable[..., int].x # E: invalid type comment or annotation
x = None # type: Tuple[1] # E: invalid type comment or annotation
def f1(x: Tuple[int, str].x) -> None: pass # E: invalid type comment or annotation
def f2(x: Iterable[x].x) -> None: pass # E: invalid type comment or annotation
def f3(x: Tuple[x][x]) -> None: pass # E: invalid type comment or annotation
def f4(x: Iterable[x][x]) -> None: pass # E: invalid type comment or annotation
def f5(x: Callable[..., int][x]) -> None: pass # E: invalid type comment or annotation
def f6(x: Callable[..., int].x) -> None: pass # E: invalid type comment or annotation
def f7(x: Tuple[1]) -> None: pass # E: invalid type comment or annotation
[case testFastParseProperty]
class C:
@property
def x(self) -> str: pass
@x.setter
def x(self, value: str) -> None: pass
[builtins fixtures/property.pyi]
[case testFastParseConditionalProperty]
class C:
if bool():
@property
def x(self) -> str: pass
@x.setter
def x(self, value: str) -> None: pass
[builtins fixtures/property.pyi]
[case testFastParsePerArgumentAnnotations]
class A: pass
class B: pass
class C: pass
class D: pass
class E: pass
class F: pass
def f(a, # type: A
b = None, # type: B
*args, # type: C
d = None, # type: D
e, # type: E
**kwargs # type: F
):
reveal_type(a) # E: Revealed type is '__main__.A'
reveal_type(b) # E: Revealed type is 'Union[__main__.B, builtins.None]'
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
reveal_type(d) # E: Revealed type is 'Union[__main__.D, builtins.None]'
reveal_type(e) # E: Revealed type is '__main__.E'
reveal_type(kwargs) # E: Revealed type is 'builtins.dict[builtins.str, __main__.F]'
[builtins fixtures/dict.pyi]
[out]
[case testFastParsePerArgumentAnnotationsWithReturn]
class A: pass
class B: pass
class C: pass
class D: pass
class E: pass
class F: pass
def f(a, # type: A
b = None, # type: B
*args, # type: C
d = None, # type: D
e, # type: E
**kwargs # type: F
):
# type: (...) -> int
reveal_type(a) # E: Revealed type is '__main__.A'
reveal_type(b) # E: Revealed type is 'Union[__main__.B, builtins.None]'
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
reveal_type(d) # E: Revealed type is 'Union[__main__.D, builtins.None]'
reveal_type(e) # E: Revealed type is '__main__.E'
reveal_type(kwargs) # E: Revealed type is 'builtins.dict[builtins.str, __main__.F]'
return "not an int" # E: Incompatible return value type (got "str", expected "int")
[builtins fixtures/dict.pyi]
[out]
[case testFastParsePerArgumentAnnotationsWithAnnotatedBareStar]
def f(*, # type: int # E: bare * has associated type comment
x # type: str
):
# type: (...) -> int
pass
[builtins fixtures/dict.pyi]
[out]
[case testFastParsePerArgumentAnnotationsWithReturnAndBareStar]
def f(*,
x # type: str
):
# type: (...) -> int
reveal_type(x) # E: Revealed type is 'builtins.str'
return "not an int" # E: Incompatible return value type (got "str", expected "int")
[builtins fixtures/dict.pyi]
[out]
[case testFastParsePerArgumentAnnotations_python2]
class A: pass
class B: pass
class C: pass
class D: pass
def f(a, # type: A
b = None, # type: B
*args # type: C
# kwargs not tested due to lack of 2.7 dict fixtures
):
reveal_type(a) # E: Revealed type is '__main__.A'
reveal_type(b) # E: Revealed type is 'Union[__main__.B, builtins.None]'
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
[builtins fixtures/dict.pyi]
[out]
[case testFastParsePerArgumentAnnotationsWithReturn_python2]
class A: pass
class B: pass
class C: pass
class D: pass
def f(a, # type: A
b = None, # type: B
*args # type: C
# kwargs not tested due to lack of 2.7 dict fixtures
):
# type: (...) -> int
reveal_type(a) # E: Revealed type is '__main__.A'
reveal_type(b) # E: Revealed type is 'Union[__main__.B, builtins.None]'
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
return "not an int" # E: Incompatible return value type (got "str", expected "int")
[builtins fixtures/dict.pyi]
[out]
[case testFasterParseTooManyArgumentsAnnotation]
def f(): # E: Type signature has too many arguments
# type: (int) -> None
pass
[case testFasterParseTooFewArgumentsAnnotation]
def f(x): # E: Type signature has too few arguments
# type: () -> None
pass
[case testFasterParseTypeCommentError_python2]
from typing import Tuple
def f(a):
# type: (Tuple(int, int)) -> int
pass
[out]
main:3: error: invalid type comment or annotation
main:3: note: Suggestion: use Tuple[...] instead of Tuple(...)
[case testFasterParseTypeErrorList_python2]
from typing import List
def f(a):
# type: (List(int)) -> int
pass
[out]
main:3: error: invalid type comment or annotation
main:3: note: Suggestion: use List[...] instead of List(...)
[case testFasterParseTypeErrorCustom]
from typing import TypeVar, Generic
T = TypeVar('T')
class Foo(Generic[T]):
pass
def f(a: Foo(int)) -> int:
pass
[out]
main:7: error: invalid type comment or annotation
main:7: note: Suggestion: use Foo[...] instead of Foo(...)
[case testFastParseMatMul]
from typing import Any
x = None # type: Any
x @ 1
x @= 1
[case testIncorrectTypeCommentIndex]
from typing import Dict
x = None # type: Dict[x: y]
[out]
main:3: error: syntax error in type comment
[case testPrintStatementTrailingCommaFastParser_python2]
print 0,
print 1, 2,
[case testFastParserShowsMultipleErrors]
def f(x): # E: Type signature has too few arguments
# type: () -> None
pass
def g(): # E: Type signature has too many arguments
# type: (int) -> None
pass
[case testFastParseMalformedAssert]
assert 1, 2
assert (1, 2) # W: Assertion is always true, perhaps remove parentheses?
assert (1, 2), 3 # W: Assertion is always true, perhaps remove parentheses?
assert ()
assert (1,) # W: Assertion is always true, perhaps remove parentheses?
[case testFastParseAssertMessage]
assert 1
assert 1, 2
assert 1, 1+2
assert 1, 1+'test' # E: Unsupported operand types for + ("int" and "str")
assert 1, f() # E: Name 'f' is not defined
[case testFastParserConsistentFunctionTypes]
def f(x, y, z):
# type: (int, int, int) -> int
pass
def f(x, # type: int # E: Function has duplicate type signatures
y, # type: int
z # type: int
):
# type: (int, int, int) -> int
pass
def f(x, # type: int
y, # type: int
z # type: int
):
# type: (...) -> int
pass
def f(x, y, z):
# type: (int, int, int) -> int
pass
def f(x) -> int: # E: Function has duplicate type signatures
# type: (int) -> int
pass
def f(x: int, y: int, z: int):
# type: (...) -> int
pass
def f(x: int): # E: Function has duplicate type signatures
# type: (int) -> int
pass
[case testFastParserDuplicateNames]
def f(x, y, z):
pass
def g(x, y, x): # E: Duplicate argument 'x' in function definition
pass
def h(x, y, *x): # E: Duplicate argument 'x' in function definition
pass
def i(x, y, *z, **z): # E: Duplicate argument 'z' in function definition
pass
def j(x: int, y: int, *, x: int = 3): # E: Duplicate argument 'x' in function definition
pass
def k(*, y, z, y): # E: Duplicate argument 'y' in function definition
pass
lambda x, y, x: ... # E: Duplicate argument 'x' in function definition
[case testFastParserDuplicateNames_python2]
def f(x, y, z):
pass
def g(x, y, x): # E: Duplicate argument 'x' in function definition
pass
def h(x, y, *x): # E: Duplicate argument 'x' in function definition
pass
def i(x, y, *z, **z): # E: Duplicate argument 'z' in function definition
pass
def j(x, (y, y), z): # E: Duplicate argument 'y' in function definition
pass
def k(x, (y, x)): # E: Duplicate argument 'x' in function definition
pass
def l((x, y), (z, x)): # E: Duplicate argument 'x' in function definition
pass
def m(x, ((x, y), z)): # E: Duplicate argument 'x' in function definition
pass
lambda x, (y, x): None # E: Duplicate argument 'x' in function definition
[case testNoCrashOnImportFromStar]
from pack import *
[file pack/__init__.py]
from . import *
[case testNoCrashOnImportFromStarNested]
import blamodule
[file blamodule/__init__.py]
from . import command
from . import backends
[file blamodule/backends/__init__.py]
from .Bla import Bla
reveal_type(Bla().method()) # E: Revealed type is 'builtins.str'
[file blamodule/backends/Bla.py]
from .. import *
class Bla:
def method(self) -> str:
return command.call()
[file blamodule/command.py]
def call() -> str: pass
[case testNoCrashOnImportFromStarPython2]
# flags: --py2
from . import * # E: No parent module -- cannot perform relative import
[case testSpuriousTrailingComma_python2]
from typing import Optional
def update_state(tid, # type: int
vid, # type: int
update_ts=None, # type: Optional[float],
): # type: (...) -> str
pass
[out]
main:3: error: Syntax error in type annotation
main:3: note: Suggestion: Is there a spurious trailing comma?