forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-overloading.test
4947 lines (3972 loc) · 131 KB
/
check-overloading.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 function overloading
[case testOverloadNotImportedNoCrash]
@overload
def f(a): pass
@overload
def f(a): pass
def f(a): pass
f(0)
@overload # E: Name 'overload' is not defined
def g(a:int): pass
def g(a): pass # E: Name 'g' already defined on line 8
g(0)
@something # E: Name 'something' is not defined
def r(a:int): pass
def r(a): pass # E: Name 'r' already defined on line 13
r(0)
[out]
main:1: error: Name 'overload' is not defined
main:3: error: Name 'f' already defined on line 1
main:3: error: Name 'overload' is not defined
main:5: error: Name 'f' already defined on line 1
[case testTypeCheckOverloadWithImplementation]
from typing import overload, Any
@overload
def f(x: 'A') -> 'B': ...
@overload
def f(x: 'B') -> 'A': ...
def f(x: Any) -> Any:
pass
reveal_type(f(A())) # E: Revealed type is '__main__.B'
reveal_type(f(B())) # E: Revealed type is '__main__.A'
class A: pass
class B: pass
[builtins fixtures/isinstance.pyi]
[case testOverloadNeedsImplementation]
from typing import overload, Any
@overload # E: An overloaded function outside a stub file must have an implementation
def f(x: 'A') -> 'B': ...
@overload
def f(x: 'B') -> 'A': ...
reveal_type(f(A())) # E: Revealed type is '__main__.B'
reveal_type(f(B())) # E: Revealed type is '__main__.A'
class A: pass
class B: pass
[builtins fixtures/isinstance.pyi]
[case testSingleOverloadNoImplementation]
from typing import overload, Any
@overload # E: Single overload definition, multiple required
def f(x: 'A') -> 'B': ...
class A: pass
class B: pass
[builtins fixtures/isinstance.pyi]
[case testOverloadByAnyOtherName]
from typing import overload as rose
from typing import Any
@rose
def f(x: 'A') -> 'B': ...
@rose
def f(x: 'B') -> 'A': ...
def f(x: Any) -> Any:
pass
reveal_type(f(A())) # E: Revealed type is '__main__.B'
reveal_type(f(B())) # E: Revealed type is '__main__.A'
class A: pass
class B: pass
[builtins fixtures/isinstance.pyi]
[case testTypeCheckOverloadWithDecoratedImplementation]
from typing import overload, Any
def deco(fun): ...
@overload
def f(x: 'A') -> 'B': ...
@overload
def f(x: 'B') -> 'A': ...
@deco
def f(x: Any) -> Any:
pass
reveal_type(f(A())) # E: Revealed type is '__main__.B'
reveal_type(f(B())) # E: Revealed type is '__main__.A'
class A: pass
class B: pass
[builtins fixtures/isinstance.pyi]
[case testOverloadDecoratedImplementationNotLast]
from typing import overload, Any
def deco(fun): ...
@overload
def f(x: 'A') -> 'B': ...
@deco # E: The implementation for an overloaded function must come last
def f(x: Any) -> Any:
pass
@overload
def f(x: 'B') -> 'A': ...
class A: pass
class B: pass
[builtins fixtures/isinstance.pyi]
[case testOverloadImplementationNotLast]
from typing import overload, Any
@overload
def f(x: 'A') -> 'B': ...
def f(x: Any) -> Any: # E: The implementation for an overloaded function must come last
pass
@overload
def f(x: 'B') -> 'A': ...
class A: pass
class B: pass
[builtins fixtures/isinstance.pyi]
[case testDecoratedRedefinitionIsNotOverload]
from typing import overload, Any
def deco(fun): ...
@deco
def f(x: 'A') -> 'B': ...
@deco # E: Name 'f' already defined on line 5
def f(x: 'B') -> 'A': ...
@deco # E: Name 'f' already defined on line 5
def f(x: Any) -> Any: ...
class A: pass
class B: pass
[builtins fixtures/isinstance.pyi]
[case testTypeCheckOverloadWithImplementationPy2]
# flags: --python-version 2.7
from typing import overload
@overload
def f(x):
# type: (A) -> B
pass
@overload
def f(x):
# type: (B) -> A
pass
def f(x):
pass
reveal_type(f(A())) # E: Revealed type is '__main__.B'
reveal_type(f(B())) # E: Revealed type is '__main__.A'
class A: pass
class B: pass
[builtins fixtures/isinstance.pyi]
[case testTypeCheckOverloadWithImplementationError]
from typing import overload, Any
@overload
def f(x: 'A') -> 'B': ...
@overload
def f(x: 'B') -> 'A': ...
def f(x: Any) -> Any:
foo = 1
foo = "bar" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
@overload
def g(x: 'A') -> 'B': ...
@overload
def g(x: 'B') -> 'A': ...
def g(x):
foo = 1
foo = "bar"
reveal_type(f(A())) # E: Revealed type is '__main__.B'
reveal_type(f(B())) # E: Revealed type is '__main__.A'
class A: pass
class B: pass
[builtins fixtures/isinstance.pyi]
[case testTypeCheckOverloadWithUntypedImplAndMultipleVariants]
from typing import overload
@overload
def f(x: int) -> str: ...
@overload
def f(x: str) -> int: ... # E: Overloaded function signatures 2 and 3 overlap with incompatible return types
@overload
def f(x: object) -> str: ...
def f(x): ...
[case testTypeCheckOverloadWithImplTooSpecificArg]
from typing import overload, Any
class A: pass
class B: pass
a = A()
@overload
def f(x: 'A') -> 'B': ...
@overload
def f(x: 'B') -> 'A': ...
def f(x: 'A') -> Any: # E: Overloaded function implementation does not accept all possible arguments of signature 2
pass
reveal_type(f(A())) # E: Revealed type is '__main__.B'
reveal_type(f(B())) # E: Revealed type is '__main__.A'
[builtins fixtures/isinstance.pyi]
[case testTypeCheckOverloadWithImplTooSpecificRetType]
from typing import overload, Any
class A: pass
class B: pass
a = A()
@overload
def f(x: 'A') -> 'B': ...
@overload
def f(x: 'B') -> 'A': ...
def f(x: Any) -> 'B': # E: Overloaded function implementation cannot produce return type of signature 2
return B()
reveal_type(f(A())) # E: Revealed type is '__main__.B'
reveal_type(f(B())) # E: Revealed type is '__main__.A'
[builtins fixtures/isinstance.pyi]
[case testTypeCheckOverloadWithImplTypeVar]
from typing import overload, Any, TypeVar
T = TypeVar('T')
class A: pass
class B: pass
a = A()
@overload
def f(x: 'A') -> 'A': ...
@overload
def f(x: 'B') -> 'B': ...
def f(x: T) -> T:
...
reveal_type(f(A())) # E: Revealed type is '__main__.A'
reveal_type(f(B())) # E: Revealed type is '__main__.B'
[builtins fixtures/isinstance.pyi]
[case testTypeCheckOverloadWithImplTypeVarProblems]
from typing import overload, Any, TypeVar, Union
T = TypeVar('T', bound='A')
class A: pass
class B: pass
a = A()
@overload
def f(x: 'A') -> 'A': ...
@overload
def f(x: 'B') -> 'B': ...
def f(x: Union[T, B]) -> T: # E: Overloaded function implementation cannot satisfy signature 2 due to inconsistencies in how they use type variables
...
reveal_type(f(A())) # E: Revealed type is '__main__.A'
reveal_type(f(B())) # E: Revealed type is '__main__.B'
[builtins fixtures/isinstance.pyi]
[case testTypeCheckOverloadImplementationTypeVarWithValueRestriction]
from typing import overload, TypeVar, Union
class A: pass
class B: pass
class C: pass
T = TypeVar('T', A, B)
@overload
def foo(x: T) -> T: ...
@overload
def foo(x: C) -> int: ...
def foo(x: Union[A, B, C]) -> Union[A, B, int]:
if isinstance(x, C):
return 3
else:
return x
@overload
def bar(x: T) -> T: ...
@overload
def bar(x: C) -> int: ...
def bar(x: Union[T, C]) -> Union[T, int]:
if isinstance(x, C):
return 3
else:
return x
[builtins fixtures/isinstancelist.pyi]
[case testTypeCheckOverloadImplementationTypeVarDifferingUsage1]
from typing import overload, Union, List, TypeVar, Generic
T = TypeVar('T')
@overload
def foo(t: List[T]) -> T: ...
@overload
def foo(t: T) -> T: ...
def foo(t: Union[List[T], T]) -> T:
if isinstance(t, list):
return t[0]
else:
return t
class Wrapper(Generic[T]):
@overload
def foo(self, t: List[T]) -> T: ...
@overload
def foo(self, t: T) -> T: ...
def foo(self, t: Union[List[T], T]) -> T:
if isinstance(t, list):
return t[0]
else:
return t
[builtins fixtures/isinstancelist.pyi]
[case testTypeCheckOverloadImplementationTypeVarDifferingUsage2]
from typing import overload, Union, List, TypeVar, Generic
T = TypeVar('T')
# Note: this is unsafe when T = object
@overload
def foo(t: List[T], s: T) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
@overload
def foo(t: T, s: T) -> str: ...
def foo(t, s): pass
class Wrapper(Generic[T]):
@overload
def foo(self, t: List[T], s: T) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
@overload
def foo(self, t: T, s: T) -> str: ...
def foo(self, t, s): pass
class Dummy(Generic[T]): pass
# Same root issue: why does the additional constraint bound T <: T
# cause the constraint solver to not infer T = object like it did in the
# first example?
@overload
def bar(d: Dummy[T], t: List[T], s: T) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
@overload
def bar(d: Dummy[T], t: T, s: T) -> str: ...
def bar(d: Dummy[T], t, s): pass
[builtins fixtures/isinstancelist.pyi]
[case testTypeCheckOverloadedFunctionBody]
from foo import *
[file foo.pyi]
from typing import overload
@overload
def f(x: 'A'):
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
x = A()
@overload
def f(x: 'B'):
x = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B")
x = B()
class A: pass
class B: pass
[out]
[case testTypeCheckOverloadedMethodBody]
from foo import *
[file foo.pyi]
from typing import overload
class A:
@overload
def f(self, x: 'A'):
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
x = A()
@overload
def f(self, x: 'B'):
x = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B")
x = B()
class B: pass
[out]
[case testCallToOverloadedFunction]
from foo import *
[file foo.pyi]
from typing import overload
f(C()) # E: No overload variant of "f" matches argument type "C" \
# N: Possible overload variants: \
# N: def f(x: A) -> None \
# N: def f(x: B) -> None
f(A())
f(B())
@overload
def f(x: 'A') -> None: pass
@overload
def f(x: 'B') -> None: pass
class A: pass
class B: pass
class C: pass
[case testOverloadedFunctionReturnValue]
from foo import *
[file foo.pyi]
from typing import overload
a, b = None, None # type: (A, B)
b = f(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
a = f(b) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
a = f(a)
b = f(b)
@overload
def f(x: 'A') -> 'A': pass
@overload
def f(x: 'B') -> 'B': pass
class A: pass
class B: pass
[case testCallToOverloadedMethod]
from foo import *
[file foo.pyi]
from typing import overload
A().f(C()) # E: No overload variant of "f" of "A" matches argument type "C" \
# N: Possible overload variants: \
# N: def f(self, x: A) -> None \
# N: def f(self, x: B) -> None
A().f(A())
A().f(B())
class A:
@overload
def f(self, x: 'A') -> None: pass
@overload
def f(self, x: 'B') -> None: pass
class B: pass
class C: pass
[case testOverloadedMethodReturnValue]
from foo import *
[file foo.pyi]
from typing import overload
a, b = None, None # type: (A, B)
b = a.f(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
a = a.f(b) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
a = a.f(a)
b = a.f(b)
class A:
@overload
def f(self, x: 'A') -> 'A': pass
@overload
def f(self, x: 'B') -> 'B': pass
class B: pass
[case testOverloadsWithDifferentArgumentCounts]
from foo import *
[file foo.pyi]
from typing import overload
a, b = None, None # type: (A, B)
a = f(a)
b = f(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
f(b) # E: No overload variant of "f" matches argument type "B" \
# N: Possible overload variant: \
# N: def f(x: A) -> A \
# N: <1 more non-matching overload not shown>
b = f(b, a)
a = f(b, a) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
f(a, a) # E: No overload variant of "f" matches argument types "A", "A" \
# N: Possible overload variant: \
# N: def f(x: B, y: A) -> B \
# N: <1 more non-matching overload not shown>
f(b, b) # E: No overload variant of "f" matches argument types "B", "B" \
# N: Possible overload variant: \
# N: def f(x: B, y: A) -> B \
# N: <1 more non-matching overload not shown>
@overload
def f(x: 'A') -> 'A': pass
@overload
def f(x: 'B', y: 'A') -> 'B': pass
class A: pass
class B: pass
[case testGenericOverloadVariant]
from foo import *
[file foo.pyi]
from typing import overload, TypeVar, Generic
t = TypeVar('t')
ab, ac, b, c = None, None, None, None # type: (A[B], A[C], B, C)
b = f(ab)
c = f(ac)
b = f(ac) # E: Incompatible types in assignment (expression has type "C", variable has type "B")
b = f(b)
c = f(b) # E: Incompatible types in assignment (expression has type "B", variable has type "C")
@overload
def f(x: 'A[t]') -> t: pass
@overload
def f(x: 'B') -> 'B': pass
class A(Generic[t]): pass
class B: pass
class C: pass
[case testOverloadedInit]
from foo import *
[file foo.pyi]
from typing import overload
a, b = None, None # type: (A, B)
a = A(a)
a = A(b)
a = A(object()) # E: No overload variant of "A" matches argument type "object" \
# N: Possible overload variants: \
# N: def __init__(self, a: A) -> A \
# N: def __init__(self, b: B) -> A
class A:
@overload
def __init__(self, a: 'A') -> None: pass
@overload
def __init__(self, b: 'B') -> None: pass
class B: pass
[case testIntersectionTypeCompatibility]
from foo import *
[file foo.pyi]
from typing import overload, Callable
o = None # type: object
a = None # type: A
a = f # E: Incompatible types in assignment (expression has type overloaded function, variable has type "A")
o = f
@overload
def f(a: 'A') -> None: pass
@overload
def f(a: Callable[[], None]) -> None: pass
class A: pass
[case testCompatibilityOfIntersectionTypeObjectWithStdType]
from foo import *
[file foo.pyi]
from typing import overload
t, a = None, None # type: (type, A)
a = A # E: Incompatible types in assignment (expression has type "Type[A]", variable has type "A")
t = A
class A:
@overload
def __init__(self, a: 'A') -> None: pass
@overload
def __init__(self, a: 'B') -> None: pass
class B: pass
[case testOverloadedGetitem]
from foo import *
[file foo.pyi]
from typing import overload
a, b = None, None # type: int, str
a = A()[a]
b = A()[a] # E: Incompatible types in assignment (expression has type "int", variable has type "str")
b = A()[b]
a = A()[b] # E: Incompatible types in assignment (expression has type "str", variable has type "int")
class A:
@overload
def __getitem__(self, a: int) -> int: pass
@overload
def __getitem__(self, b: str) -> str: pass
[case testOverloadedGetitemWithGenerics]
from foo import *
[file foo.pyi]
from typing import TypeVar, Generic, overload
t = TypeVar('t')
a, b, c = None, None, None # type: (A, B, C[A])
a = c[a]
b = c[a] # E: Incompatible types in assignment (expression has type "A", variable has type "B")
a = c[b]
b = c[b] # E: Incompatible types in assignment (expression has type "A", variable has type "B")
class C(Generic[t]):
@overload
def __getitem__(self, a: 'A') -> t: pass
@overload
def __getitem__(self, b: 'B') -> t: pass
class A: pass
class B: pass
[case testImplementingOverloadedMethod]
from foo import *
[file foo.pyi]
from typing import overload
from abc import abstractmethod, ABCMeta
class I(metaclass=ABCMeta):
@overload
@abstractmethod
def f(self) -> None: pass
@overload
@abstractmethod
def f(self, a: 'A') -> None: pass
class A(I):
@overload
def f(self) -> None: pass
@overload
def f(self, a: 'A') -> None: pass
[case testOverloadWithFunctionType]
from foo import *
[file foo.pyi]
from typing import overload, Callable
class A: pass
@overload
def f(x: A) -> None: pass
@overload
def f(x: Callable[[], None]) -> None: pass
f(A())
[builtins fixtures/function.pyi]
[case testVarArgsOverload]
from foo import *
[file foo.pyi]
from typing import overload, Any
@overload
def f(x: 'A', *more: Any) -> 'A': pass
@overload
def f(x: 'B', *more: Any) -> 'A': pass
f(A())
f(A(), A, A)
f(B())
f(B(), B)
f(B(), B, B)
f(object()) # E: No overload variant of "f" matches argument type "object" \
# N: Possible overload variants: \
# N: def f(x: A, *more: Any) -> A \
# N: def f(x: B, *more: Any) -> A
class A: pass
class B: pass
[builtins fixtures/list.pyi]
[case testVarArgsOverload2]
from foo import *
[file foo.pyi]
from typing import overload
@overload
def f(x: 'A', *more: 'B') -> 'A': pass
@overload
def f(x: 'B', *more: 'A') -> 'A': pass
f(A(), B())
f(A(), B(), B())
f(A(), A(), B()) # E: No overload variant of "f" matches argument types "A", "A", "B" \
# N: Possible overload variants: \
# N: def f(x: A, *more: B) -> A \
# N: def f(x: B, *more: A) -> A
f(A(), B(), A()) # E: No overload variant of "f" matches argument types "A", "B", "A" \
# N: Possible overload variants: \
# N: def f(x: A, *more: B) -> A \
# N: def f(x: B, *more: A) -> A
class A: pass
class B: pass
[builtins fixtures/list.pyi]
[case testOverloadWithTypeObject]
from foo import *
[file foo.pyi]
from typing import overload
@overload
def f(a: 'A', t: type) -> None: pass
@overload
def f(a: 'B', t: type) -> None: pass
f(A(), B)
f(B(), A)
class A: pass
class B: pass
[builtins fixtures/function.pyi]
[case testOverloadedInitAndTypeObjectInOverload]
from foo import *
[file foo.pyi]
from typing import overload
@overload
def f(t: type) -> 'A': pass
@overload
def f(t: 'A') -> 'B': pass
a, b = None, None # type: (A, B)
a = f(A)
b = f(a)
b = f(A) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
a = f(a) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
class A:
@overload
def __init__(self) -> None: pass
@overload
def __init__(self, a: 'A') -> None: pass
class B:
pass
[case testOverlappingErasedSignatures]
from foo import *
[file foo.pyi]
from typing import overload, List
@overload
def f(a: List[int]) -> int: pass
@overload
def f(a: List[str]) -> int: pass
list_int = [] # type: List[int]
list_str = [] # type: List[str]
list_object = [] # type: List[object]
n = f(list_int)
m = f(list_str)
n = 1
m = 1
n = 'x' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
m = 'x' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
f(list_object) # E: Argument 1 to "f" has incompatible type "List[object]"; expected "List[int]"
[builtins fixtures/list.pyi]
[case testOverlappingOverloadSignatures]
from foo import *
[file foo.pyi]
from typing import overload
class A: pass
class B(A): pass
@overload
def f(x: B) -> int: pass # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
@overload
def f(x: A) -> str: pass
[case testContravariantOverlappingOverloadSignatures]
from foo import *
[file foo.pyi]
from typing import overload
class A: pass
class B(A): pass
@overload
def f(x: A) -> A: pass
@overload
def f(x: B) -> B: pass # E: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader
[case testPartiallyCovariantOverlappingOverloadSignatures]
from foo import *
[file foo.pyi]
from typing import overload
class A: pass
class B(A): pass
@overload
def f(x: B) -> A: pass # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
@overload
def f(x: A) -> B: pass
[case testPartiallyContravariantOverloadSignatures]
from foo import *
[file foo.pyi]
from typing import overload
class A: pass
class B(A): pass
@overload
def g(x: A) -> int: pass
@overload
def g(x: B) -> str: pass # E: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader
[case testCovariantOverlappingOverloadSignatures]
from foo import *
[file foo.pyi]
from typing import overload
class A: pass
class B(A): pass
@overload
def g(x: B) -> B: pass
@overload
def g(x: A) -> A: pass
[case testCovariantOverlappingOverloadSignaturesWithSomeSameArgTypes]
from foo import *
[file foo.pyi]
from typing import overload
class A: pass
class B(A): pass
@overload
def g(x: int, y: B) -> B: pass
@overload
def g(x: int, y: A) -> A: pass
[case testCovariantOverlappingOverloadSignaturesWithAnyType]
from foo import *
[file foo.pyi]
from typing import Any, overload
@overload
def g(x: int) -> int: pass
@overload
def g(x: Any) -> Any: pass
[case testContravariantOverlappingOverloadSignaturesWithAnyType]
from foo import *
[file foo.pyi]
from typing import Any, overload
@overload
def g(x: Any) -> Any: pass
@overload
def g(x: int) -> int: pass # E: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader
[case testOverloadedLtAndGtMethods]
from foo import *
[file foo.pyi]
from typing import overload
class A:
def __lt__(self, x: A) -> int: pass
def __gt__(self, x: A) -> int: pass
class B:
@overload
def __lt__(self, x: B) -> int: pass
@overload
def __lt__(self, x: A) -> int: pass
@overload
def __gt__(self, x: B) -> int: pass
@overload
def __gt__(self, x: A) -> int: pass
A() < A()
A() < B()
B() < A()
B() < B()
A() < object() # E: Unsupported operand types for < ("A" and "object")
B() < object() # E: No overload variant of "__lt__" of "B" matches argument type "object" \
# N: Possible overload variants: \
# N: def __lt__(self, B) -> int \
# N: def __lt__(self, A) -> int
[case testOverloadedForwardMethodAndCallingReverseMethod]
from foo import *
[file foo.pyi]
from typing import overload
class A:
@overload
def __add__(self, x: 'A') -> int: pass
@overload
def __add__(self, x: int) -> int: pass
class B:
def __radd__(self, x: A) -> int: pass
A() + A()
A() + 1
A() + B()
A() + '' # E: No overload variant of "__add__" of "A" matches argument type "str" \
# N: Possible overload variants: \
# N: def __add__(self, A) -> int \
# N: def __add__(self, int) -> int
[case testOverrideOverloadSwapped]
from foo import *
[file foo.pyi]
from typing import overload
class Parent:
@overload
def f(self, x: int) -> int: ...
@overload
def f(self, x: str) -> str: ...
class Child(Parent):
@overload # E: Signature of "f" incompatible with supertype "Parent" \
# N: Overload variants must be defined in the same order as they are in "Parent"
def f(self, x: str) -> str: ...
@overload
def f(self, x: int) -> int: ...
[case testOverrideOverloadSwappedWithExtraVariants]
from foo import *
[file foo.pyi]
from typing import overload
class bool: pass
class Parent:
@overload
def f(self, x: int) -> int: ...
@overload
def f(self, x: str) -> str: ...
class Child1(Parent):
@overload # E: Signature of "f" incompatible with supertype "Parent" \
# N: Overload variants must be defined in the same order as they are in "Parent"
def f(self, x: bool) -> bool: ...
@overload
def f(self, x: str) -> str: ...
@overload
def f(self, x: int) -> int: ...
class Child2(Parent):
@overload # E: Signature of "f" incompatible with supertype "Parent" \
# N: Overload variants must be defined in the same order as they are in "Parent"
def f(self, x: str) -> str: ...
@overload
def f(self, x: bool) -> bool: ...
@overload
def f(self, x: int) -> int: ...
class Child3(Parent):
@overload # E: Signature of "f" incompatible with supertype "Parent" \
# N: Overload variants must be defined in the same order as they are in "Parent"
def f(self, x: str) -> str: ...
@overload
def f(self, x: int) -> int: ...
@overload
def f(self, x: bool) -> bool: ...
[case testOverrideOverloadSwappedWithAdjustedVariants]
from foo import *
[file foo.pyi]
from typing import overload
class A: pass
class B(A): pass
class C(B): pass
class Parent:
@overload
def f(self, x: int) -> int: ...
@overload
def f(self, x: B) -> B: ...
class Child1(Parent):
@overload # E: Signature of "f" incompatible with supertype "Parent" \
# N: Overload variants must be defined in the same order as they are in "Parent"
def f(self, x: A) -> B: ...
@overload
def f(self, x: int) -> int: ...
class Child2(Parent):
@overload # E: Signature of "f" incompatible with supertype "Parent" \
# N: Overload variants must be defined in the same order as they are in "Parent"
def f(self, x: B) -> C: ...
@overload
def f(self, x: int) -> int: ...
class Child3(Parent):
@overload # E: Signature of "f" incompatible with supertype "Parent"
def f(self, x: B) -> A: ...
@overload
def f(self, x: int) -> int: ...
[case testOverrideOverloadedMethodWithMoreGeneralArgumentTypes]
from foo import *
[file foo.pyi]
from typing import overload
class IntSub(int): pass
class StrSub(str): pass
class A:
@overload
def f(self, x: IntSub) -> int: return 0
@overload
def f(self, x: StrSub) -> str: return ''
class B(A):
@overload
def f(self, x: int) -> int: return 0
@overload
def f(self, x: str) -> str: return ''
[out]
[case testOverrideOverloadedMethodWithMoreSpecificArgumentTypes]