/
check-python38.test
603 lines (471 loc) · 18.8 KB
/
check-python38.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
[case testDecoratedClassLine]
def d(c): ...
@d
class C: ...
class C: ... # E: Name "C" already defined on line 4
[case testDecoratedFunctionLine]
# flags: --disallow-untyped-defs
def d(f): ... # type: ignore
@d
def f(): ... # E: Function is missing a return type annotation \
# N: Use "-> None" if function does not return a value
[case testIgnoreDecoratedFunction1]
# flags: --disallow-untyped-defs --warn-unused-ignores
def d(f): ... # type: ignore
@d
# type: ignore
def f(): ... # type: ignore # E: Unused "type: ignore" comment
[case testIgnoreDecoratedFunction2]
# flags: --disallow-untyped-defs
def d(f): ... # type: ignore
@d
def f(): ... # type: ignore
[case testIgnoreScopeIssue1032]
def f(a: int): ...
f(
1,
2,
) # type: ignore
[case testIgnoreScopeNested1]
def g(x: str) -> str: ...
def f(a: int) -> int: ...
f(
f(
g(
"IGNORE"
) # type: ignore
)
)
[case testIgnoreScopeNested2]
[
"IGNORE" # type: ignore
&
"IGNORE",
]
[builtins fixtures/list.pyi]
[case testIgnoreScopeNested3]
{
"IGNORE"
| # type: ignore
"IGNORE",
}
[builtins fixtures/set.pyi]
[case testIgnoreScopeNested4]
{
None: "IGNORE"
^
"IGNORE", # type: ignore
}
[builtins fixtures/dict.pyi]
[case testIgnoreScopeNestedNonOverlapping]
def f(x: int): ...
def g(x: int): ...
(
f("ERROR"), # E: Argument 1 to "f" has incompatible type "str"; expected "int"
g("IGNORE"), # type: ignore
f("ERROR"), # E: Argument 1 to "f" has incompatible type "str"; expected "int"
)
[builtins fixtures/tuple.pyi]
[case testIgnoreScopeNestedOverlapping]
def f(x: int): ...
def g(x: int): ...
(
f("ERROR"), g( # E: Argument 1 to "f" has incompatible type "str"; expected "int"
"IGNORE" # type: ignore
), f("ERROR"), # E: Argument 1 to "f" has incompatible type "str"; expected "int"
)
[builtins fixtures/tuple.pyi]
[case testIgnoreScopeUnused1]
# flags: --warn-unused-ignores
( # type: ignore # E: Unused "type: ignore" comment
"IGNORE" # type: ignore # E: Unused "type: ignore" comment
+ # type: ignore # E: Unused "type: ignore" comment
0 # type: ignore
) # type: ignore # E: Unused "type: ignore" comment
[builtins fixtures/primitives.pyi]
[case testIgnoreScopeUnused2]
# flags: --warn-unused-ignores
( # type: ignore # E: Unused "type: ignore" comment
"IGNORE"
- # type: ignore
0 # type: ignore # E: Unused "type: ignore" comment
) # type: ignore # E: Unused "type: ignore" comment
[case testIgnoreScopeUnused3]
# flags: --warn-unused-ignores
( # type: ignore # E: Unused "type: ignore" comment
"IGNORE"
/
0 # type: ignore
) # type: ignore # E: Unused "type: ignore" comment
[case testPEP570ArgTypesMissing]
# flags: --python-version 3.8 --disallow-untyped-defs
def f(arg, /) -> None: ... # E: Function is missing a type annotation for one or more arguments
[case testPEP570ArgTypesBadDefault]
# flags: --python-version 3.8
def f(arg: int = "ERROR", /) -> None: ... # E: Incompatible default for argument "arg" (default has type "str", argument has type "int")
[case testPEP570ArgTypesDefault]
# flags: --python-version 3.8
def f(arg: int = 0, /) -> None:
reveal_type(arg) # N: Revealed type is "builtins.int"
[case testPEP570ArgTypesRequired]
# flags: --python-version 3.8
def f(arg: int, /) -> None:
reveal_type(arg) # N: Revealed type is "builtins.int"
[case testPEP570Required]
# flags: --python-version 3.8
def f(arg: int, /) -> None: ... # N: "f" defined here
f(1)
f("ERROR") # E: Argument 1 to "f" has incompatible type "str"; expected "int"
f(arg=1) # E: Unexpected keyword argument "arg" for "f"
f(arg="ERROR") # E: Unexpected keyword argument "arg" for "f"
[case testPEP570Default]
# flags: --python-version 3.8
def f(arg: int = 0, /) -> None: ... # N: "f" defined here
f()
f(1)
f("ERROR") # E: Argument 1 to "f" has incompatible type "str"; expected "int"
f(arg=1) # E: Unexpected keyword argument "arg" for "f"
f(arg="ERROR") # E: Unexpected keyword argument "arg" for "f"
[case testPEP570Calls]
# flags: --python-version 3.8 --no-strict-optional
from typing import Any, Dict
def f(p, /, p_or_kw, *, kw) -> None: ... # N: "f" defined here
d = None # type: Dict[Any, Any]
f(0, 0, 0) # E: Too many positional arguments for "f"
f(0, 0, kw=0)
f(0, p_or_kw=0, kw=0)
f(p=0, p_or_kw=0, kw=0) # E: Unexpected keyword argument "p" for "f"
f(0, **d)
f(**d) # E: Missing positional argument "p_or_kw" in call to "f"
[builtins fixtures/dict.pyi]
[case testPEP570Signatures1]
# flags: --python-version 3.8
def f(p1: bytes, p2: float, /, p_or_kw: int, *, kw: str) -> None:
reveal_type(p1) # N: Revealed type is "builtins.bytes"
reveal_type(p2) # N: Revealed type is "builtins.float"
reveal_type(p_or_kw) # N: Revealed type is "builtins.int"
reveal_type(kw) # N: Revealed type is "builtins.str"
[case testPEP570Signatures2]
# flags: --python-version 3.8
def f(p1: bytes, p2: float = 0.0, /, p_or_kw: int = 0, *, kw: str) -> None:
reveal_type(p1) # N: Revealed type is "builtins.bytes"
reveal_type(p2) # N: Revealed type is "builtins.float"
reveal_type(p_or_kw) # N: Revealed type is "builtins.int"
reveal_type(kw) # N: Revealed type is "builtins.str"
[case testPEP570Signatures3]
# flags: --python-version 3.8
def f(p1: bytes, p2: float = 0.0, /, *, kw: int) -> None:
reveal_type(p1) # N: Revealed type is "builtins.bytes"
reveal_type(p2) # N: Revealed type is "builtins.float"
reveal_type(kw) # N: Revealed type is "builtins.int"
[case testPEP570Signatures4]
# flags: --python-version 3.8
def f(p1: bytes, p2: int = 0, /) -> None:
reveal_type(p1) # N: Revealed type is "builtins.bytes"
reveal_type(p2) # N: Revealed type is "builtins.int"
[case testPEP570Signatures5]
# flags: --python-version 3.8
def f(p1: bytes, p2: float, /, p_or_kw: int) -> None:
reveal_type(p1) # N: Revealed type is "builtins.bytes"
reveal_type(p2) # N: Revealed type is "builtins.float"
reveal_type(p_or_kw) # N: Revealed type is "builtins.int"
[case testPEP570Signatures6]
# flags: --python-version 3.8
def f(p1: bytes, p2: float, /) -> None:
reveal_type(p1) # N: Revealed type is "builtins.bytes"
reveal_type(p2) # N: Revealed type is "builtins.float"
[case testPEP570Unannotated]
# flags: --python-version 3.8
def f(arg, /): ... # N: "f" defined here
g = lambda arg, /: arg
def h(arg=0, /): ... # N: "h" defined here
i = lambda arg=0, /: arg
f(1)
g(1)
h()
h(1)
i()
i(1)
f(arg=0) # E: Unexpected keyword argument "arg" for "f"
g(arg=0) # E: Unexpected keyword argument "arg"
h(arg=0) # E: Unexpected keyword argument "arg" for "h"
i(arg=0) # E: Unexpected keyword argument "arg"
[case testWalrus]
# flags: --strict-optional --python-version 3.8
from typing import NamedTuple, Optional, List
from typing_extensions import Final
if a := 2:
reveal_type(a) # N: Revealed type is "builtins.int"
while b := "x":
reveal_type(b) # N: Revealed type is "builtins.str"
l = [y2 := 1, y2 + 2, y2 + 3]
reveal_type(y2) # N: Revealed type is "builtins.int"
reveal_type(l) # N: Revealed type is "builtins.list[builtins.int]"
filtered_data = [y3 for x in l if (y3 := a) is not None]
reveal_type(filtered_data) # N: Revealed type is "builtins.list[builtins.int]"
reveal_type(y3) # N: Revealed type is "builtins.int"
d = {'a': (a2 := 1), 'b': a2 + 1, 'c': a2 + 2}
reveal_type(d) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
reveal_type(a2) # N: Revealed type is "builtins.int"
d2 = {(prefix := 'key_') + 'a': (start_val := 1), prefix + 'b': start_val + 1, prefix + 'c': start_val + 2}
reveal_type(d2) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
reveal_type(prefix) # N: Revealed type is "builtins.str"
reveal_type(start_val) # N: Revealed type is "builtins.int"
filtered_dict = {k: new_v for k, v in [('a', 1), ('b', 2), ('c', 3)] if (new_v := v + 1) == 2}
reveal_type(filtered_dict) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
reveal_type(new_v) # N: Revealed type is "builtins.int"
def f(x: int = (c := 4)) -> int:
if a := 2:
reveal_type(a) # N: Revealed type is "builtins.int"
while b := "x":
reveal_type(b) # N: Revealed type is "builtins.str"
x = (y := 1) + (z := 2)
reveal_type(x) # N: Revealed type is "builtins.int"
reveal_type(y) # N: Revealed type is "builtins.int"
reveal_type(z) # N: Revealed type is "builtins.int"
l = [y2 := 1, y2 + 2, y2 + 3]
reveal_type(y2) # N: Revealed type is "builtins.int"
reveal_type(l) # N: Revealed type is "builtins.list[builtins.int]"
filtered_data = [y3 for x in l if (y3 := a) is not None]
reveal_type(filtered_data) # N: Revealed type is "builtins.list[builtins.int]"
reveal_type(y3) # N: Revealed type is "builtins.int"
d = {'a': (a2 := 1), 'b': a2 + 1, 'c': a2 + 2}
reveal_type(d) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
reveal_type(a2) # N: Revealed type is "builtins.int"
d2 = {(prefix := 'key_') + 'a': (start_val := 1), prefix + 'b': start_val + 1, prefix + 'c': start_val + 2}
reveal_type(d2) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
reveal_type(prefix) # N: Revealed type is "builtins.str"
reveal_type(start_val) # N: Revealed type is "builtins.int"
filtered_dict = {k: new_v for k, v in [('a', 1), ('b', 2), ('c', 3)] if (new_v := v + 1) == 2}
reveal_type(filtered_dict) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
reveal_type(new_v) # N: Revealed type is "builtins.int"
# https://www.python.org/dev/peps/pep-0572/#exceptional-cases
(y4 := 3)
reveal_type(y4) # N: Revealed type is "builtins.int"
y5 = (y6 := 3)
reveal_type(y5) # N: Revealed type is "builtins.int"
reveal_type(y6) # N: Revealed type is "builtins.int"
f(x=(y7 := 3))
reveal_type(y7) # N: Revealed type is "builtins.int"
reveal_type((lambda: (y8 := 3) and y8)()) # N: Revealed type is "builtins.int"
y8 # E: Name "y8" is not defined
y7 = 1.0 # E: Incompatible types in assignment (expression has type "float", variable has type "int")
if y7 := "x": # E: Incompatible types in assignment (expression has type "str", variable has type "int")
pass
# Just make sure we don't crash on this sort of thing.
if NT := NamedTuple("NT", [("x", int)]): # E: "int" not callable
z2: NT # E: Variable "NT" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
if Alias := int:
z3: Alias # E: Variable "Alias" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
if (reveal_type(y9 := 3) and # N: Revealed type is "Literal[3]?"
reveal_type(y9)): # N: Revealed type is "builtins.int"
reveal_type(y9) # N: Revealed type is "builtins.int"
return (y10 := 3) + y10
reveal_type(c) # N: Revealed type is "builtins.int"
def check_final() -> None:
x: Final = 3
if x := 4: # E: Cannot assign to final name "x"
pass
def check_binder(x: Optional[int], y: Optional[int], z: Optional[int], a: Optional[int],
b: Optional[int]) -> None:
reveal_type(x) # N: Revealed type is "Union[builtins.int, None]"
(x := 1)
reveal_type(x) # N: Revealed type is "builtins.int"
if x or (y := 1):
reveal_type(y) # N: Revealed type is "Union[builtins.int, None]"
if x and (y := 1):
reveal_type(y) # N: Revealed type is "builtins.int"
if (a := 1) and x:
reveal_type(a) # N: Revealed type is "builtins.int"
if (b := 1) or x:
reveal_type(b) # N: Revealed type is "builtins.int"
if z := 1:
reveal_type(z) # N: Revealed type is "builtins.int"
def check_partial() -> None:
x = None
if bool() and (x := 2):
pass
reveal_type(x) # N: Revealed type is "Union[builtins.int, None]"
def check_narrow(x: Optional[int], s: List[int]) -> None:
if (y := x):
reveal_type(y) # N: Revealed type is "builtins.int"
if (y := x) is not None:
reveal_type(y) # N: Revealed type is "builtins.int"
if (y := x) == 10:
reveal_type(y) # N: Revealed type is "builtins.int"
if (y := x) in s:
reveal_type(y) # N: Revealed type is "builtins.int"
if isinstance((y := x), int):
reveal_type(y) # N: Revealed type is "builtins.int"
class AssignmentExpressionsClass:
x = (y := 1) + (z := 2)
reveal_type(z) # N: Revealed type is "builtins.int"
l = [x2 := 1, 2, 3]
reveal_type(x2) # N: Revealed type is "builtins.int"
def __init__(self) -> None:
reveal_type(self.z) # N: Revealed type is "builtins.int"
l = [z2 := 1, z2 + 2, z2 + 3]
reveal_type(z2) # N: Revealed type is "builtins.int"
reveal_type(l) # N: Revealed type is "builtins.list[builtins.int]"
filtered_data = [z3 for x in l if (z3 := 1) is not None]
reveal_type(filtered_data) # N: Revealed type is "builtins.list[builtins.int]"
reveal_type(z3) # N: Revealed type is "builtins.int"
# Assignment expressions from inside the class should not escape the class scope.
reveal_type(x2) # E: Name "x2" is not defined # N: Revealed type is "Any"
reveal_type(z2) # E: Name "z2" is not defined # N: Revealed type is "Any"
[builtins fixtures/isinstancelist.pyi]
[case testWalrusConditionalTypeBinder]
# flags: --python-version 3.8
from typing import Union
from typing_extensions import Literal
class Good:
@property
def is_good(self) -> Literal[True]: ...
class Bad:
@property
def is_good(self) -> Literal[False]: ...
def get_thing() -> Union[Good, Bad]: ...
if (thing := get_thing()).is_good:
reveal_type(thing) # N: Revealed type is "__main__.Good"
else:
reveal_type(thing) # N: Revealed type is "__main__.Bad"
[builtins fixtures/property.pyi]
[case testWalrusConditionalTypeCheck]
# flags: --strict-optional --python-version 3.8
from typing import Optional
maybe_str: Optional[str]
if (is_str := maybe_str is not None):
reveal_type(is_str) # N: Revealed type is "Literal[True]"
reveal_type(maybe_str) # N: Revealed type is "builtins.str"
else:
reveal_type(is_str) # N: Revealed type is "Literal[False]"
reveal_type(maybe_str) # N: Revealed type is "None"
reveal_type(maybe_str) # N: Revealed type is "Union[builtins.str, None]"
[builtins fixtures/bool.pyi]
[case testWalrusConditionalTypeCheck2]
# flags: --python-version 3.8
from typing import Optional
maybe_str: Optional[str]
if (x := maybe_str) is not None:
reveal_type(x) # N: Revealed type is "builtins.str"
reveal_type(maybe_str) # N: Revealed type is "Union[builtins.str, None]"
else:
reveal_type(x) # N: Revealed type is "None"
reveal_type(maybe_str) # N: Revealed type is "Union[builtins.str, None]"
reveal_type(maybe_str) # N: Revealed type is "Union[builtins.str, None]"
[builtins fixtures/bool.pyi]
[case testWalrusPartialTypes]
# flags: --python-version 3.8
from typing import List
def check_partial_list() -> None:
if (x := []): # E: Need type annotation for "x" (hint: "x: List[<type>] = ...")
pass
y: List[str]
if (y := []):
pass
if (z := []):
z.append(3)
reveal_type(z) # N: Revealed type is "builtins.list[builtins.int]"
[builtins fixtures/list.pyi]
[case testWalrusAssignmentAndConditionScopeForLiteral]
# flags: --warn-unreachable --python-version 3.8
if (x := 0):
reveal_type(x) # E: Statement is unreachable
else:
reveal_type(x) # N: Revealed type is "builtins.int"
reveal_type(x) # N: Revealed type is "builtins.int"
[case testWalrusAssignmentAndConditionScopeForProperty]
# flags: --warn-unreachable --python-version 3.8
from typing_extensions import Literal
class PropertyWrapper:
@property
def f(self) -> str: ...
@property
def always_false(self) -> Literal[False]: ...
wrapper = PropertyWrapper()
if x := wrapper.f:
reveal_type(x) # N: Revealed type is "builtins.str"
else:
reveal_type(x) # N: Revealed type is "builtins.str"
reveal_type(x) # N: Revealed type is "builtins.str"
if y := wrapper.always_false:
reveal_type(y) # E: Statement is unreachable
else:
reveal_type(y) # N: Revealed type is "Literal[False]"
reveal_type(y) # N: Revealed type is "Literal[False]"
[builtins fixtures/property.pyi]
[case testWalrusAssignmentAndConditionScopeForFunction]
# flags: --warn-unreachable --python-version 3.8
from typing_extensions import Literal
def f() -> str: ...
if x := f():
reveal_type(x) # N: Revealed type is "builtins.str"
else:
reveal_type(x) # N: Revealed type is "builtins.str"
reveal_type(x) # N: Revealed type is "builtins.str"
def always_false() -> Literal[False]: ...
if y := always_false():
reveal_type(y) # E: Statement is unreachable
else:
reveal_type(y) # N: Revealed type is "Literal[False]"
reveal_type(y) # N: Revealed type is "Literal[False]"
def always_false_with_parameter(x: int) -> Literal[False]: ...
if z := always_false_with_parameter(5):
reveal_type(z) # E: Statement is unreachable
else:
reveal_type(z) # N: Revealed type is "Literal[False]"
reveal_type(z) # N: Revealed type is "Literal[False]"
[builtins fixtures/tuple.pyi]
[case testWalrusExpr]
# flags: --python-version 3.8
def func() -> None:
foo = Foo()
if x := foo.x:
pass
class Foo:
def __init__(self) -> None:
self.x = 123
[case testWalrusTypeGuard]
# flags: --python-version 3.8
from typing_extensions import TypeGuard
def is_float(a: object) -> TypeGuard[float]: pass
def main(a: object) -> None:
if is_float(x := a):
reveal_type(x) # N: Revealed type is "builtins.float"
reveal_type(a) # N: Revealed type is "builtins.object"
[builtins fixtures/tuple.pyi]
[case testWalrusRedefined]
# flags: --python-version 3.8
def foo() -> None:
x = 0
[x := x + y for y in [1, 2, 3]]
[builtins fixtures/dict.pyi]
[case testOverloadWithPositionalOnlySelf]
# flags: --python-version 3.8
from typing import overload, Optional
class Foo:
@overload
def f(self, a: str, /) -> None: ...
@overload
def f(self, *, b: bool = False) -> None: ...
def f(self, a: Optional[str] = None, /, *, b: bool = False) -> None: # E: Overloaded function implementation does not accept all possible arguments of signature 2
...
class Bar:
@overload
def f(self, a: str, /) -> None: ...
@overload # Notice `/` in sig below:
def f(self, /, *, b: bool = False) -> None: ...
def f(self, a: Optional[str] = None, /, *, b: bool = False) -> None:
...
[builtins fixtures/bool.pyi]
[case testUnpackWithDuplicateNamePositionalOnly]
# flags: --python-version 3.8
from typing_extensions import Unpack, TypedDict
class Person(TypedDict):
name: str
age: int
def foo(name: str, /, **kwargs: Unpack[Person]) -> None: # Allowed
...
[builtins fixtures/dict.pyi]