forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typexport-basic.test
1279 lines (1132 loc) · 25.7 KB
/
typexport-basic.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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Test cases for exporting node types from the type checker.
--
-- Each test case consists of at least two sections.
-- The first section contains [case NAME] followed by the input code,
-- while the second section contains [out] followed by the output from the type
-- checker.
--
-- The first line of input code should be a regexp in comment that describes
-- the information to dump (prefix with ##). The regexp is matched against
-- the following items:
--
-- * each name of an expression node
-- * each type string of a node (e.g. OpExpr)
--
-- Lines starting with "--" in this file will be ignored.
-- Expressions
-- -----------
[case testConstructorCall]
import typing
class A: pass
class B: pass
A()
B()
[out]
CallExpr(4) : A
NameExpr(4) : def () -> A
CallExpr(5) : B
NameExpr(5) : def () -> B
[case testLiterals]
import typing
5
2.3
'foo'
[builtins fixtures/primitives.pyi]
[out]
IntExpr(2) : Literal[5]?
FloatExpr(3) : builtins.float
StrExpr(4) : Literal['foo']?
[case testNameExpression]
a = None # type: A
a # node
def f(aa: 'A') -> None:
b = None # type: B
aa # node
b # node
class A:
def g(self) -> None:
self # node
class B: pass
[out]
NameExpr(3) : A
NameExpr(6) : A
NameExpr(7) : B
NameExpr(10) : A
[case testEllipsis]
import typing
...
[out]
EllipsisExpr(2) : builtins.ellipsis
[case testMemberAccess]
## MemberExpr|CallExpr
a = None # type: A
a.m
a.f
a.f()
class A:
m = None # type: A
def f(self) -> 'B': pass
class B: pass
[out]
MemberExpr(4) : A
MemberExpr(5) : def () -> B
CallExpr(6) : B
MemberExpr(6) : def () -> B
[case testCastExpression]
## CastExpr|[a-z]
from typing import Any, cast
d = None # type: Any
b = None # type: B
class A: pass
class B(A): pass
cast(A, d)
cast(A, b)
cast(B, b)
[out]
CastExpr(7) : A
NameExpr(7) : Any
CastExpr(8) : A
NameExpr(8) : B
CastExpr(9) : B
NameExpr(9) : B
[case testAssertTypeExpr]
## AssertTypeExpr|[a-z]
from typing import Any, assert_type
d = None # type: Any
a = None # type: A
b = None # type: B
class A: pass
class B(A): pass
assert_type(d, Any)
assert_type(a, A)
assert_type(b, B)
[out]
AssertTypeExpr(8) : Any
NameExpr(8) : Any
AssertTypeExpr(9) : A
NameExpr(9) : A
AssertTypeExpr(10) : B
NameExpr(10) : B
[case testArithmeticOps]
## OpExpr
import typing
a = 1 + 2
1.2 * 3
2.2 - 3
1 / 2
[file builtins.py]
class object:
def __init__(self) -> None: pass
class function: pass
class int:
def __add__(self, x: int) -> int: pass
def __truediv__(self, x: int) -> float: pass
class float:
def __mul__(self, x: int) -> float: pass
def __sub__(self, x: int) -> float: pass
class type: pass
class str: pass
class list: pass
class dict: pass
[out]
OpExpr(3) : builtins.int
OpExpr(4) : builtins.float
OpExpr(5) : builtins.float
OpExpr(6) : builtins.float
[case testComparisonOps]
## ComparisonExpr
import typing
1 == object()
1 == 2
2 < 3
1 < 2 < 3
8 > 3
4 < 6 > 2
[file builtins.py]
class object:
def __init__(self) -> None: pass
class int:
def __eq__(self, x: object) -> bool: pass
def __lt__(self, x: int) -> bool: pass
def __gt__(self, x: int) -> int: pass
class bool: pass
class type: pass
class function: pass
class str: pass
class list: pass
class dict: pass
[out]
ComparisonExpr(3) : builtins.bool
ComparisonExpr(4) : builtins.bool
ComparisonExpr(5) : builtins.bool
ComparisonExpr(6) : builtins.bool
ComparisonExpr(7) : builtins.int
ComparisonExpr(8) : builtins.object
[case testBooleanOps]
## OpExpr|UnaryExpr
import typing
a = 1
a and a
a or a
not a
[builtins fixtures/bool.pyi]
[out]
OpExpr(4) : builtins.int
OpExpr(5) : builtins.int
UnaryExpr(6) : builtins.bool
[case testBooleanOpsOnBools]
## OpExpr|UnaryExpr
import typing
a = bool()
a and a
a or a
not a
[builtins fixtures/bool.pyi]
[out]
OpExpr(4) : builtins.bool
OpExpr(5) : builtins.bool
UnaryExpr(6) : builtins.bool
[case testFunctionCall]
## CallExpr
from typing import Tuple
class A: pass
class B: pass
def f(a: A, b: B) -> Tuple[A, B]: pass
f(
A(),
B())
[builtins fixtures/tuple-simple.pyi]
[out]
CallExpr(6) : Tuple[A, B]
CallExpr(7) : A
CallExpr(8) : B
-- Statements
-- ----------
[case testSimpleAssignment]
from typing import Any
a = None # type: A
b = a # type: Any
if b:
b = a
a = b
class A: pass
[out]
NameExpr(3) : A
NameExpr(4) : Any
NameExpr(5) : A
NameExpr(5) : Any
NameExpr(6) : A
NameExpr(6) : Any
[case testMemberAssignment]
from typing import Any
class A:
a = None # type: A
b = None # type: Any
def f(self) -> None:
self.b = self.a
self.a.a = self.b
[out]
MemberExpr(6) : A
MemberExpr(6) : Any
NameExpr(6) : A
NameExpr(6) : A
MemberExpr(7) : A
MemberExpr(7) : A
MemberExpr(7) : A
NameExpr(7) : A
NameExpr(7) : A
[case testIf]
a = None # type: bool
if a:
1
elif not a:
1
[builtins fixtures/bool.pyi]
[out]
NameExpr(3) : builtins.bool
IntExpr(4) : Literal[1]?
NameExpr(5) : Literal[False]
UnaryExpr(5) : builtins.bool
IntExpr(6) : Literal[1]?
[case testWhile]
a = None # type: bool
while a:
a
[builtins fixtures/bool.pyi]
[out]
NameExpr(3) : builtins.bool
NameExpr(4) : Literal[True]
-- Simple type inference
-- ---------------------
[case testInferSingleType]
import typing
x = ()
[builtins fixtures/primitives.pyi]
[out]
NameExpr(2) : Tuple[()]
TupleExpr(2) : Tuple[()]
[case testInferTwoTypes]
## NameExpr
import typing
(s,
i) = 'x', 1
[builtins fixtures/primitives.pyi]
[out]
NameExpr(3) : builtins.str
NameExpr(4) : builtins.int
[case testInferSingleLocalVarType]
import typing
def f() -> None:
x = ()
[builtins fixtures/primitives.pyi]
[out]
NameExpr(3) : Tuple[()]
TupleExpr(3) : Tuple[()]
-- Basic generics
-- --------------
[case testImplicitBoundTypeVarsForMethod]
## MemberExpr
from typing import TypeVar, Generic
T = TypeVar('T')
class A(Generic[T]):
def f(self) -> T: pass
class B: pass
def g() -> None:
a = None # type: A[B]
f = a.f
[out]
MemberExpr(9) : def () -> B
[case testImplicitBoundTypeVarsForSelfMethodReference]
from typing import TypeVar, Generic
T = TypeVar('T')
class A(Generic[T]):
def f(self) -> T:
return self.f()
[out]
CallExpr(5) : T`1
MemberExpr(5) : def () -> T`1
NameExpr(5) : A[T`1]
[case testGenericFunctionCallWithTypeApp-skip]
## CallExpr|TypeApplication|NameExpr
from typing import Any, TypeVar, Tuple
T = TypeVar('T')
class A: pass
f[A](A())
f[Any](A())
def f(a: T) -> Tuple[T, T]: pass
[builtins fixtures/tuple.pyi]
[out]
CallExpr(5) : A
CallExpr(5) : Tuple[A, A]
NameExpr(5) : def () -> A
NameExpr(5) : def (a: A) -> Tuple[A, A]
TypeApplication(5) : def (a: A) -> Tuple[A, A]
CallExpr(6) : A
CallExpr(6) : Tuple[Any, Any]
NameExpr(6) : def () -> A
NameExpr(6) : def (a: Any) -> Tuple[Any, Any]
TypeApplication(6) : def (a: Any) -> Tuple[Any, Any]
-- NOTE: Type applications are not supported for generic methods, so the
-- following test cases are commented out.
--[case testGenericMethodCallWithTypeApp]
--## CallExpr|MemberExpr|TypeApplication
--from typing import Any, TypeVar, Tuple
--T = TypeVar('T')
--class A:
-- def f(self, a: T) -> Tuple[T, T]: pass
--a.f[A](a)
--a.f[Any](a)
--a = None # type: A
--[builtins fixtures/tuple.py]
--[out]
--CallExpr(2) : Tuple[A, A]
--MemberExpr(2) : def (A a) -> Tuple[A, A]
--TypeApplication(2) : def (A a) -> Tuple[A, A]
--CallExpr(3) : Tuple[Any, Any]
--MemberExpr(3) : def (any a) -> Tuple[Any, Any]
--TypeApplication(3) : def (any a) -> Tuple[Any, Any]
--[case testGenericMethodCallInGenericTypeWithTypeApp]
--## CallExpr|MemberExpr|TypeApplication
--from typing import Any, TypeVar, Generic, Tuple
--T = TypeVar('T')
--S = TypeVar('S')
--class B: pass
--class C: pass
--a.f[B](b)
--a.f[Any](b)
--class A(Generic[T]):
-- def f(self, a: S) -> Tuple[T, S]: pass
--a = None # type: A[C]
--b = None # type: B
--[builtins fixtures/tuple.py]
--[out]
--CallExpr(6) : Tuple[C, B]
--MemberExpr(6) : def (B a) -> Tuple[C, B]
--TypeApplication(6) : def (B a) -> Tuple[C, B]
--CallExpr(7) : Tuple[C, Any]
--MemberExpr(7) : def (any a) -> Tuple[C, Any]
--TypeApplication(7) : def (any a) -> Tuple[C, Any]
[case testGenericTypeVariableInference]
from typing import TypeVar, Generic
T = TypeVar('T')
class A(Generic[T]):
def __init__(self, a: T) -> None: pass
class B: pass
A(A(B()))
[out]
CallExpr(6) : A[A[B]]
CallExpr(6) : A[B]
CallExpr(6) : B
NameExpr(6) : def (a: A[B]) -> A[A[B]]
NameExpr(6) : def (a: B) -> A[B]
NameExpr(6) : def () -> B
-- Generic inheritance
-- -------------------
[case testInheritedMethodReferenceWithGenericInheritance]
from typing import TypeVar, Generic
T = TypeVar('T')
class C: pass
class A(Generic[T]):
def f(self, a: T) -> None: pass
class B(A[C]):
def g(self, c: C) -> None:
self.f(c)
[out]
CallExpr(8) : None
MemberExpr(8) : def (a: C)
NameExpr(8) : C
NameExpr(8) : B
[case testInheritedMethodReferenceWithGenericSubclass]
from typing import TypeVar, Generic
S = TypeVar('S')
T = TypeVar('T')
class C: pass
class A(Generic[S, T]):
def f(self, a: C) -> None: pass
class B(A[C, T], Generic[T]):
def g(self, c: C) -> None:
self.f(c)
[out]
CallExpr(9) : None
MemberExpr(9) : def (a: C)
NameExpr(9) : C
NameExpr(9) : B[T`1]
[case testExternalReferenceWithGenericInheritance]
from typing import TypeVar, Generic
T = TypeVar('T')
class C: pass
class A(Generic[T]):
def f(self, a: T) -> None: pass
class B(A[C]): pass
b = None # type: B
c = None # type: C
b.f(c)
[out]
CallExpr(9) : None
MemberExpr(9) : def (a: C)
NameExpr(9) : B
NameExpr(9) : C
-- Implicit Any types
-- ------------------
[case testDynamicallyTypedFunction]
def f(x):
y = x + o
z = o
z
o = None # type: object
[out]
NameExpr(3) : builtins.object
NameExpr(3) : Any
NameExpr(3) : Any
OpExpr(3) : Any
NameExpr(4) : builtins.object
NameExpr(4) : Any
NameExpr(5) : Any
[case testDynamicallyTypedMethod]
class A:
def f(self, x):
y = (
o) # Place y and o on separate lines
x
y
o = None # type: object
[out]
NameExpr(4) : Any
NameExpr(5) : builtins.object
NameExpr(6) : Any
NameExpr(7) : Any
[case testDynamicallyTypedConstructor]
class A:
def __init__(self, x):
y = o
x
y
o = None # type: object
[out]
NameExpr(4) : builtins.object
NameExpr(4) : Any
NameExpr(5) : Any
NameExpr(6) : Any
[case testCallInDynamicallyTypedFunction]
def f():
g(o)
def g(a: object) -> object: pass
o = None # type: object
[out]
CallExpr(3) : Any
NameExpr(3) : def (a: builtins.object) -> builtins.object
NameExpr(3) : builtins.object
[case testExpressionInDynamicallyTypedFn]
import typing
def f():
x = None
x.f()
[out]
CallExpr(4) : Any
MemberExpr(4) : Any
NameExpr(4) : Any
[case testGenericCall]
from typing import TypeVar, Generic
T = TypeVar('T')
def f() -> None:
a1 = A(b) # type: A[B]
a2 = A(b) # type: A[object]
class A(Generic[T]):
def __init__(self, a: T) -> None: pass
class B: pass
b = None # type: B
[out]
CallExpr(4) : A[B]
NameExpr(4) : def (a: B) -> A[B]
NameExpr(4) : B
CallExpr(5) : A[builtins.object]
NameExpr(5) : def (a: builtins.object) -> A[builtins.object]
NameExpr(5) : B
[case testGenericCallInDynamicallyTypedFunction]
from typing import TypeVar, Generic
T = TypeVar('T')
def f():
A()
class A(Generic[T]): pass
[out]
CallExpr(4) : Any
NameExpr(4) : def [T] () -> A[T`1]
[case testGenericCallInDynamicallyTypedFunction2]
from typing import TypeVar, Generic
T = TypeVar('T')
def f():
A(f)
class A(Generic[T]):
def __init__(self, x: T) -> None: pass
[out]
CallExpr(4) : Any
NameExpr(4) : def [T] (x: T`1) -> A[T`1]
NameExpr(4) : def () -> Any
[case testGenericCallInDynamicallyTypedFunction3]
from typing import TypeVar
t = TypeVar('t')
def f():
g(None)
def g(x: t) -> t: pass
[out]
CallExpr(4) : Any
NameExpr(4) : def [t] (x: t`-1) -> t`-1
-- Generic types and type inference
-- --------------------------------
[case testInferenceInArgumentContext]
## CallExpr
from typing import TypeVar, Generic
T = TypeVar('T')
class A(Generic[T]): pass
class B: pass
class C(B): pass
def f(a: 'A[B]') -> None: pass
def g() -> 'A[T]': pass
def h(a: T) -> 'A[T]': pass
b = None # type: B
c = None # type: C
f(g())
f(h(b))
f(h(c))
[out]
CallExpr(14) : None
CallExpr(14) : A[B]
CallExpr(15) : None
CallExpr(15) : A[B]
CallExpr(16) : None
CallExpr(16) : A[B]
[case testInferGenericTypeForLocalVariable]
from typing import TypeVar, Generic
T = TypeVar('T')
def f() -> None:
a = A(b)
a
a2, a3 = A(b), A(c)
a2
a3
b = None # type: B
c = None # type: C
class A(Generic[T]):
def __init__(self, x: T) -> None: pass
class B: pass
class C: pass
[out]
CallExpr(4) : A[B]
NameExpr(4) : def (x: B) -> A[B]
NameExpr(4) : A[B]
NameExpr(4) : B
NameExpr(5) : A[B]
CallExpr(6) : A[B]
CallExpr(6) : A[C]
NameExpr(6) : def (x: B) -> A[B]
NameExpr(6) : def (x: C) -> A[C]
NameExpr(6) : A[B]
NameExpr(6) : A[C]
NameExpr(6) : B
NameExpr(6) : C
NameExpr(7) : A[B]
NameExpr(8) : A[C]
[case testNestedGenericCalls]
from typing import TypeVar, Generic
T = TypeVar('T')
S = TypeVar('S')
def h() -> None:
g(f(c))
c = None # type: C
class A(Generic[T]): pass
class B(Generic[T]): pass
class C: pass
def f(a: T) -> A[T]: pass
def g(a: S) -> B[S]: pass
[out]
CallExpr(5) : A[C]
CallExpr(5) : B[A[C]]
NameExpr(5) : C
NameExpr(5) : def (a: C) -> A[C]
NameExpr(5) : def (a: A[C]) -> B[A[C]]
[case testInferListLiterals]
from typing import List
a = [] # type: List[A]
class A: pass
[builtins fixtures/list.pyi]
[out]
ListExpr(2) : builtins.list[A]
[case testInferGenericTypeInTypeAnyContext]
from typing import Any
a = [] # type: Any
[builtins fixtures/list.pyi]
[out]
ListExpr(2) : builtins.list[Any]
[case testHigherOrderFunction]
from typing import TypeVar, Callable, List
t = TypeVar('t')
s = TypeVar('s')
def map(f: Callable[[t], s], a: List[t]) -> List[s]: pass
class A: pass
class B: pass
def f(a: A) -> B: pass
map(
f,
[A()])
[builtins fixtures/list.pyi]
[out]
CallExpr(8) : builtins.list[B]
NameExpr(8) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
NameExpr(9) : def (a: A) -> B
CallExpr(10) : A
ListExpr(10) : builtins.list[A]
NameExpr(10) : def () -> A
-- Lambdas
-- -------
[case testLambdaWithTypeInferredFromContext]
from typing import Callable
f = lambda x: x.a # type: Callable[[B], A]
class A: pass
class B:
a = None # type: A
[out]
LambdaExpr(2) : def (x: B) -> A
MemberExpr(2) : A
NameExpr(2) : B
[case testLambdaWithInferredType]
## LambdaExpr|NameExpr
import typing
f = lambda: 1
[out]
LambdaExpr(3) : def () -> Literal[1]?
NameExpr(3) : def () -> builtins.int
[case testLambdaWithInferredType2]
## LambdaExpr|NameExpr
import typing
f = lambda: [1]
[builtins fixtures/list.pyi]
[out]
LambdaExpr(3) : def () -> builtins.list[builtins.int]
NameExpr(3) : def () -> builtins.list[builtins.int]
[case testLambdaWithInferredType3]
from typing import List, Callable
f = lambda x: [] # type: Callable[[B], List[A]]
class A: pass
class B:
a = None # type: A
[builtins fixtures/list.pyi]
[out]
LambdaExpr(2) : def (x: B) -> builtins.list[A]
ListExpr(2) : builtins.list[A]
[case testLambdaAndHigherOrderFunction]
from typing import TypeVar, Callable, List
t = TypeVar('t')
s = TypeVar('s')
def map(f: Callable[[t], s], a: List[t]) -> List[s]: pass
class A: pass
class B: pass
def f(a: A) -> B: pass
l = None # type: List[A]
map(
lambda x: f(x), l)
[builtins fixtures/list.pyi]
[out]
CallExpr(9) : builtins.list[B]
NameExpr(9) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
CallExpr(10) : B
LambdaExpr(10) : def (x: A) -> B
NameExpr(10) : def (a: A) -> B
NameExpr(10) : builtins.list[A]
NameExpr(10) : A
[case testLambdaAndHigherOrderFunction2]
## LambdaExpr|NameExpr|ListExpr
from typing import TypeVar, List, Callable
t = TypeVar('t')
s = TypeVar('s')
def map(f: Callable[[t], List[s]], a: List[t]) -> List[s]: pass
class A: pass
class B: pass
def f(a: A) -> B: pass
l = None # type: List[A]
map(
lambda x: [f(x)], l)
[builtins fixtures/list.pyi]
[out]
NameExpr(10) : def (f: def (A) -> builtins.list[B], a: builtins.list[A]) -> builtins.list[B]
LambdaExpr(11) : def (x: A) -> builtins.list[B]
ListExpr(11) : builtins.list[B]
NameExpr(11) : def (a: A) -> B
NameExpr(11) : builtins.list[A]
NameExpr(11) : A
[case testLambdaInListAndHigherOrderFunction]
from typing import TypeVar, Callable, List
t = TypeVar('t')
s = TypeVar('s')
def map(f: List[Callable[[t], s]], a: List[t]) -> List[s]: pass
class A: pass
l = None # type: List[A]
map(
[lambda x: x],
l)
[builtins fixtures/list.pyi]
[out]
-- TODO We probably should not silently infer 'Any' types in statically typed
-- context. Perhaps just fail instead?
CallExpr(7) : builtins.list[Any]
NameExpr(7) : def (f: builtins.list[def (A) -> Any], a: builtins.list[A]) -> builtins.list[Any]
LambdaExpr(8) : def (x: A) -> A
ListExpr(8) : builtins.list[def (A) -> Any]
NameExpr(8) : A
NameExpr(9) : builtins.list[A]
[case testLambdaAndHigherOrderFunction3]
from typing import TypeVar, Callable, List
t = TypeVar('t')
s = TypeVar('s')
def map(f: Callable[[t], s], a: List[t]) -> List[s]: pass
class A:
b = None # type: B
class B: pass
l = None # type: List[A]
map(
lambda x: x.b,
l)
[builtins fixtures/list.pyi]
[out]
CallExpr(9) : builtins.list[B]
NameExpr(9) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
LambdaExpr(10) : def (x: A) -> B
MemberExpr(10) : B
NameExpr(10) : A
NameExpr(11) : builtins.list[A]
[case testLambdaAndHigherOrderFunctionAndKeywordArgs]
from typing import TypeVar, Callable, List
t = TypeVar('t')
s = TypeVar('s')
def map(f: Callable[[t], s], a: List[t]) -> List[s]: pass
class A:
b = None # type: B
class B: pass
l = None # type: List[A]
map(
a=l,
f=lambda x: x.b)
[builtins fixtures/list.pyi]
[out]
CallExpr(9) : builtins.list[B]
NameExpr(9) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B]
NameExpr(10) : builtins.list[A]
LambdaExpr(11) : def (x: A) -> B
MemberExpr(11) : B
NameExpr(11) : A
-- Boolean operations
-- ------------------
[case testBooleanOr]
from typing import List
a = None # type: List[A]
a or []
a = a or []
if int():
a = [] or a
class A: pass
[builtins fixtures/list.pyi]
[out]
ListExpr(3) : builtins.list[A]
NameExpr(3) : builtins.list[A]
OpExpr(3) : builtins.list[A]
ListExpr(4) : builtins.list[A]
NameExpr(4) : builtins.list[A]
NameExpr(4) : builtins.list[A]
OpExpr(4) : builtins.list[A]
CallExpr(5) : builtins.int
NameExpr(5) : def () -> builtins.int
ListExpr(6) : builtins.list[A]
NameExpr(6) : builtins.list[A]
NameExpr(6) : builtins.list[A]
OpExpr(6) : builtins.list[A]
-- Class attributes
-- ----------------
[case testUnboundMethod]
## MemberExpr
import typing
class A:
def f(self) -> None: pass
A.f
[out]
MemberExpr(5) : def (self: A)
[case testUnboundMethodWithImplicitSig]
## MemberExpr
import typing
class A:
def f(self): pass
A.f
[out]
MemberExpr(5) : def (self: A) -> Any
[case testOverloadedUnboundMethod]
## MemberExpr
from typing import overload
class A:
@overload
def f(self) -> None: pass
@overload
def f(self, __x: object) -> None: pass
def f(self, *args) -> None: pass
A.f
[builtins fixtures/tuple.pyi]
[out]
MemberExpr(10) : Overload(def (self: A), def (self: A, builtins.object))
[case testOverloadedUnboundMethodWithImplicitSig]
## MemberExpr
from typing import overload
class A:
@overload
def f(self): pass
@overload
def f(self, __x): pass
def f(self, *args): pass
A.f
[builtins fixtures/tuple.pyi]
[out]
MemberExpr(10) : Overload(def (self: A) -> Any, def (self: A, Any) -> Any)
[case testUnboundMethodWithInheritance]
## MemberExpr
import typing
class A:
def __init__(self) -> None: pass
def f(self) -> None: pass
class B(A):
pass
B.f
[out]
MemberExpr(8) : def (self: A)
[case testUnboundGenericMethod]
## MemberExpr
from typing import TypeVar
t = TypeVar('t')
class B: pass
class A:
def f(self, x: t) -> None: pass
A.f(A(), B())
[out]
MemberExpr(7) : def (self: A, x: B)
[case testUnboundMethodOfGenericClass]
## MemberExpr
from typing import TypeVar, Generic
t = TypeVar('t')
class B: pass
class A(Generic[t]):
def f(self, x: t) -> None: pass
A.f
a_b = A() # type: A[B]
A.f(a_b, B())
[out]
MemberExpr(7) : def [t] (self: A[t`1], x: t`1)
MemberExpr(9) : def (self: A[B], x: B)
[case testUnboundOverloadedMethodOfGenericClass]
## CallExpr
from typing import TypeVar, Generic, overload
t = TypeVar('t')
class B: pass
class A(Generic[t]):
@overload
def f(self, x: t) -> t: pass
@overload
def f(self) -> object: pass
def f(self, *args): pass
ab, b = None, None # type: (A[B], B)
A.f(ab, b)
[builtins fixtures/tuple.pyi]