Debian Codegraph @

test-data/unit/typexport-basic.test

raw
1 -- Test cases for exporting node types from the type checker.
2 --
3 -- Each test case consists of at least two sections.
4 -- The first section contains [case NAME] followed by the input code,
5 -- while the second section contains [out] followed by the output from the type
6 -- checker.
7 --
8 -- The first line of input code should be a regexp in comment that describes
9 -- the information to dump (prefix with ##). The regexp is matched against
10 -- the following items:
11 --
12 -- * each name of an expression node
13 -- * each type string of a node (e.g. OpExpr)
14 --
15 -- Lines starting with "--" in this file will be ignored.
16
17
18 -- Expressions
19 -- -----------
20
21
22 [case testConstructorCall]
23 import typing
24 class A: pass
25 class B: pass
26 A()
27 B()
28 [out]
29 CallExpr(4) : A
30 NameExpr(4) : def () -> A
31 CallExpr(5) : B
32 NameExpr(5) : def () -> B
33
34 [case testLiterals]
35 import typing
36 5
37 2.3
38 'foo'
39 [builtins fixtures/primitives.pyi]
40 [out]
41 IntExpr(2) : Literal[5]?
42 FloatExpr(3) : builtins.float
43 StrExpr(4) : Literal['foo']?
44
45 [case testNameExpression]
46
47 a = None # type: A
48 a # node
49 def f(aa: 'A') -> None:
50 b = None # type: B
51 aa # node
52 b # node
53 class A:
54 def g(self) -> None:
55 self # node
56 class B: pass
57 [out]
58 NameExpr(3) : A
59 NameExpr(6) : A
60 NameExpr(7) : B
61 NameExpr(10) : A
62
63 [case testEllipsis]
64 import typing
65 ...
66 [out]
67 EllipsisExpr(2) : builtins.ellipsis
68
69 [case testMemberAccess]
70 ## MemberExpr|CallExpr
71
72 a = None # type: A
73 a.m
74 a.f
75 a.f()
76 class A:
77 m = None # type: A
78 def f(self) -> 'B': pass
79 class B: pass
80 [out]
81 MemberExpr(4) : A
82 MemberExpr(5) : def () -> B
83 CallExpr(6) : B
84 MemberExpr(6) : def () -> B
85
86 [case testCastExpression]
87 ## CastExpr|[a-z]
88 from typing import Any, cast
89 d = None # type: Any
90 b = None # type: B
91 class A: pass
92 class B(A): pass
93 cast(A, d)
94 cast(A, b)
95 cast(B, b)
96 [out]
97 CastExpr(7) : A
98 NameExpr(7) : Any
99 CastExpr(8) : A
100 NameExpr(8) : B
101 CastExpr(9) : B
102 NameExpr(9) : B
103
104 [case testAssertTypeExpr]
105 ## AssertTypeExpr|[a-z]
106 from typing import Any, assert_type
107 d = None # type: Any
108 a = None # type: A
109 b = None # type: B
110 class A: pass
111 class B(A): pass
112 assert_type(d, Any)
113 assert_type(a, A)
114 assert_type(b, B)
115 [out]
116 AssertTypeExpr(8) : Any
117 NameExpr(8) : Any
118 AssertTypeExpr(9) : A
119 NameExpr(9) : A
120 AssertTypeExpr(10) : B
121 NameExpr(10) : B
122
123 [case testArithmeticOps]
124 ## OpExpr
125 import typing
126 a = 1 + 2
127 1.2 * 3
128 2.2 - 3
129 1 / 2
130 [file builtins.py]
131 class object:
132 def __init__(self) -> None: pass
133 class function: pass
134 class int:
135 def __add__(self, x: int) -> int: pass
136 def __truediv__(self, x: int) -> float: pass
137 class float:
138 def __mul__(self, x: int) -> float: pass
139 def __sub__(self, x: int) -> float: pass
140 class type: pass
141 class str: pass
142 class list: pass
143 class dict: pass
144 [out]
145 OpExpr(3) : builtins.int
146 OpExpr(4) : builtins.float
147 OpExpr(5) : builtins.float
148 OpExpr(6) : builtins.float
149
150 [case testComparisonOps]
151 ## ComparisonExpr
152 import typing
153 1 == object()
154 1 == 2
155 2 < 3
156 1 < 2 < 3
157 8 > 3
158 4 < 6 > 2
159 [file builtins.py]
160 class object:
161 def __init__(self) -> None: pass
162 class int:
163 def __eq__(self, x: object) -> bool: pass
164 def __lt__(self, x: int) -> bool: pass
165 def __gt__(self, x: int) -> int: pass
166 class bool: pass
167 class type: pass
168 class function: pass
169 class str: pass
170 class list: pass
171 class dict: pass
172 [out]
173 ComparisonExpr(3) : builtins.bool
174 ComparisonExpr(4) : builtins.bool
175 ComparisonExpr(5) : builtins.bool
176 ComparisonExpr(6) : builtins.bool
177 ComparisonExpr(7) : builtins.int
178 ComparisonExpr(8) : builtins.object
179
180 [case testBooleanOps]
181 ## OpExpr|UnaryExpr
182 import typing
183 a = 1
184 a and a
185 a or a
186 not a
187 [builtins fixtures/bool.pyi]
188 [out]
189 OpExpr(4) : builtins.int
190 OpExpr(5) : builtins.int
191 UnaryExpr(6) : builtins.bool
192
193 [case testBooleanOpsOnBools]
194 ## OpExpr|UnaryExpr
195 import typing
196 a = bool()
197 a and a
198 a or a
199 not a
200 [builtins fixtures/bool.pyi]
201 [out]
202 OpExpr(4) : builtins.bool
203 OpExpr(5) : builtins.bool
204 UnaryExpr(6) : builtins.bool
205
206 [case testFunctionCall]
207 ## CallExpr
208 from typing import Tuple
209 class A: pass
210 class B: pass
211 def f(a: A, b: B) -> Tuple[A, B]: pass
212 f(
213 A(),
214 B())
215 [builtins fixtures/tuple-simple.pyi]
216 [out]
217 CallExpr(6) : tuple[A, B]
218 CallExpr(7) : A
219 CallExpr(8) : B
220
221
222 -- Statements
223 -- ----------
224
225
226 [case testSimpleAssignment]
227 from typing import Any
228 a = None # type: A
229 b = a # type: Any
230 if b:
231 b = a
232 a = b
233
234 class A: pass
235 [out]
236 NameExpr(3) : A
237 NameExpr(4) : Any
238 NameExpr(5) : A
239 NameExpr(5) : Any
240 NameExpr(6) : A
241 NameExpr(6) : Any
242
243 [case testMemberAssignment]
244 from typing import Any
245 class A:
246 a = None # type: A
247 b = None # type: Any
248 def f(self) -> None:
249 self.b = self.a
250 self.a.a = self.b
251 [out]
252 MemberExpr(6) : A
253 MemberExpr(6) : Any
254 NameExpr(6) : A
255 NameExpr(6) : A
256 MemberExpr(7) : A
257 MemberExpr(7) : A
258 MemberExpr(7) : Any
259 NameExpr(7) : A
260 NameExpr(7) : A
261
262 [case testIf]
263
264 a = None # type: bool
265 if a:
266 1
267 elif not a:
268 1
269 [builtins fixtures/bool.pyi]
270 [out]
271 NameExpr(3) : builtins.bool
272 IntExpr(4) : Literal[1]?
273 NameExpr(5) : Literal[False]
274 UnaryExpr(5) : builtins.bool
275 IntExpr(6) : Literal[1]?
276
277 [case testWhile]
278
279 a = None # type: bool
280 while a:
281 a
282 [builtins fixtures/bool.pyi]
283 [out]
284 NameExpr(3) : builtins.bool
285 NameExpr(4) : Literal[True]
286
287
288 -- Simple type inference
289 -- ---------------------
290
291
292 [case testInferSingleType]
293 import typing
294 x = ()
295 [builtins fixtures/primitives.pyi]
296 [out]
297 NameExpr(2) : tuple[()]
298 TupleExpr(2) : tuple[()]
299
300 [case testInferTwoTypes]
301 ## NameExpr
302 import typing
303 (s,
304 i) = 'x', 1
305 [builtins fixtures/primitives.pyi]
306 [out]
307 NameExpr(3) : builtins.str
308 NameExpr(4) : builtins.int
309
310 [case testInferSingleLocalVarType]
311 import typing
312 def f() -> None:
313 x = ()
314 [builtins fixtures/primitives.pyi]
315 [out]
316 NameExpr(3) : tuple[()]
317 TupleExpr(3) : tuple[()]
318
319
320 -- Basic generics
321 -- --------------
322
323
324 [case testImplicitBoundTypeVarsForMethod]
325 ## MemberExpr
326 from typing import TypeVar, Generic
327 T = TypeVar('T')
328 class A(Generic[T]):
329 def f(self) -> T: pass
330 class B: pass
331 def g() -> None:
332 a = None # type: A[B]
333 f = a.f
334 [out]
335 MemberExpr(9) : def () -> B
336
337 [case testImplicitBoundTypeVarsForSelfMethodReference]
338 from typing import TypeVar, Generic
339 T = TypeVar('T')
340 class A(Generic[T]):
341 def f(self) -> T:
342 return self.f()
343 [out]
344 CallExpr(5) : T`1
345 MemberExpr(5) : def () -> T`1
346 NameExpr(5) : A[T`1]
347
348 [case testGenericFunctionCallWithTypeApp-skip]
349 ## CallExpr|TypeApplication|NameExpr
350 from typing import Any, TypeVar, Tuple
351 T = TypeVar('T')
352 class A: pass
353 f[A](A())
354 f[Any](A())
355 def f(a: T) -> Tuple[T, T]: pass
356 [builtins fixtures/tuple.pyi]
357 [out]
358 CallExpr(5) : A
359 CallExpr(5) : Tuple[A, A]
360 NameExpr(5) : def () -> A
361 NameExpr(5) : def (a: A) -> Tuple[A, A]
362 TypeApplication(5) : def (a: A) -> Tuple[A, A]
363 CallExpr(6) : A
364 CallExpr(6) : Tuple[Any, Any]
365 NameExpr(6) : def () -> A
366 NameExpr(6) : def (a: Any) -> Tuple[Any, Any]
367 TypeApplication(6) : def (a: Any) -> Tuple[Any, Any]
368
369 -- NOTE: Type applications are not supported for generic methods, so the
370 -- following test cases are commented out.
371
372 --[case testGenericMethodCallWithTypeApp]
373 --## CallExpr|MemberExpr|TypeApplication
374 --from typing import Any, TypeVar, Tuple
375 --T = TypeVar('T')
376 --class A:
377 -- def f(self, a: T) -> Tuple[T, T]: pass
378 --a.f[A](a)
379 --a.f[Any](a)
380 --a = None # type: A
381 --[builtins fixtures/tuple.py]
382 --[out]
383 --CallExpr(2) : Tuple[A, A]
384 --MemberExpr(2) : def (A a) -> Tuple[A, A]
385 --TypeApplication(2) : def (A a) -> Tuple[A, A]
386 --CallExpr(3) : Tuple[Any, Any]
387 --MemberExpr(3) : def (any a) -> Tuple[Any, Any]
388 --TypeApplication(3) : def (any a) -> Tuple[Any, Any]
389
390 --[case testGenericMethodCallInGenericTypeWithTypeApp]
391 --## CallExpr|MemberExpr|TypeApplication
392 --from typing import Any, TypeVar, Generic, Tuple
393 --T = TypeVar('T')
394 --S = TypeVar('S')
395 --class B: pass
396 --class C: pass
397 --a.f[B](b)
398 --a.f[Any](b)
399 --class A(Generic[T]):
400 -- def f(self, a: S) -> Tuple[T, S]: pass
401 --a = None # type: A[C]
402 --b = None # type: B
403 --[builtins fixtures/tuple.py]
404 --[out]
405 --CallExpr(6) : Tuple[C, B]
406 --MemberExpr(6) : def (B a) -> Tuple[C, B]
407 --TypeApplication(6) : def (B a) -> Tuple[C, B]
408 --CallExpr(7) : Tuple[C, Any]
409 --MemberExpr(7) : def (any a) -> Tuple[C, Any]
410 --TypeApplication(7) : def (any a) -> Tuple[C, Any]
411
412 [case testGenericTypeVariableInference]
413 from typing import TypeVar, Generic
414 T = TypeVar('T')
415 class A(Generic[T]):
416 def __init__(self, a: T) -> None: pass
417 class B: pass
418 A(A(B()))
419 [out]
420 CallExpr(6) : A[A[B]]
421 CallExpr(6) : A[B]
422 CallExpr(6) : B
423 NameExpr(6) : def (a: A[B]) -> A[A[B]]
424 NameExpr(6) : def (a: B) -> A[B]
425 NameExpr(6) : def () -> B
426
427
428 -- Generic inheritance
429 -- -------------------
430
431
432 [case testInheritedMethodReferenceWithGenericInheritance]
433 from typing import TypeVar, Generic
434 T = TypeVar('T')
435 class C: pass
436 class A(Generic[T]):
437 def f(self, a: T) -> None: pass
438 class B(A[C]):
439 def g(self, c: C) -> None:
440 self.f(c)
441 [out]
442 CallExpr(8) : None
443 MemberExpr(8) : def (a: C)
444 NameExpr(8) : C
445 NameExpr(8) : B
446
447 [case testInheritedMethodReferenceWithGenericSubclass]
448 from typing import TypeVar, Generic
449 S = TypeVar('S')
450 T = TypeVar('T')
451 class C: pass
452 class A(Generic[S, T]):
453 def f(self, a: C) -> None: pass
454 class B(A[C, T], Generic[T]):
455 def g(self, c: C) -> None:
456 self.f(c)
457 [out]
458 CallExpr(9) : None
459 MemberExpr(9) : def (a: C)
460 NameExpr(9) : C
461 NameExpr(9) : B[T`1]
462
463 [case testExternalReferenceWithGenericInheritance]
464 from typing import TypeVar, Generic
465 T = TypeVar('T')
466 class C: pass
467 class A(Generic[T]):
468 def f(self, a: T) -> None: pass
469 class B(A[C]): pass
470 b = None # type: B
471 c = None # type: C
472 b.f(c)
473 [out]
474 CallExpr(9) : None
475 MemberExpr(9) : def (a: C)
476 NameExpr(9) : B
477 NameExpr(9) : C
478
479
480 -- Implicit Any types
481 -- ------------------
482
483
484 [case testDynamicallyTypedFunction]
485
486 def f(x):
487 y = x + o
488 z = o
489 z
490 o = None # type: object
491 [out]
492 NameExpr(3) : builtins.object
493 NameExpr(3) : Any
494 NameExpr(3) : Any
495 OpExpr(3) : Any
496 NameExpr(4) : builtins.object
497 NameExpr(4) : Any
498 NameExpr(5) : Any
499
500 [case testDynamicallyTypedMethod]
501
502 class A:
503 def f(self, x):
504 y = (
505 o) # Place y and o on separate lines
506 x
507 y
508 o = None # type: object
509 [out]
510 NameExpr(4) : Any
511 NameExpr(5) : builtins.object
512 NameExpr(6) : Any
513 NameExpr(7) : Any
514
515 [case testDynamicallyTypedConstructor]
516
517 class A:
518 def __init__(self, x):
519 y = o
520 x
521 y
522 o = None # type: object
523 [out]
524 NameExpr(4) : builtins.object
525 NameExpr(4) : Any
526 NameExpr(5) : Any
527 NameExpr(6) : Any
528
529 [case testCallInDynamicallyTypedFunction]
530
531 def f():
532 g(o)
533 def g(a: object) -> object: pass
534 o = None # type: object
535 [out]
536 CallExpr(3) : Any
537 NameExpr(3) : def (a: builtins.object) -> builtins.object
538 NameExpr(3) : builtins.object
539
540 [case testExpressionInDynamicallyTypedFn]
541 import typing
542 def f():
543 x = None
544 x.f()
545 [out]
546 CallExpr(4) : Any
547 MemberExpr(4) : Any
548 NameExpr(4) : Any
549
550 [case testGenericCall]
551 from typing import TypeVar, Generic
552 T = TypeVar('T')
553 def f() -> None:
554 a1 = A(b) # type: A[B]
555 a2 = A(b) # type: A[object]
556 class A(Generic[T]):
557 def __init__(self, a: T) -> None: pass
558 class B: pass
559 b = None # type: B
560 [out]
561 CallExpr(4) : A[B]
562 NameExpr(4) : def (a: B) -> A[B]
563 NameExpr(4) : B
564 CallExpr(5) : A[builtins.object]
565 NameExpr(5) : def (a: builtins.object) -> A[builtins.object]
566 NameExpr(5) : B
567
568 [case testGenericCallInDynamicallyTypedFunction]
569 from typing import TypeVar, Generic
570 T = TypeVar('T')
571 def f():
572 A()
573 class A(Generic[T]): pass
574 [out]
575 CallExpr(4) : Any
576 NameExpr(4) : def [T] () -> A[T`1]
577
578 [case testGenericCallInDynamicallyTypedFunction2]
579 from typing import TypeVar, Generic
580 T = TypeVar('T')
581 def f():
582 A(f)
583 class A(Generic[T]):
584 def __init__(self, x: T) -> None: pass
585 [out]
586 CallExpr(4) : Any
587 NameExpr(4) : def [T] (x: T`1) -> A[T`1]
588 NameExpr(4) : def () -> Any
589
590 [case testGenericCallInDynamicallyTypedFunction3]
591 from typing import TypeVar
592 t = TypeVar('t')
593 def f():
594 g(None)
595 def g(x: t) -> t: pass
596 [out]
597 CallExpr(4) : Any
598 NameExpr(4) : def [t] (x: t`-1) -> t`-1
599
600
601 -- Generic types and type inference
602 -- --------------------------------
603
604
605 [case testInferenceInArgumentContext]
606 ## CallExpr
607 from typing import TypeVar, Generic
608 T = TypeVar('T')
609 class A(Generic[T]): pass
610 class B: pass
611 class C(B): pass
612 def f(a: 'A[B]') -> None: pass
613 def g() -> 'A[T]': pass
614 def h(a: T) -> 'A[T]': pass
615
616 b = None # type: B
617 c = None # type: C
618
619 f(g())
620 f(h(b))
621 f(h(c))
622 [out]
623 CallExpr(14) : None
624 CallExpr(14) : A[B]
625 CallExpr(15) : None
626 CallExpr(15) : A[B]
627 CallExpr(16) : None
628 CallExpr(16) : A[B]
629
630 [case testInferGenericTypeForLocalVariable]
631 from typing import TypeVar, Generic
632 T = TypeVar('T')
633 def f() -> None:
634 a = A(b)
635 a
636 a2, a3 = A(b), A(c)
637 a2
638 a3
639 b = None # type: B
640 c = None # type: C
641 class A(Generic[T]):
642 def __init__(self, x: T) -> None: pass
643 class B: pass
644 class C: pass
645 [out]
646 CallExpr(4) : A[B]
647 NameExpr(4) : def (x: B) -> A[B]
648 NameExpr(4) : A[B]
649 NameExpr(4) : B
650 NameExpr(5) : A[B]
651 CallExpr(6) : A[B]
652 CallExpr(6) : A[C]
653 NameExpr(6) : def (x: B) -> A[B]
654 NameExpr(6) : def (x: C) -> A[C]
655 NameExpr(6) : A[B]
656 NameExpr(6) : A[C]
657 NameExpr(6) : B
658 NameExpr(6) : C
659 NameExpr(7) : A[B]
660 NameExpr(8) : A[C]
661
662 [case testNestedGenericCalls]
663 from typing import TypeVar, Generic
664 T = TypeVar('T')
665 S = TypeVar('S')
666 def h() -> None:
667 g(f(c))
668
669 c = None # type: C
670
671 class A(Generic[T]): pass
672 class B(Generic[T]): pass
673 class C: pass
674 def f(a: T) -> A[T]: pass
675 def g(a: S) -> B[S]: pass
676 [out]
677 CallExpr(5) : A[C]
678 CallExpr(5) : B[A[C]]
679 NameExpr(5) : C
680 NameExpr(5) : def (a: C) -> A[C]
681 NameExpr(5) : def (a: A[C]) -> B[A[C]]
682
683 [case testInferListLiterals]
684 from typing import List
685 a = [] # type: List[A]
686 class A: pass
687 [builtins fixtures/list.pyi]
688 [out]
689 ListExpr(2) : builtins.list[A]
690
691 [case testInferGenericTypeInTypeAnyContext]
692 from typing import Any
693 a = [] # type: Any
694 [builtins fixtures/list.pyi]
695 [out]
696 ListExpr(2) : builtins.list[Any]
697
698 [case testHigherOrderFunction]
699 from typing import TypeVar, Callable, List
700 t = TypeVar('t')
701 s = TypeVar('s')
702 def map(f: Callable[[t], s], a: List[t]) -> List[s]: pass
703 class A: pass
704 class B: pass
705 def f(a: A) -> B: pass
706 map(
707 f,
708 [A()])
709 [builtins fixtures/list.pyi]
710 [out]
711 CallExpr(8) : builtins.list[B]
712 NameExpr(8) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
713 NameExpr(9) : def (a: A) -> B
714 CallExpr(10) : A
715 ListExpr(10) : builtins.list[A]
716 NameExpr(10) : def () -> A
717
718
719 -- Lambdas
720 -- -------
721
722
723 [case testLambdaWithTypeInferredFromContext]
724 from typing import Callable
725 f = lambda x: x.a # type: Callable[[B], A]
726 class A: pass
727 class B:
728 a = None # type: A
729 [out]
730 LambdaExpr(2) : def (x: B) -> A
731 MemberExpr(2) : A
732 NameExpr(2) : B
733
734 [case testLambdaWithInferredType]
735 ## LambdaExpr|NameExpr
736 import typing
737 f = lambda: 1
738 [out]
739 LambdaExpr(3) : def () -> Literal[1]?
740 NameExpr(3) : def () -> builtins.int
741
742 [case testLambdaWithInferredType2]
743 ## LambdaExpr|NameExpr
744 import typing
745 f = lambda: [1]
746 [builtins fixtures/list.pyi]
747 [out]
748 LambdaExpr(3) : def () -> builtins.list[builtins.int]
749 NameExpr(3) : def () -> builtins.list[builtins.int]
750
751 [case testLambdaWithInferredType3]
752 from typing import List, Callable
753 f = lambda x: [] # type: Callable[[B], List[A]]
754 class A: pass
755 class B:
756 a = None # type: A
757 [builtins fixtures/list.pyi]
758 [out]
759 LambdaExpr(2) : def (x: B) -> builtins.list[A]
760 ListExpr(2) : builtins.list[A]
761
762 [case testLambdaAndHigherOrderFunction]
763 from typing import TypeVar, Callable, List
764 t = TypeVar('t')
765 s = TypeVar('s')
766 def map(f: Callable[[t], s], a: List[t]) -> List[s]: pass
767 class A: pass
768 class B: pass
769 def f(a: A) -> B: pass
770 l = None # type: List[A]
771 map(
772 lambda x: f(x), l)
773 [builtins fixtures/list.pyi]
774 [out]
775 CallExpr(9) : builtins.list[B]
776 NameExpr(9) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
777 CallExpr(10) : B
778 LambdaExpr(10) : def (x: A) -> B
779 NameExpr(10) : def (a: A) -> B
780 NameExpr(10) : builtins.list[A]
781 NameExpr(10) : A
782
783 [case testLambdaAndHigherOrderFunction2]
784 ## LambdaExpr|NameExpr|ListExpr
785 from typing import TypeVar, List, Callable
786 t = TypeVar('t')
787 s = TypeVar('s')
788 def map(f: Callable[[t], List[s]], a: List[t]) -> List[s]: pass
789 class A: pass
790 class B: pass
791 def f(a: A) -> B: pass
792 l = None # type: List[A]
793 map(
794 lambda x: [f(x)], l)
795 [builtins fixtures/list.pyi]
796 [out]
797 NameExpr(10) : def (f: def (A) -> builtins.list[B], a: builtins.list[A]) -> builtins.list[B]
798 LambdaExpr(11) : def (x: A) -> builtins.list[B]
799 ListExpr(11) : builtins.list[B]
800 NameExpr(11) : def (a: A) -> B
801 NameExpr(11) : builtins.list[A]
802 NameExpr(11) : A
803
804 [case testLambdaInListAndHigherOrderFunction]
805 from typing import TypeVar, Callable, List
806 t = TypeVar('t')
807 s = TypeVar('s')
808 def map(f: List[Callable[[t], s]], a: List[t]) -> List[s]: pass
809 class A: pass
810 l = None # type: List[A]
811 map(
812 [lambda x: x],
813 l)
814 [builtins fixtures/list.pyi]
815 [out]
816 -- TODO We probably should not silently infer 'Any' types in statically typed
817 -- context. Perhaps just fail instead?
818 CallExpr(7) : builtins.list[Any]
819 NameExpr(7) : def (f: builtins.list[def (A) -> Any], a: builtins.list[A]) -> builtins.list[Any]
820 LambdaExpr(8) : def (x: A) -> A
821 ListExpr(8) : builtins.list[def (A) -> Any]
822 NameExpr(8) : A
823 NameExpr(9) : builtins.list[A]
824
825 [case testLambdaAndHigherOrderFunction3]
826 from typing import TypeVar, Callable, List
827 t = TypeVar('t')
828 s = TypeVar('s')
829 def map(f: Callable[[t], s], a: List[t]) -> List[s]: pass
830 class A:
831 b = None # type: B
832 class B: pass
833 l = None # type: List[A]
834 map(
835 lambda x: x.b,
836 l)
837 [builtins fixtures/list.pyi]
838 [out]
839 CallExpr(9) : builtins.list[B]
840 NameExpr(9) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
841 LambdaExpr(10) : def (x: A) -> B
842 MemberExpr(10) : B
843 NameExpr(10) : A
844 NameExpr(11) : builtins.list[A]
845
846 [case testLambdaAndHigherOrderFunctionAndKeywordArgs]
847 from typing import TypeVar, Callable, List
848 t = TypeVar('t')
849 s = TypeVar('s')
850 def map(f: Callable[[t], s], a: List[t]) -> List[s]: pass
851 class A:
852 b = None # type: B
853 class B: pass
854 l = None # type: List[A]
855 map(
856 a=l,
857 f=lambda x: x.b)
858 [builtins fixtures/list.pyi]
859 [out]
860 CallExpr(9) : builtins.list[B]
861 NameExpr(9) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
862 NameExpr(10) : builtins.list[A]
863 LambdaExpr(11) : def (x: A) -> B
864 MemberExpr(11) : B
865 NameExpr(11) : A
866
867
868 -- Boolean operations
869 -- ------------------
870
871
872 [case testBooleanOr]
873 from typing import List
874 a = None # type: List[A]
875 a or []
876 a = a or []
877 if int():
878 a = [] or a
879 class A: pass
880 [builtins fixtures/list.pyi]
881 [out]
882 ListExpr(3) : builtins.list[A]
883 NameExpr(3) : builtins.list[A]
884 OpExpr(3) : builtins.list[A]
885 ListExpr(4) : builtins.list[A]
886 NameExpr(4) : builtins.list[A]
887 NameExpr(4) : builtins.list[A]
888 OpExpr(4) : builtins.list[A]
889 CallExpr(5) : builtins.int
890 NameExpr(5) : def () -> builtins.int
891 ListExpr(6) : builtins.list[A]
892 NameExpr(6) : builtins.list[A]
893 NameExpr(6) : builtins.list[A]
894 OpExpr(6) : builtins.list[A]
895
896
897 -- Class attributes
898 -- ----------------
899
900
901 [case testUnboundMethod]
902 ## MemberExpr
903 import typing
904 class A:
905 def f(self) -> None: pass
906 A.f
907 [out]
908 MemberExpr(5) : def (self: A)
909
910 [case testUnboundMethodWithImplicitSig]
911 ## MemberExpr
912 import typing
913 class A:
914 def f(self): pass
915 A.f
916 [out]
917 MemberExpr(5) : def (self: A) -> Any
918
919 [case testOverloadedUnboundMethod]
920 ## MemberExpr
921 from typing import overload
922 class A:
923 @overload
924 def f(self) -> None: pass
925 @overload
926 def f(self, __x: object) -> None: pass
927
928 def f(self, *args) -> None: pass
929 A.f
930 [builtins fixtures/tuple.pyi]
931 [out]
932 MemberExpr(10) : Overload(def (self: A), def (self: A, builtins.object))
933
934 [case testOverloadedUnboundMethodWithImplicitSig]
935 ## MemberExpr
936 from typing import overload
937 class A:
938 @overload
939 def f(self): pass
940 @overload
941 def f(self, __x): pass
942
943 def f(self, *args): pass
944 A.f
945 [builtins fixtures/tuple.pyi]
946 [out]
947 MemberExpr(10) : Overload(def (self: A) -> Any, def (self: A, Any) -> Any)
948
949 [case testUnboundMethodWithInheritance]
950 ## MemberExpr
951 import typing
952 class A:
953 def __init__(self) -> None: pass
954 def f(self) -> None: pass
955 class B(A):
956 pass
957 B.f
958 [out]
959 MemberExpr(8) : def (self: A)
960
961 [case testUnboundGenericMethod]
962 ## MemberExpr
963 from typing import TypeVar
964 t = TypeVar('t')
965 class B: pass
966 class A:
967 def f(self, x: t) -> None: pass
968 A.f(A(), B())
969 [out]
970 MemberExpr(7) : def (self: A, x: B)
971
972 [case testUnboundMethodOfGenericClass]
973 ## MemberExpr
974 from typing import TypeVar, Generic
975 t = TypeVar('t')
976 class B: pass
977 class A(Generic[t]):
978 def f(self, x: t) -> None: pass
979 A.f
980 a_b = A() # type: A[B]
981 A.f(a_b, B())
982 [out]
983 MemberExpr(7) : def [t] (self: A[t`1], x: t`1)
984 MemberExpr(9) : def (self: A[B], x: B)
985
986 [case testUnboundOverloadedMethodOfGenericClass]
987 ## CallExpr
988 from typing import TypeVar, Generic, overload
989 t = TypeVar('t')
990 class B: pass
991 class A(Generic[t]):
992 @overload
993 def f(self, x: t) -> t: pass
994 @overload
995 def f(self) -> object: pass
996 def f(self, *args): pass
997
998 ab, b = None, None # type: (A[B], B)
999 A.f(ab, b)
1000 [builtins fixtures/tuple.pyi]
1001 [out]
1002 CallExpr(13) : B
1003
1004 [case testUnboundMethodOfGenericClassWithImplicitSig]
1005 ## MemberExpr
1006 from typing import TypeVar, Generic
1007 t = TypeVar('t')
1008 class B: pass
1009 class A(Generic[t]):
1010 def f(self, x): pass
1011 A.f(None, None)
1012 [out]
1013 MemberExpr(7) : def (self: A[t`1], x: Any) -> Any
1014
1015 [case testGenericMethodOfGenericClass]
1016 ## MemberExpr
1017 from typing import TypeVar, Generic
1018 t = TypeVar('t')
1019 s = TypeVar('s')
1020 class B: pass
1021 class A(Generic[t]):
1022 def f(self, y: s) -> None: pass
1023 ab = None # type: A[B]
1024 o = None # type: object
1025 A.f(ab, o)
1026 [out]
1027 MemberExpr(10) : def (self: A[B], y: builtins.object)
1028
1029
1030 -- Type variables with value restriction
1031 -- -------------------------------------
1032
1033
1034 [case testTypeVariableWithValueRestriction]
1035 ## NameExpr
1036 from typing import TypeVar
1037 T = TypeVar('T', int, str)
1038 def f(x: T) -> None: pass
1039 f(1)
1040 f('x')
1041 [out]
1042 NameExpr(5) : def (x: builtins.int)
1043 NameExpr(6) : def (x: builtins.str)
1044
1045 [case testTypeVariableWithValueRestrictionAndSubtype]
1046 ## NameExpr|CallExpr
1047 from typing import TypeVar
1048 T = TypeVar('T', int, str)
1049 def f(x: T) -> T: pass
1050 class S(str): pass
1051 s = None # type: S
1052 f(s)
1053 [out]
1054 CallExpr(7) : builtins.str
1055 NameExpr(7) : def (x: builtins.str) -> builtins.str
1056 NameExpr(7) : S
1057
1058 [case testTypeVariableWithValueRestrictionInFunction]
1059 ## NameExpr
1060 from typing import TypeVar
1061
1062 T = TypeVar("T", int, str)
1063
1064 def f(x: T) -> T:
1065 y = 1
1066 return x
1067 [out]
1068 NameExpr(7) : builtins.int
1069 NameExpr(7) : builtins.int
1070 NameExpr(8) : builtins.int
1071 NameExpr(8) : builtins.str
1072
1073
1074 -- Binary operations
1075 -- -----------------
1076
1077
1078 [case testBinaryOperatorWithAnyLeftOperand]
1079 ## OpExpr
1080 from typing import Any, cast
1081 class B:
1082 def __add__(self, x: int) -> str: pass
1083 class A:
1084 def __radd__(self, x: B) -> int: pass
1085 cast(Any, 1) + A()
1086 B() + A()
1087 [out]
1088 OpExpr(7) : Any
1089 OpExpr(8) : builtins.int
1090
1091 [case testBinaryOperatorWithAnyRightOperand]
1092 ## OpExpr
1093 from typing import Any, cast
1094 class A:
1095 def __add__(self, x: str) -> int: pass
1096 A() + cast(Any, 1)
1097 [out]
1098 OpExpr(5) : Any
1099
1100
1101 -- Callable overloading
1102 -- --------------------
1103
1104
1105 [case testOverloadedFunctionType]
1106 ## CallExpr
1107 from typing import overload
1108 @overload
1109 def f(x: int) -> str: pass
1110 @overload
1111 def f(x: str) -> int: pass
1112 def f(x): pass
1113 f(1)
1114 f('')
1115 [out]
1116 CallExpr(8) : builtins.str
1117 CallExpr(9) : builtins.int
1118
1119 [case testOverlappingOverloadedFunctionType]
1120 ## CallExpr
1121 from typing import overload, Any
1122 class A: pass
1123 class B(A): pass
1124 @overload
1125 def f(x: B) -> B: pass
1126 @overload
1127 def f(x: A) -> A: pass
1128
1129 def f(x) -> Any: pass
1130
1131 a = None # type: A
1132 b = None # type: B
1133 f(a)
1134 f(b)
1135 [out]
1136 CallExpr(14) : A
1137 CallExpr(15) : B
1138
1139
1140
1141 [case testOverloadedErasedType]
1142 from typing import Callable
1143 from typing import List
1144 from typing import overload
1145 from typing import TypeVar
1146
1147 T = TypeVar("T")
1148 V = TypeVar("V")
1149
1150 def fun(s: int) -> int: pass
1151
1152 def m(fun: Callable[[T], V], iter: List[T]) -> None: pass
1153
1154 nums = [1] # type: List[int]
1155 m(fun,
1156 nums)
1157 [builtins fixtures/list.pyi]
1158 [out]
1159 IntExpr(13) : Literal[1]?
1160 ListExpr(13) : builtins.list[builtins.int]
1161 CallExpr(14) : None
1162 NameExpr(14) : def (s: builtins.int) -> builtins.int
1163 NameExpr(14) : def (fun: def (builtins.int) -> builtins.int, iter: builtins.list[builtins.int])
1164 NameExpr(15) : builtins.list[builtins.int]
1165
1166
1167 -- Special cases
1168 -- -------------
1169
1170
1171 [case testImplicitDataAttributeInit]
1172 ## NameExpr
1173 import typing
1174 class A:
1175 def __init__(self) -> None:
1176 self.x = (
1177 A())
1178 [out]
1179 NameExpr(5) : A
1180 NameExpr(6) : def () -> A
1181
1182 [case testListMultiplicationInContext]
1183 ## ListExpr|OpExpr|IntExpr
1184 from typing import List
1185 a = [None] * 3 # type: List[str]
1186 [builtins fixtures/list.pyi]
1187 [out]
1188 IntExpr(3) : Literal[3]?
1189 ListExpr(3) : builtins.list[builtins.str]
1190 OpExpr(3) : builtins.list[builtins.str]
1191
1192 [case testStringFormatting]
1193 ## IntExpr|OpExpr|StrExpr
1194 '%d' % 1
1195 [builtins fixtures/primitives.pyi]
1196 [typing fixtures/typing-medium.pyi]
1197 [out]
1198 IntExpr(2) : Literal[1]?
1199 OpExpr(2) : builtins.str
1200 StrExpr(2) : Literal['%d']?
1201
1202 [case testExportOverloadArgType]
1203 ## LambdaExpr|NameExpr
1204 from typing import List, overload, Callable
1205 @overload
1206 def f(x: int, f: Callable[[int], int]) -> None: ...
1207 @overload
1208 def f(x: str, f: Callable[[str], str]) -> None: ...
1209 def f(x): ...
1210 f(
1211 1, lambda x: x)
1212 [builtins fixtures/list.pyi]
1213 [out]
1214 NameExpr(8) : Overload(def (x: builtins.int, f: def (builtins.int) -> builtins.int), def (x: builtins.str, f: def (builtins.str) -> builtins.str))
1215 LambdaExpr(9) : def (x: builtins.int) -> builtins.int
1216 NameExpr(9) : builtins.int
1217
1218 [case testExportOverloadArgTypeNested]
1219 ## LambdaExpr
1220 from typing import overload, Callable
1221 @overload
1222 def f(x: int, f: Callable[[int], int]) -> int: ...
1223 @overload
1224 def f(x: str, f: Callable[[str], str]) -> str: ...
1225 def f(x): ...
1226 f(
1227 f(1, lambda y: y),
1228 lambda x: x)
1229 f(
1230 f('x', lambda y: y),
1231 lambda x: x)
1232 [builtins fixtures/list.pyi]
1233 [out]
1234 LambdaExpr(9) : def (y: builtins.int) -> builtins.int
1235 LambdaExpr(10) : def (x: builtins.int) -> builtins.int
1236 LambdaExpr(12) : def (y: builtins.str) -> builtins.str
1237 LambdaExpr(13) : def (x: builtins.str) -> builtins.str
1238
1239 [case testExportOverloadArgTypeDict]
1240 ## DictExpr
1241 from typing import TypeVar, Generic, Any, overload, Dict
1242 T = TypeVar("T")
1243 class Key(Generic[T]): ...
1244 @overload
1245 def f(x: Key[T], y: T) -> T: ...
1246 @overload
1247 def f(x: int, y: Any) -> Any: ...
1248 def f(x, y): ...
1249 d: Dict = {}
1250 d.get(
1251 "", {})
1252 f(
1253 2, {})
1254 [builtins fixtures/dict.pyi]
1255 [out]
1256 DictExpr(10) : builtins.dict[Any, Any]
1257 DictExpr(12) : builtins.dict[Any, Any]
1258 DictExpr(14) : builtins.dict[Any, Any]
1259
1260 -- TODO
1261 --
1262 -- test expressions
1263 -- list literal
1264 -- tuple literal
1265 -- unary minus
1266 -- indexing
1267 -- super expression
1268 -- more complex lambda (multiple arguments etc.)
1269 -- list comprehension
1270 -- generator expression
1271 -- other things
1272 -- type inference
1273 -- default argument value
1274 -- for loop variable
1275 -- exception variable
1276 -- varargs
1277 -- generics
1278 -- explicit types
1279 -- type of 'None' (currently stripped, but sometimes we may want to dump it)