Debian Codegraph @

test-data/unit/semanal-types.test

raw
1 [case testVarWithType]
2 import typing
3 class A: pass
4 x = A() # type: A
5 y = x
6 [out]
7 MypyFile:1(
8 Import:1(typing)
9 ClassDef:2(
10 A
11 PassStmt:2())
12 AssignmentStmt:3(
13 NameExpr(x [__main__.x])
14 CallExpr:3(
15 NameExpr(A [__main__.A])
16 Args())
17 __main__.A)
18 AssignmentStmt:4(
19 NameExpr(y* [__main__.y])
20 NameExpr(x [__main__.x])))
21
22 [case testLocalVarWithType]
23 class A: pass
24 def f():
25 x = None # type: A
26 y = x
27 [out]
28 MypyFile:1(
29 ClassDef:1(
30 A
31 PassStmt:1())
32 FuncDef:2(
33 f
34 Block:3(
35 AssignmentStmt:3(
36 NameExpr(x [l])
37 NameExpr(None [builtins.None])
38 __main__.A)
39 AssignmentStmt:4(
40 NameExpr(y* [l])
41 NameExpr(x [l])))))
42
43 [case testAnyType]
44 from typing import Any
45 x = None # type: Any
46 y = x
47 [out]
48 MypyFile:1(
49 ImportFrom:1(typing, [Any])
50 AssignmentStmt:2(
51 NameExpr(x [__main__.x])
52 NameExpr(None [builtins.None])
53 Any)
54 AssignmentStmt:3(
55 NameExpr(y* [__main__.y])
56 NameExpr(x [__main__.x])))
57
58 [case testMemberVarWithType]
59 import typing
60 class A:
61 def __init__(self):
62 self.x = None # type: int
63 [out]
64 MypyFile:1(
65 Import:1(typing)
66 ClassDef:2(
67 A
68 FuncDef:3(
69 __init__
70 Args(
71 Var(self))
72 Block:4(
73 AssignmentStmt:4(
74 MemberExpr:4(
75 NameExpr(self [l])
76 x)
77 NameExpr(None [builtins.None])
78 builtins.int)))))
79
80 [case testClassVarWithType]
81 import typing
82 class A:
83 x = None # type: int
84 x = 1
85 [out]
86 MypyFile:1(
87 Import:1(typing)
88 ClassDef:2(
89 A
90 AssignmentStmt:3(
91 NameExpr(x [m])
92 NameExpr(None [builtins.None])
93 builtins.int)
94 AssignmentStmt:4(
95 NameExpr(x [__main__.A.x])
96 IntExpr(1))))
97
98 [case testFunctionSig]
99 from typing import Any
100 class A: pass
101 def f(x: A) -> A: pass
102 def g(x: Any, y: A) -> None:
103 z = x, y
104 [out]
105 MypyFile:1(
106 ImportFrom:1(typing, [Any])
107 ClassDef:2(
108 A
109 PassStmt:2())
110 FuncDef:3(
111 f
112 Args(
113 Var(x))
114 def (x: __main__.A) -> __main__.A
115 Block:3(
116 PassStmt:3()))
117 FuncDef:4(
118 g
119 Args(
120 Var(x)
121 Var(y))
122 def (x: Any, y: __main__.A)
123 Block:5(
124 AssignmentStmt:5(
125 NameExpr(z* [l])
126 TupleExpr:5(
127 NameExpr(x [l])
128 NameExpr(y [l]))))))
129
130 [case testBaseclass]
131 class A: pass
132 class B(A): pass
133 [out]
134 MypyFile:1(
135 ClassDef:1(
136 A
137 PassStmt:1())
138 ClassDef:2(
139 B
140 BaseType(
141 __main__.A)
142 PassStmt:2()))
143
144 [case testMultipleVarDef]
145
146 class A: pass
147 class B: pass
148 a, b = None, None # type: (A, B)
149 x = a, b
150 [builtins fixtures/tuple.pyi]
151 [out]
152 MypyFile:1(
153 ClassDef:2(
154 A
155 PassStmt:2())
156 ClassDef:3(
157 B
158 PassStmt:3())
159 AssignmentStmt:4(
160 TupleExpr:4(
161 NameExpr(a [__main__.a])
162 NameExpr(b [__main__.b]))
163 TupleExpr:4(
164 NameExpr(None [builtins.None])
165 NameExpr(None [builtins.None]))
166 tuple[__main__.A, __main__.B])
167 AssignmentStmt:5(
168 NameExpr(x* [__main__.x])
169 TupleExpr:5(
170 NameExpr(a [__main__.a])
171 NameExpr(b [__main__.b]))))
172
173 [case testGenericType]
174 from typing import TypeVar, Generic, Any
175
176 t = TypeVar('t')
177
178 class A(Generic[t]): pass
179 class B: pass
180 x = None # type: A[B]
181 y = None # type: A[Any]
182 [out]
183 MypyFile:1(
184 ImportFrom:1(typing, [TypeVar, Generic, Any])
185 AssignmentStmt:3(
186 NameExpr(t* [__main__.t])
187 TypeVarExpr:3())
188 ClassDef:5(
189 A
190 TypeVars(
191 t`1)
192 PassStmt:5())
193 ClassDef:6(
194 B
195 PassStmt:6())
196 AssignmentStmt:7(
197 NameExpr(x [__main__.x])
198 NameExpr(None [builtins.None])
199 __main__.A[__main__.B])
200 AssignmentStmt:8(
201 NameExpr(y [__main__.y])
202 NameExpr(None [builtins.None])
203 __main__.A[Any]))
204
205 [case testGenericType2]
206 from typing import TypeVar, Generic, Any
207 t = TypeVar('t')
208 s = TypeVar('s')
209 class A(Generic[t, s]): pass
210 class B: pass
211 x = None # type: A[B, Any]
212 [out]
213 MypyFile:1(
214 ImportFrom:1(typing, [TypeVar, Generic, Any])
215 AssignmentStmt:2(
216 NameExpr(t* [__main__.t])
217 TypeVarExpr:2())
218 AssignmentStmt:3(
219 NameExpr(s* [__main__.s])
220 TypeVarExpr:3())
221 ClassDef:4(
222 A
223 TypeVars(
224 t`1
225 s`2)
226 PassStmt:4())
227 ClassDef:5(
228 B
229 PassStmt:5())
230 AssignmentStmt:6(
231 NameExpr(x [__main__.x])
232 NameExpr(None [builtins.None])
233 __main__.A[__main__.B, Any]))
234
235 [case testAssignmentAfterDef]
236
237
238 class A: pass
239 a = None # type: A
240 a = 1
241 def f():
242 b = None # type: A
243 b = 1
244 [out]
245 MypyFile:1(
246 ClassDef:3(
247 A
248 PassStmt:3())
249 AssignmentStmt:4(
250 NameExpr(a [__main__.a])
251 NameExpr(None [builtins.None])
252 __main__.A)
253 AssignmentStmt:5(
254 NameExpr(a [__main__.a])
255 IntExpr(1))
256 FuncDef:6(
257 f
258 Block:7(
259 AssignmentStmt:7(
260 NameExpr(b [l])
261 NameExpr(None [builtins.None])
262 __main__.A)
263 AssignmentStmt:8(
264 NameExpr(b [l])
265 IntExpr(1)))))
266
267 [case testCast]
268 from typing import TypeVar, Generic, Any, cast
269 t = TypeVar('t')
270 class c: pass
271 class d(Generic[t]): pass
272 cast(Any, 1)
273 cast(c, 1)
274 cast(d[c], c)
275 [out]
276 MypyFile:1(
277 ImportFrom:1(typing, [TypeVar, Generic, Any, cast])
278 AssignmentStmt:2(
279 NameExpr(t* [__main__.t])
280 TypeVarExpr:2())
281 ClassDef:3(
282 c
283 PassStmt:3())
284 ClassDef:4(
285 d
286 TypeVars(
287 t`1)
288 PassStmt:4())
289 ExpressionStmt:5(
290 CastExpr:5(
291 IntExpr(1)
292 Any))
293 ExpressionStmt:6(
294 CastExpr:6(
295 IntExpr(1)
296 __main__.c))
297 ExpressionStmt:7(
298 CastExpr:7(
299 NameExpr(c [__main__.c])
300 __main__.d[__main__.c])))
301
302 [case testCastToQualifiedTypeAndCast]
303 import typing
304 import _m
305 typing.cast(_m.C, object)
306 [fixture _m.py]
307 class C: pass
308 [out]
309 MypyFile:1(
310 Import:1(typing)
311 Import:2(_m)
312 ExpressionStmt:3(
313 CastExpr:3(
314 NameExpr(object [builtins.object])
315 _m.C)))
316
317 [case testLongQualifiedCast]
318 import typing
319 import _m._n
320 typing.cast(_m._n.C, object)
321 [fixture _m/__init__.py]
322 [fixture _m/_n.py]
323 class C: pass
324 [out]
325 MypyFile:1(
326 Import:1(typing)
327 Import:2(_m._n)
328 ExpressionStmt:3(
329 CastExpr:3(
330 NameExpr(object [builtins.object])
331 _m._n.C)))
332
333 [case testCastTargetWithTwoTypeArgs]
334 from typing import TypeVar, Generic, cast
335 t = TypeVar('t')
336 s = TypeVar('s')
337 class C(Generic[t, s]): pass
338 cast(C[str, int], C)
339 [out]
340 MypyFile:1(
341 ImportFrom:1(typing, [TypeVar, Generic, cast])
342 AssignmentStmt:2(
343 NameExpr(t* [__main__.t])
344 TypeVarExpr:2())
345 AssignmentStmt:3(
346 NameExpr(s* [__main__.s])
347 TypeVarExpr:3())
348 ClassDef:4(
349 C
350 TypeVars(
351 t`1
352 s`2)
353 PassStmt:4())
354 ExpressionStmt:5(
355 CastExpr:5(
356 NameExpr(C [__main__.C])
357 __main__.C[builtins.str, builtins.int])))
358
359 [case testCastToTupleType]
360 from typing import Tuple, cast
361 cast(Tuple[int, str], None)
362 [builtins fixtures/tuple.pyi]
363 [out]
364 MypyFile:1(
365 ImportFrom:1(typing, [Tuple, cast])
366 ExpressionStmt:2(
367 CastExpr:2(
368 NameExpr(None [builtins.None])
369 tuple[builtins.int, builtins.str])))
370
371 [case testCastToFunctionType]
372 from typing import Callable, cast
373 cast(Callable[[int], str], None)
374 [out]
375 MypyFile:1(
376 ImportFrom:1(typing, [Callable, cast])
377 ExpressionStmt:2(
378 CastExpr:2(
379 NameExpr(None [builtins.None])
380 def (builtins.int) -> builtins.str)))
381
382 [case testCastToStringLiteralType]
383 from typing import cast
384 cast('int', 1)
385 [out]
386 MypyFile:1(
387 ImportFrom:1(typing, [cast])
388 ExpressionStmt:2(
389 CastExpr:2(
390 IntExpr(1)
391 builtins.int)))
392
393 [case testAssertType]
394 from typing import assert_type
395 assert_type(1, int)
396 [out]
397 MypyFile:1(
398 ImportFrom:1(typing, [assert_type])
399 ExpressionStmt:2(
400 AssertTypeExpr:2(
401 IntExpr(1)
402 builtins.int)))
403
404 [case testFunctionTypeVariable]
405 from typing import TypeVar
406 t = TypeVar('t')
407 def f(x: t) -> None:
408 y = None # type: t
409 [out]
410 MypyFile:1(
411 ImportFrom:1(typing, [TypeVar])
412 AssignmentStmt:2(
413 NameExpr(t* [__main__.t])
414 TypeVarExpr:2())
415 FuncDef:3(
416 f
417 Args(
418 Var(x))
419 def [t] (x: t`-1)
420 Block:4(
421 AssignmentStmt:4(
422 NameExpr(y [l])
423 NameExpr(None [builtins.None])
424 t`-1))))
425
426 [case testTwoFunctionTypeVariables]
427 from typing import TypeVar
428 t = TypeVar('t')
429 u = TypeVar('u')
430 def f(x: t, y: u, z: t) -> None: pass
431 [out]
432 MypyFile:1(
433 ImportFrom:1(typing, [TypeVar])
434 AssignmentStmt:2(
435 NameExpr(t* [__main__.t])
436 TypeVarExpr:2())
437 AssignmentStmt:3(
438 NameExpr(u* [__main__.u])
439 TypeVarExpr:3())
440 FuncDef:4(
441 f
442 Args(
443 Var(x)
444 Var(y)
445 Var(z))
446 def [t, u] (x: t`-1, y: u`-2, z: t`-1)
447 Block:4(
448 PassStmt:4())))
449
450 [case testNestedGenericFunctionTypeVariable]
451 from typing import TypeVar, Generic
452 t = TypeVar('t')
453 class A(Generic[t]): pass
454 def f(x: A[t], y) -> None: pass
455 [out]
456 MypyFile:1(
457 ImportFrom:1(typing, [TypeVar, Generic])
458 AssignmentStmt:2(
459 NameExpr(t* [__main__.t])
460 TypeVarExpr:2())
461 ClassDef:3(
462 A
463 TypeVars(
464 t`1)
465 PassStmt:3())
466 FuncDef:4(
467 f
468 Args(
469 Var(x)
470 Var(y))
471 def [t] (x: __main__.A[t`-1], y: Any)
472 Block:4(
473 PassStmt:4())))
474
475 [case testNestedGenericFunctionTypeVariable2]
476 from typing import TypeVar, Tuple, Generic
477 t = TypeVar('t')
478 class A(Generic[t]): pass
479 def f(x: Tuple[int, t]) -> None: pass
480 [builtins fixtures/tuple.pyi]
481 [out]
482 MypyFile:1(
483 ImportFrom:1(typing, [TypeVar, Tuple, Generic])
484 AssignmentStmt:2(
485 NameExpr(t* [__main__.t])
486 TypeVarExpr:2())
487 ClassDef:3(
488 A
489 TypeVars(
490 t`1)
491 PassStmt:3())
492 FuncDef:4(
493 f
494 Args(
495 Var(x))
496 def [t] (x: tuple[builtins.int, t`-1])
497 Block:4(
498 PassStmt:4())))
499
500 [case testNestedGenericFunctionTypeVariable3]
501 from typing import TypeVar, Callable, Generic
502 t = TypeVar('t')
503 class A(Generic[t]): pass
504 def f(x: Callable[[int, t], int]) -> None: pass
505 [out]
506 MypyFile:1(
507 ImportFrom:1(typing, [TypeVar, Callable, Generic])
508 AssignmentStmt:2(
509 NameExpr(t* [__main__.t])
510 TypeVarExpr:2())
511 ClassDef:3(
512 A
513 TypeVars(
514 t`1)
515 PassStmt:3())
516 FuncDef:4(
517 f
518 Args(
519 Var(x))
520 def [t] (x: def (builtins.int, t`-1) -> builtins.int)
521 Block:4(
522 PassStmt:4())))
523
524 [case testNestedGenericFunctionTypeVariable4]
525 from typing import TypeVar, Callable, Generic
526 t = TypeVar('t')
527 class A(Generic[t]): pass
528 def f(x: Callable[[], t]) -> None: pass
529 [out]
530 MypyFile:1(
531 ImportFrom:1(typing, [TypeVar, Callable, Generic])
532 AssignmentStmt:2(
533 NameExpr(t* [__main__.t])
534 TypeVarExpr:2())
535 ClassDef:3(
536 A
537 TypeVars(
538 t`1)
539 PassStmt:3())
540 FuncDef:4(
541 f
542 Args(
543 Var(x))
544 def [t] (x: def () -> t`-1)
545 Block:4(
546 PassStmt:4())))
547
548 [case testGenericFunctionTypeVariableInReturnType]
549 from typing import TypeVar
550 t = TypeVar('t')
551 def f() -> t: pass
552 [out]
553 MypyFile:1(
554 ImportFrom:1(typing, [TypeVar])
555 AssignmentStmt:2(
556 NameExpr(t* [__main__.t])
557 TypeVarExpr:2())
558 FuncDef:3(
559 f
560 def [t] () -> t`-1
561 Block:3(
562 PassStmt:3())))
563
564 [case testSelfType]
565 class A:
566 def f(self, o: object) -> None: pass
567 [out]
568 MypyFile:1(
569 ClassDef:1(
570 A
571 FuncDef:2(
572 f
573 Args(
574 Var(self)
575 Var(o))
576 def (self: __main__.A, o: builtins.object)
577 Block:2(
578 PassStmt:2()))))
579
580 [case testNestedGenericFunction]
581 from typing import TypeVar
582 t = TypeVar('t')
583 def f() -> None:
584 def g() -> t: pass
585 [out]
586 MypyFile:1(
587 ImportFrom:1(typing, [TypeVar])
588 AssignmentStmt:2(
589 NameExpr(t* [__main__.t])
590 TypeVarExpr:2())
591 FuncDef:3(
592 f
593 def ()
594 Block:4(
595 FuncDef:4(
596 g
597 def [t] () -> t`-1
598 Block:4(
599 PassStmt:4())))))
600
601 [case testClassTvar]
602 from typing import TypeVar, Generic
603
604 t = TypeVar('t')
605
606 class c(Generic[t]):
607 def f(self) -> t: pass
608 [out]
609 MypyFile:1(
610 ImportFrom:1(typing, [TypeVar, Generic])
611 AssignmentStmt:3(
612 NameExpr(t* [__main__.t])
613 TypeVarExpr:3())
614 ClassDef:5(
615 c
616 TypeVars(
617 t`1)
618 FuncDef:6(
619 f
620 Args(
621 Var(self))
622 def (self: __main__.c[t`1]) -> t`1
623 Block:6(
624 PassStmt:6()))))
625
626 [case testClassTvar2]
627 from typing import TypeVar, Generic
628
629 t = TypeVar('t')
630 s = TypeVar('s')
631
632 class c(Generic[t, s]):
633 def f(self, x: s) -> t: pass
634 [out]
635 MypyFile:1(
636 ImportFrom:1(typing, [TypeVar, Generic])
637 AssignmentStmt:3(
638 NameExpr(t* [__main__.t])
639 TypeVarExpr:3())
640 AssignmentStmt:4(
641 NameExpr(s* [__main__.s])
642 TypeVarExpr:4())
643 ClassDef:6(
644 c
645 TypeVars(
646 t`1
647 s`2)
648 FuncDef:7(
649 f
650 Args(
651 Var(self)
652 Var(x))
653 def (self: __main__.c[t`1, s`2], x: s`2) -> t`1
654 Block:7(
655 PassStmt:7()))))
656
657 [case testGenericBaseClass]
658 from typing import TypeVar, Generic
659 t = TypeVar('t')
660 class d(Generic[t]): pass
661 class c(d[t], Generic[t]): pass
662 [out]
663 MypyFile:1(
664 ImportFrom:1(typing, [TypeVar, Generic])
665 AssignmentStmt:2(
666 NameExpr(t* [__main__.t])
667 TypeVarExpr:2())
668 ClassDef:3(
669 d
670 TypeVars(
671 t`1)
672 PassStmt:3())
673 ClassDef:4(
674 c
675 TypeVars(
676 t`1)
677 BaseType(
678 __main__.d[t`1])
679 PassStmt:4()))
680
681 [case testTupleType]
682 from typing import Tuple
683 t = None # type: tuple
684 t1 = None # type: Tuple[object]
685 t2 = None # type: Tuple[int, object]
686 [builtins fixtures/tuple.pyi]
687 [out]
688 MypyFile:1(
689 ImportFrom:1(typing, [Tuple])
690 AssignmentStmt:2(
691 NameExpr(t [__main__.t])
692 NameExpr(None [builtins.None])
693 builtins.tuple[Any, ...])
694 AssignmentStmt:3(
695 NameExpr(t1 [__main__.t1])
696 NameExpr(None [builtins.None])
697 tuple[builtins.object])
698 AssignmentStmt:4(
699 NameExpr(t2 [__main__.t2])
700 NameExpr(None [builtins.None])
701 tuple[builtins.int, builtins.object]))
702
703 [case testVariableLengthTuple]
704 from typing import Tuple
705 t = None # type: Tuple[int, ...]
706 [builtins fixtures/tuple.pyi]
707 [out]
708 MypyFile:1(
709 ImportFrom:1(typing, [Tuple])
710 AssignmentStmt:2(
711 NameExpr(t [__main__.t])
712 NameExpr(None [builtins.None])
713 builtins.tuple[builtins.int, ...]))
714
715 [case testInvalidTupleType]
716 from typing import Tuple
717 t = None # type: Tuple[int, str, ...] # E: Unexpected "..."
718 [builtins fixtures/tuple.pyi]
719 [out]
720
721 [case testFunctionTypes]
722 from typing import Callable
723 f = None # type: Callable[[object, int], str]
724 g = None # type: Callable[[], None]
725 [out]
726 MypyFile:1(
727 ImportFrom:1(typing, [Callable])
728 AssignmentStmt:2(
729 NameExpr(f [__main__.f])
730 NameExpr(None [builtins.None])
731 def (builtins.object, builtins.int) -> builtins.str)
732 AssignmentStmt:3(
733 NameExpr(g [__main__.g])
734 NameExpr(None [builtins.None])
735 def ()))
736
737 [case testOverloadedFunction]
738 from typing import overload, Any
739 @overload
740 def f(a: object) -> int: a
741 @overload
742 def f(a: str) -> object: a
743
744 def f(a: Any) -> Any: return a
745
746 [out]
747 MypyFile:1(
748 ImportFrom:1(typing, [overload, Any])
749 OverloadedFuncDef:2(
750 FuncDef:7(
751 f
752 Args(
753 Var(a))
754 def (a: Any) -> Any
755 Block:7(
756 ReturnStmt:7(
757 NameExpr(a [l]))))
758 Overload(def (a: builtins.object) -> builtins.int, \
759 def (a: builtins.str) -> builtins.object)
760 Decorator:2(
761 Var(f)
762 NameExpr(overload [typing.overload])
763 FuncDef:3(
764 f
765 Args(
766 Var(a))
767 def (a: builtins.object) -> builtins.int
768 Block:3(
769 ExpressionStmt:3(
770 NameExpr(a [l])))))
771 Decorator:4(
772 Var(f)
773 NameExpr(overload [typing.overload])
774 FuncDef:5(
775 f
776 Args(
777 Var(a))
778 def (a: builtins.str) -> builtins.object
779 Block:5(
780 ExpressionStmt:5(
781 NameExpr(a [l])))))))
782
783 [case testReferenceToOverloadedFunction]
784 from typing import overload
785 @overload
786 def f() -> None: pass
787 @overload
788 def f(x: int) -> None: pass
789
790 def f(*args) -> None: pass
791
792 x = f
793 [builtins fixtures/tuple.pyi]
794 [out]
795 MypyFile:1(
796 ImportFrom:1(typing, [overload])
797 OverloadedFuncDef:2(
798 FuncDef:7(
799 f
800 def (*args: Any)
801 VarArg(
802 Var(args))
803 Block:7(
804 PassStmt:7()))
805 Overload(def (), def (x: builtins.int))
806 Decorator:2(
807 Var(f)
808 NameExpr(overload [typing.overload])
809 FuncDef:3(
810 f
811 def ()
812 Block:3(
813 PassStmt:3())))
814 Decorator:4(
815 Var(f)
816 NameExpr(overload [typing.overload])
817 FuncDef:5(
818 f
819 Args(
820 Var(x))
821 def (x: builtins.int)
822 Block:5(
823 PassStmt:5()))))
824 AssignmentStmt:9(
825 NameExpr(x* [__main__.x])
826 NameExpr(f [__main__.f])))
827
828 [case testNestedOverloadedFunction]
829 from typing import overload
830 def f():
831 @overload
832 def g(): pass
833 @overload
834 def g(x): pass
835
836 def g(*args): pass
837
838 y = g
839 [out]
840 MypyFile:1(
841 ImportFrom:1(typing, [overload])
842 FuncDef:2(
843 f
844 Block:3(
845 OverloadedFuncDef:3(
846 FuncDef:8(
847 g
848 VarArg(
849 Var(args))
850 Block:8(
851 PassStmt:8()))
852 Overload(def () -> Any, def (x: Any) -> Any)
853 Decorator:3(
854 Var(g)
855 NameExpr(overload [typing.overload])
856 FuncDef:4(
857 g
858 Block:4(
859 PassStmt:4())))
860 Decorator:5(
861 Var(g)
862 NameExpr(overload [typing.overload])
863 FuncDef:6(
864 g
865 Args(
866 Var(x))
867 Block:6(
868 PassStmt:6()))))
869 AssignmentStmt:10(
870 NameExpr(y* [l])
871 NameExpr(g [l])))))
872
873 [case testImplicitGenericTypeArgs]
874 from typing import TypeVar, Generic
875 t = TypeVar('t')
876 s = TypeVar('s')
877 class A(Generic[t, s]): pass
878 x = None # type: A
879 [out]
880 MypyFile:1(
881 ImportFrom:1(typing, [TypeVar, Generic])
882 AssignmentStmt:2(
883 NameExpr(t* [__main__.t])
884 TypeVarExpr:2())
885 AssignmentStmt:3(
886 NameExpr(s* [__main__.s])
887 TypeVarExpr:3())
888 ClassDef:4(
889 A
890 TypeVars(
891 t`1
892 s`2)
893 PassStmt:4())
894 AssignmentStmt:5(
895 NameExpr(x [__main__.x])
896 NameExpr(None [builtins.None])
897 __main__.A[Any, Any]))
898
899 [case testImplicitTypeArgsAndGenericBaseClass]
900 from typing import TypeVar, Generic
901 t = TypeVar('t')
902 s = TypeVar('s')
903 class B(Generic[s]): pass
904 class A(B, Generic[t]): pass
905 [out]
906 MypyFile:1(
907 ImportFrom:1(typing, [TypeVar, Generic])
908 AssignmentStmt:2(
909 NameExpr(t* [__main__.t])
910 TypeVarExpr:2())
911 AssignmentStmt:3(
912 NameExpr(s* [__main__.s])
913 TypeVarExpr:3())
914 ClassDef:4(
915 B
916 TypeVars(
917 s`1)
918 PassStmt:4())
919 ClassDef:5(
920 A
921 TypeVars(
922 t`1)
923 BaseType(
924 __main__.B[Any])
925 PassStmt:5()))
926
927 [case testTypeApplication]
928 from typing import TypeVar, Generic
929 t = TypeVar('t')
930 class A(Generic[t]): pass
931 x = A[int]()
932 [out]
933 MypyFile:1(
934 ImportFrom:1(typing, [TypeVar, Generic])
935 AssignmentStmt:2(
936 NameExpr(t* [__main__.t])
937 TypeVarExpr:2())
938 ClassDef:3(
939 A
940 TypeVars(
941 t`1)
942 PassStmt:3())
943 AssignmentStmt:4(
944 NameExpr(x* [__main__.x])
945 CallExpr:4(
946 TypeApplication:4(
947 NameExpr(A [__main__.A])
948 Types(
949 builtins.int))
950 Args())))
951
952 [case testTypeApplicationWithTwoTypeArgs]
953 from typing import TypeVar, Generic, Any
954 t = TypeVar('t')
955 s = TypeVar('s')
956 class A(Generic[t, s]): pass
957 x = A[int, Any]()
958 [out]
959 MypyFile:1(
960 ImportFrom:1(typing, [TypeVar, Generic, Any])
961 AssignmentStmt:2(
962 NameExpr(t* [__main__.t])
963 TypeVarExpr:2())
964 AssignmentStmt:3(
965 NameExpr(s* [__main__.s])
966 TypeVarExpr:3())
967 ClassDef:4(
968 A
969 TypeVars(
970 t`1
971 s`2)
972 PassStmt:4())
973 AssignmentStmt:5(
974 NameExpr(x* [__main__.x])
975 CallExpr:5(
976 TypeApplication:5(
977 NameExpr(A [__main__.A])
978 Types(
979 builtins.int
980 Any))
981 Args())))
982
983 [case testFunctionTypeApplication]
984 from typing import TypeVar
985 t = TypeVar('t')
986 def f(x: t) -> None: pass
987 f[int](1)
988 [out]
989 MypyFile:1(
990 ImportFrom:1(typing, [TypeVar])
991 AssignmentStmt:2(
992 NameExpr(t* [__main__.t])
993 TypeVarExpr:2())
994 FuncDef:3(
995 f
996 Args(
997 Var(x))
998 def [t] (x: t`-1)
999 Block:3(
1000 PassStmt:3()))
1001 ExpressionStmt:4(
1002 CallExpr:4(
1003 TypeApplication:4(
1004 NameExpr(f [__main__.f])
1005 Types(
1006 builtins.int))
1007 Args(
1008 IntExpr(1)))))
1009
1010 [case testTypeApplicationWithStringLiteralType]
1011 from typing import TypeVar, Generic
1012 t = TypeVar('t')
1013 class A(Generic[t]): pass
1014 A['int']()
1015 [out]
1016 MypyFile:1(
1017 ImportFrom:1(typing, [TypeVar, Generic])
1018 AssignmentStmt:2(
1019 NameExpr(t* [__main__.t])
1020 TypeVarExpr:2())
1021 ClassDef:3(
1022 A
1023 TypeVars(
1024 t`1)
1025 PassStmt:3())
1026 ExpressionStmt:4(
1027 CallExpr:4(
1028 TypeApplication:4(
1029 NameExpr(A [__main__.A])
1030 Types(
1031 builtins.int))
1032 Args())))
1033
1034 [case testVarArgsAndKeywordArgs]
1035 def g(*x: int, y: str = ''): pass
1036 [builtins fixtures/tuple.pyi]
1037 [out]
1038 MypyFile:1(
1039 FuncDef:1(
1040 g
1041 MaxPos(0)
1042 Args(
1043 default(
1044 Var(y)
1045 StrExpr()))
1046 def (*x: builtins.int, y: builtins.str =) -> Any
1047 VarArg(
1048 Var(x))
1049 Block:1(
1050 PassStmt:1())))
1051
1052 [case testQualifiedGeneric]
1053 from typing import TypeVar
1054 import typing
1055 T = TypeVar('T')
1056 class A(typing.Generic[T]): pass
1057 [out]
1058 MypyFile:1(
1059 ImportFrom:1(typing, [TypeVar])
1060 Import:2(typing)
1061 AssignmentStmt:3(
1062 NameExpr(T* [__main__.T])
1063 TypeVarExpr:3())
1064 ClassDef:4(
1065 A
1066 TypeVars(
1067 T`1)
1068 PassStmt:4()))
1069
1070 [case testQualifiedTypevar]
1071 import typing
1072 T = typing.TypeVar('T')
1073 def f(x: T) -> T: pass
1074 [out]
1075 MypyFile:1(
1076 Import:1(typing)
1077 AssignmentStmt:2(
1078 NameExpr(T* [__main__.T])
1079 TypeVarExpr:2())
1080 FuncDef:3(
1081 f
1082 Args(
1083 Var(x))
1084 def [T] (x: T`-1) -> T`-1
1085 Block:3(
1086 PassStmt:3())))
1087
1088 [case testAliasedTypevar]
1089 from typing import TypeVar as tv
1090 T = tv('T')
1091 def f(x: T) -> T: pass
1092 [out]
1093 MypyFile:1(
1094 ImportFrom:1(typing, [TypeVar : tv])
1095 AssignmentStmt:2(
1096 NameExpr(T* [__main__.T])
1097 TypeVarExpr:2())
1098 FuncDef:3(
1099 f
1100 Args(
1101 Var(x))
1102 def [T] (x: T`-1) -> T`-1
1103 Block:3(
1104 PassStmt:3())))
1105
1106 [case testLocalTypevar]
1107 from typing import TypeVar
1108 def f():
1109 T = TypeVar('T')
1110 def g(x: T) -> T: pass
1111 [out]
1112 MypyFile:1(
1113 ImportFrom:1(typing, [TypeVar])
1114 FuncDef:2(
1115 f
1116 Block:3(
1117 AssignmentStmt:3(
1118 NameExpr(T* [l])
1119 TypeVarExpr:3())
1120 FuncDef:4(
1121 g
1122 Args(
1123 Var(x))
1124 def [T] (x: T`-1) -> T`-1
1125 Block:4(
1126 PassStmt:4())))))
1127
1128 [case testClassLevelTypevar]
1129 from typing import TypeVar
1130 class A:
1131 T = TypeVar('T')
1132 def g(self, x: T) -> T: pass
1133 [out]
1134 MypyFile:1(
1135 ImportFrom:1(typing, [TypeVar])
1136 ClassDef:2(
1137 A
1138 AssignmentStmt:3(
1139 NameExpr(T* [m])
1140 TypeVarExpr:3())
1141 FuncDef:4(
1142 g
1143 Args(
1144 Var(self)
1145 Var(x))
1146 def [T] (self: __main__.A, x: T`-1) -> T`-1
1147 Block:4(
1148 PassStmt:4()))))
1149
1150 [case testImportTypevar]
1151 from typing import Generic
1152 from _m import T
1153 class A(Generic[T]):
1154 y = None # type: T
1155 [fixture _m.py]
1156 from typing import TypeVar
1157 T = TypeVar('T')
1158 [out]
1159 MypyFile:1(
1160 ImportFrom:1(typing, [Generic])
1161 ImportFrom:2(_m, [T])
1162 ClassDef:3(
1163 A
1164 TypeVars(
1165 T`1)
1166 AssignmentStmt:4(
1167 NameExpr(y [m])
1168 NameExpr(None [builtins.None])
1169 T`1)))
1170
1171 [case testQualifiedReferenceToTypevarInClass]
1172 from typing import Generic
1173 import _m
1174 class A(Generic[_m.T]):
1175 a = None # type: _m.T
1176 def f(self, x: _m.T):
1177 b = None # type: _m.T
1178 [fixture _m.py]
1179 from typing import TypeVar
1180 T = TypeVar('T')
1181 [out]
1182 MypyFile:1(
1183 ImportFrom:1(typing, [Generic])
1184 Import:2(_m)
1185 ClassDef:3(
1186 A
1187 TypeVars(
1188 _m.T`1)
1189 AssignmentStmt:4(
1190 NameExpr(a [m])
1191 NameExpr(None [builtins.None])
1192 _m.T`1)
1193 FuncDef:5(
1194 f
1195 Args(
1196 Var(self)
1197 Var(x))
1198 def (self: __main__.A[_m.T`1], x: _m.T`1) -> Any
1199 Block:6(
1200 AssignmentStmt:6(
1201 NameExpr(b [l])
1202 NameExpr(None [builtins.None])
1203 _m.T`1)))))
1204
1205 [case testQualifiedReferenceToTypevarInFunctionSignature]
1206 import _m
1207 def f(x: _m.T) -> None:
1208 a = None # type: _m.T
1209 [fixture _m.py]
1210 from typing import TypeVar
1211 T = TypeVar('T')
1212 [out]
1213 MypyFile:1(
1214 Import:1(_m)
1215 FuncDef:2(
1216 f
1217 Args(
1218 Var(x))
1219 def [_m.T] (x: _m.T`-1)
1220 Block:3(
1221 AssignmentStmt:3(
1222 NameExpr(a [l])
1223 NameExpr(None [builtins.None])
1224 _m.T`-1))))
1225
1226 [case testFunctionCommentAnnotation]
1227 from typing import Any
1228 def f(x): # type: (int) -> Any
1229 x = 1
1230 [out]
1231 MypyFile:1(
1232 ImportFrom:1(typing, [Any])
1233 FuncDef:2(
1234 f
1235 Args(
1236 Var(x))
1237 def (x: builtins.int) -> Any
1238 Block:3(
1239 AssignmentStmt:3(
1240 NameExpr(x [l])
1241 IntExpr(1)))))
1242
1243 [case testMethodCommentAnnotation]
1244 import typing
1245 class A:
1246 def f(self, x): # type: (int) -> str
1247 x = 1
1248 [out]
1249 MypyFile:1(
1250 Import:1(typing)
1251 ClassDef:2(
1252 A
1253 FuncDef:3(
1254 f
1255 Args(
1256 Var(self)
1257 Var(x))
1258 def (self: __main__.A, x: builtins.int) -> builtins.str
1259 Block:4(
1260 AssignmentStmt:4(
1261 NameExpr(x [l])
1262 IntExpr(1))))))
1263
1264 [case testTypevarWithValues]
1265 from typing import TypeVar, Any
1266 T = TypeVar('T', int, str)
1267 S = TypeVar('S', Any, int, str)
1268 [out]
1269 MypyFile:1(
1270 ImportFrom:1(typing, [TypeVar, Any])
1271 AssignmentStmt:2(
1272 NameExpr(T* [__main__.T])
1273 TypeVarExpr:2(
1274 Values(
1275 builtins.int
1276 builtins.str)))
1277 AssignmentStmt:3(
1278 NameExpr(S* [__main__.S])
1279 TypeVarExpr:3(
1280 Values(
1281 Any
1282 builtins.int
1283 builtins.str))))
1284
1285 [case testTypevarWithValuesAndVariance]
1286 from typing import TypeVar
1287 T = TypeVar('T', int, str, covariant=True)
1288 [builtins fixtures/bool.pyi]
1289 [out]
1290 MypyFile:1(
1291 ImportFrom:1(typing, [TypeVar])
1292 AssignmentStmt:2(
1293 NameExpr(T* [__main__.T])
1294 TypeVarExpr:2(
1295 Variance(COVARIANT)
1296 Values(
1297 builtins.int
1298 builtins.str))))
1299
1300 [case testTypevarWithFalseVariance]
1301 from typing import TypeVar
1302 T1 = TypeVar('T1', covariant=False)
1303 T2 = TypeVar('T2', covariant=False, contravariant=False)
1304 T3 = TypeVar('T3', contravariant=False)
1305 T4 = TypeVar('T4', covariant=True, contravariant=False)
1306 T5 = TypeVar('T5', covariant=False, contravariant=True)
1307 [builtins fixtures/bool.pyi]
1308 [out]
1309 MypyFile:1(
1310 ImportFrom:1(typing, [TypeVar])
1311 AssignmentStmt:2(
1312 NameExpr(T1* [__main__.T1])
1313 TypeVarExpr:2())
1314 AssignmentStmt:3(
1315 NameExpr(T2* [__main__.T2])
1316 TypeVarExpr:3())
1317 AssignmentStmt:4(
1318 NameExpr(T3* [__main__.T3])
1319 TypeVarExpr:4())
1320 AssignmentStmt:5(
1321 NameExpr(T4* [__main__.T4])
1322 TypeVarExpr:5(
1323 Variance(COVARIANT)))
1324 AssignmentStmt:6(
1325 NameExpr(T5* [__main__.T5])
1326 TypeVarExpr:6(
1327 Variance(CONTRAVARIANT))))
1328
1329 [case testTypevarWithBound]
1330 from typing import TypeVar
1331 T = TypeVar('T', bound=int)
1332 [out]
1333 MypyFile:1(
1334 ImportFrom:1(typing, [TypeVar])
1335 AssignmentStmt:2(
1336 NameExpr(T* [__main__.T])
1337 TypeVarExpr:2(
1338 UpperBound(builtins.int))))
1339
1340 [case testGenericFunctionWithValueSet]
1341 from typing import TypeVar
1342 T = TypeVar('T', int, str)
1343 def f(x: T) -> T: pass
1344 [out]
1345 MypyFile:1(
1346 ImportFrom:1(typing, [TypeVar])
1347 AssignmentStmt:2(
1348 NameExpr(T* [__main__.T])
1349 TypeVarExpr:2(
1350 Values(
1351 builtins.int
1352 builtins.str)))
1353 FuncDef:3(
1354 f
1355 Args(
1356 Var(x))
1357 def [T in (builtins.int, builtins.str)] (x: T`-1) -> T`-1
1358 Block:3(
1359 PassStmt:3())))
1360
1361 [case testGenericClassWithValueSet]
1362 from typing import TypeVar, Generic
1363 T = TypeVar('T', int, str)
1364 class C(Generic[T]): pass
1365 [out]
1366 MypyFile:1(
1367 ImportFrom:1(typing, [TypeVar, Generic])
1368 AssignmentStmt:2(
1369 NameExpr(T* [__main__.T])
1370 TypeVarExpr:2(
1371 Values(
1372 builtins.int
1373 builtins.str)))
1374 ClassDef:3(
1375 C
1376 TypeVars(
1377 T`1)
1378 PassStmt:3()))
1379
1380 [case testGenericFunctionWithBound]
1381 from typing import TypeVar
1382 T = TypeVar('T', bound=int)
1383 def f(x: T) -> T: pass
1384 [out]
1385 MypyFile:1(
1386 ImportFrom:1(typing, [TypeVar])
1387 AssignmentStmt:2(
1388 NameExpr(T* [__main__.T])
1389 TypeVarExpr:2(
1390 UpperBound(builtins.int)))
1391 FuncDef:3(
1392 f
1393 Args(
1394 Var(x))
1395 def [T <: builtins.int] (x: T`-1) -> T`-1
1396 Block:3(
1397 PassStmt:3())))
1398
1399 [case testGenericClassWithBound]
1400 from typing import TypeVar, Generic
1401 T = TypeVar('T', bound=int)
1402 class C(Generic[T]): pass
1403 [out]
1404 MypyFile:1(
1405 ImportFrom:1(typing, [TypeVar, Generic])
1406 AssignmentStmt:2(
1407 NameExpr(T* [__main__.T])
1408 TypeVarExpr:2(
1409 UpperBound(builtins.int)))
1410 ClassDef:3(
1411 C
1412 TypeVars(
1413 T`1)
1414 PassStmt:3()))
1415
1416 [case testSimpleDucktypeDecorator]
1417 from typing import _promote
1418 @_promote(str)
1419 class S: pass
1420 [typing fixtures/typing-medium.pyi]
1421 [out]
1422 MypyFile:1(
1423 ImportFrom:1(typing, [_promote])
1424 ClassDef:3(
1425 S
1426 Promote([builtins.str])
1427 Decorators(
1428 PromoteExpr:2(builtins.str))
1429 PassStmt:3()))
1430
1431 [case testUnionType]
1432 from typing import Union
1433 def f(x: Union[int, str]) -> None: pass
1434 [out]
1435 MypyFile:1(
1436 ImportFrom:1(typing, [Union])
1437 FuncDef:2(
1438 f
1439 Args(
1440 Var(x))
1441 def (x: builtins.int | builtins.str)
1442 Block:2(
1443 PassStmt:2())))
1444
1445 [case testUnionTypeWithNoneItem]
1446 from typing import Union
1447 def f(x: Union[int, None]) -> None: pass
1448 [out]
1449 MypyFile:1(
1450 ImportFrom:1(typing, [Union])
1451 FuncDef:2(
1452 f
1453 Args(
1454 Var(x))
1455 def (x: builtins.int | None)
1456 Block:2(
1457 PassStmt:2())))
1458
1459 [case testUnionTypeWithNoneItemAndTwoItems]
1460 from typing import Union
1461 def f(x: Union[int, None, str]) -> None: pass
1462 [out]
1463 MypyFile:1(
1464 ImportFrom:1(typing, [Union])
1465 FuncDef:2(
1466 f
1467 Args(
1468 Var(x))
1469 def (x: builtins.int | None | builtins.str)
1470 Block:2(
1471 PassStmt:2())))
1472
1473 [case testUnionTypeWithSingleItem]
1474 from typing import Union
1475 def f(x: Union[int]) -> None: pass
1476 [out]
1477 MypyFile:1(
1478 ImportFrom:1(typing, [Union])
1479 FuncDef:2(
1480 f
1481 Args(
1482 Var(x))
1483 def (x: builtins.int)
1484 Block:2(
1485 PassStmt:2())))
1486
1487 [case testOptionalTypes]
1488 from typing import Optional
1489 x = 1 # type: Optional[int]
1490 [out]
1491 MypyFile:1(
1492 ImportFrom:1(typing, [Optional])
1493 AssignmentStmt:2(
1494 NameExpr(x [__main__.x])
1495 IntExpr(1)
1496 builtins.int | None))
1497
1498 [case testInvalidOptionalType]
1499 from typing import Optional
1500 x = 1 # type: Optional[int, str] # E: Optional[...] must have exactly one type argument
1501 y = 1 # type: Optional # E: Optional[...] must have exactly one type argument
1502 [out]
1503
1504 [case testCoAndContravariantTypeVar]
1505 from typing import TypeVar
1506 T = TypeVar('T', covariant=True)
1507 S = TypeVar('S', contravariant=True)
1508 [builtins fixtures/bool.pyi]
1509 [out]
1510 MypyFile:1(
1511 ImportFrom:1(typing, [TypeVar])
1512 AssignmentStmt:2(
1513 NameExpr(T* [__main__.T])
1514 TypeVarExpr:2(
1515 Variance(COVARIANT)))
1516 AssignmentStmt:3(
1517 NameExpr(S* [__main__.S])
1518 TypeVarExpr:3(
1519 Variance(CONTRAVARIANT))))
1520
1521 [case testTupleExpressionAsType]
1522 def f(x: (int, int)) -> None: pass
1523 [out]
1524 main:1: error: Syntax error in type annotation
1525 main:1: note: Suggestion: Use Tuple[T1, ..., Tn] instead of (T1, ..., Tn)
1526
1527 [case testQualifiedTypeNameBasedOnAny]
1528 from typing import Any
1529 x = 0 # type: Any
1530 z = 0 # type: x.y
1531 [out]
1532 MypyFile:1(
1533 ImportFrom:1(typing, [Any])
1534 AssignmentStmt:2(
1535 NameExpr(x [__main__.x])
1536 IntExpr(0)
1537 Any)
1538 AssignmentStmt:3(
1539 NameExpr(z [__main__.z])
1540 IntExpr(0)
1541 Any))
1542
1543
1544 [case testParamSpec]
1545 from typing import ParamSpec
1546 P = ParamSpec("P")
1547 [out]
1548 MypyFile:1(
1549 ImportFrom:1(typing, [ParamSpec])
1550 AssignmentStmt:2(
1551 NameExpr(P* [__main__.P])
1552 ParamSpecExpr:2()))
1553
1554 [case testTypeVarTuple]
1555 from typing_extensions import TypeVarTuple
1556 TV = TypeVarTuple("TV")
1557 [out]
1558 MypyFile:1(
1559 ImportFrom:1(typing_extensions, [TypeVarTuple])
1560 AssignmentStmt:2(
1561 NameExpr(TV* [__main__.TV])
1562 TypeVarTupleExpr:2(
1563 UpperBound(builtins.tuple[builtins.object, ...]))))
1564 [builtins fixtures/tuple.pyi]
1565
1566 [case testTypeVarTupleCallable]
1567 from typing_extensions import TypeVarTuple, Unpack
1568 from typing import Callable
1569 Ts = TypeVarTuple("Ts")
1570
1571 def foo(x: Callable[[Unpack[Ts]], None]) -> None:
1572 pass
1573 [out]
1574 MypyFile:1(
1575 ImportFrom:1(typing_extensions, [TypeVarTuple, Unpack])
1576 ImportFrom:2(typing, [Callable])
1577 AssignmentStmt:3(
1578 NameExpr(Ts* [__main__.Ts])
1579 TypeVarTupleExpr:3(
1580 UpperBound(builtins.tuple[builtins.object, ...])))
1581 FuncDef:5(
1582 foo
1583 Args(
1584 Var(x))
1585 def [Ts] (x: def (*Unpack[Ts`-1]))
1586 Block:6(
1587 PassStmt:6())))
1588
1589 [builtins fixtures/tuple.pyi]