-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
check-selftype.test
2216 lines (1701 loc) · 63.4 KB
/
check-selftype.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
[case testSelfTypeInstance]
from typing import TypeVar
T = TypeVar('T', bound='A', covariant=True)
class A:
def copy(self: T) -> T: pass
class B(A):
pass
reveal_type(A().copy) # N: Revealed type is "def () -> __main__.A"
reveal_type(B().copy) # N: Revealed type is "def () -> __main__.B"
reveal_type(A().copy()) # N: Revealed type is "__main__.A"
reveal_type(B().copy()) # N: Revealed type is "__main__.B"
[builtins fixtures/bool.pyi]
[case testSelfTypeStaticAccess]
from typing import TypeVar
T = TypeVar('T', bound='A', covariant=True)
class A:
def copy(self: T) -> T: pass
class B(A):
pass
# Erased instances appear on reveal_type; unrelated to self type
def f(a: A) -> None: pass
f(A.copy(A()))
f(A.copy(B()))
f(B.copy(B()))
# TODO: make it an error
# f(B.copy(A()))
def g(a: B) -> None: pass
g(A.copy(A())) # E: Argument 1 to "g" has incompatible type "A"; expected "B"
g(A.copy(B()))
g(B.copy(B()))
[builtins fixtures/bool.pyi]
[case testSelfTypeReturn]
from typing import TypeVar, Type
R = TypeVar('R')
def _type(self: R) -> Type[R]: pass
T = TypeVar('T', bound='A', covariant=True)
class A:
def copy(self: T) -> T:
if B():
return A() # E: Incompatible return value type (got "A", expected "T")
elif A():
return B() # E: Incompatible return value type (got "B", expected "T")
reveal_type(_type(self)) # N: Revealed type is "Type[T`-1]"
return reveal_type(_type(self)()) # N: Revealed type is "T`-1"
class B(A):
pass
Q = TypeVar('Q', bound='C', covariant=True)
class C:
def __init__(self, a: int) -> None: pass
def copy(self: Q) -> Q:
if self:
return reveal_type(_type(self)(1)) # N: Revealed type is "Q`-1"
else:
return _type(self)() # E: Missing positional argument "a" in call to "C"
[builtins fixtures/bool.pyi]
[case testSelfTypeClass]
from typing import TypeVar, Type
T = TypeVar('T', bound='A')
class A:
@classmethod
def new(cls: Type[T]) -> T:
return reveal_type(cls()) # N: Revealed type is "T`-1"
class B(A):
pass
Q = TypeVar('Q', bound='C', covariant=True)
class C:
def __init__(self, a: int) -> None: pass
@classmethod
def new(cls: Type[Q]) -> Q:
if cls:
return cls(1)
else:
return cls() # E: Missing positional argument "a" in call to "C"
reveal_type(A.new) # N: Revealed type is "def () -> __main__.A"
reveal_type(B.new) # N: Revealed type is "def () -> __main__.B"
reveal_type(A.new()) # N: Revealed type is "__main__.A"
reveal_type(B.new()) # N: Revealed type is "__main__.B"
[builtins fixtures/classmethod.pyi]
[case testSelfTypeOverride]
from typing import TypeVar, cast
T = TypeVar('T', bound='A', covariant=True)
class A:
def copy(self: T) -> T: pass
class B(A):
pass
Q = TypeVar('Q', bound='C', covariant=True)
class C(A):
def copy(self: Q) -> Q: pass
reveal_type(C().copy) # N: Revealed type is "def () -> __main__.C"
reveal_type(C().copy()) # N: Revealed type is "__main__.C"
reveal_type(cast(A, C()).copy) # N: Revealed type is "def () -> __main__.A"
reveal_type(cast(A, C()).copy()) # N: Revealed type is "__main__.A"
[builtins fixtures/bool.pyi]
[case testSelfTypeOverrideCompatibility]
from typing import overload, TypeVar, Generic
T = TypeVar("T")
class A(Generic[T]):
@overload
def f(self: A[int]) -> int: ...
@overload
def f(self: A[str]) -> str: ...
def f(self): ...
class B(A[T]):
@overload
def f(self: A[int]) -> int: ...
@overload
def f(self: A[str]) -> str: ...
def f(self): ...
class B2(A[T]):
@overload
def f(self: A[int]) -> int: ...
@overload
def f(self: A[str]) -> str: ...
@overload
def f(self: A[bytes]) -> bytes: ...
def f(self): ...
class C(A[int]):
def f(self) -> int: ...
class D(A[str]):
def f(self) -> int: ... # E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: @overload \
# N: def f(self) -> str \
# N: Subclass: \
# N: def f(self) -> int
class E(A[T]):
def f(self) -> int: ... # E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: @overload \
# N: def f(self) -> int \
# N: @overload \
# N: def f(self) -> str \
# N: Subclass: \
# N: def f(self) -> int
class F(A[bytes]):
# Note there's an argument to be made that this is actually compatible with the supertype
def f(self) -> bytes: ... # E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: @overload \
# N: def f(self) -> int \
# N: @overload \
# N: def f(self) -> str \
# N: Subclass: \
# N: def f(self) -> bytes
class G(A):
def f(self): ...
class H(A[int]):
def f(self): ...
class I(A[int]):
def f(*args): ...
class J(A[int]):
def f(self, arg) -> int: ... # E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: @overload \
# N: def f(self) -> int \
# N: Subclass: \
# N: def f(self, arg: Any) -> int
[builtins fixtures/tuple.pyi]
[case testSelfTypeOverrideCompatibilityGeneric]
from typing import TypeVar, Generic, overload
T = TypeVar("T", str, int, None)
class A(Generic[T]):
@overload
def f(self, s: T) -> T: ...
@overload
def f(self: A[str], s: bytes) -> str: ...
def f(self, s: object): ...
class B(A[int]):
def f(self, s: int) -> int: ...
class C(A[None]):
def f(self, s: int) -> int: ... # E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: @overload \
# N: def f(self, s: None) -> None \
# N: Subclass: \
# N: def f(self, s: int) -> int
[builtins fixtures/tuple.pyi]
[case testSelfTypeOverrideCompatibilityTypeVar]
from typing import overload, TypeVar, Union
AT = TypeVar("AT", bound="A")
class A:
@overload
def f(self: AT, x: int) -> AT: ...
@overload
def f(self, x: str) -> None: ...
@overload
def f(self: AT) -> bytes: ...
def f(*a, **kw): ...
class B(A):
@overload # E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: @overload \
# N: def f(self, x: int) -> B \
# N: @overload \
# N: def f(self, x: str) -> None \
# N: @overload \
# N: def f(self) -> bytes \
# N: Subclass: \
# N: @overload \
# N: def f(self, x: int) -> B \
# N: @overload \
# N: def f(self, x: str) -> None
def f(self, x: int) -> B: ...
@overload
def f(self, x: str) -> None: ...
def f(*a, **kw): ...
[builtins fixtures/dict.pyi]
[case testSelfTypeOverrideCompatibilitySelfTypeVar]
from typing import Any, Generic, Self, TypeVar, overload
T_co = TypeVar('T_co', covariant=True)
class Config(Generic[T_co]):
@overload
def get(self, instance: None) -> Self: ...
@overload
def get(self, instance: Any) -> T_co: ...
def get(self, *a, **kw): ...
class MultiConfig(Config[T_co]):
@overload
def get(self, instance: None) -> Self: ...
@overload
def get(self, instance: Any) -> T_co: ...
def get(self, *a, **kw): ...
[builtins fixtures/dict.pyi]
[case testSelfTypeSuper]
from typing import TypeVar, cast
T = TypeVar('T', bound='A', covariant=True)
class A:
def copy(self: T) -> T: pass
Q = TypeVar('Q', bound='B', covariant=True)
class B(A):
def copy(self: Q) -> Q:
reveal_type(self) # N: Revealed type is "Q`-1"
reveal_type(super().copy) # N: Revealed type is "def () -> Q`-1"
return super().copy()
[builtins fixtures/bool.pyi]
[case testSelfTypeRecursiveBinding]
from typing import TypeVar, Callable, Type
T = TypeVar('T', bound='A', covariant=True)
class A:
# TODO: This is potentially unsafe, as we use T in an argument type
def copy(self: T, factory: Callable[[T], T]) -> T:
return factory(self)
@classmethod
def new(cls: Type[T], factory: Callable[[T], T]) -> T:
reveal_type(cls) # N: Revealed type is "Type[T`-1]"
reveal_type(cls()) # N: Revealed type is "T`-1"
cls(2) # E: Too many arguments for "A"
return cls()
class B(A):
pass
reveal_type(A().copy) # N: Revealed type is "def (factory: def (__main__.A) -> __main__.A) -> __main__.A"
reveal_type(B().copy) # N: Revealed type is "def (factory: def (__main__.B) -> __main__.B) -> __main__.B"
reveal_type(A.new) # N: Revealed type is "def (factory: def (__main__.A) -> __main__.A) -> __main__.A"
reveal_type(B.new) # N: Revealed type is "def (factory: def (__main__.B) -> __main__.B) -> __main__.B"
[builtins fixtures/classmethod.pyi]
[case testSelfTypeBound]
from typing import TypeVar, Callable, cast
TA = TypeVar('TA', bound='A', covariant=True)
class A:
def copy(self: TA) -> TA:
pass
class C(A):
def copy(self: C) -> C:
pass
class D(A):
def copy(self: A) -> A: # E: Return type "A" of "copy" incompatible with return type "D" in supertype "A"
pass
TB = TypeVar('TB', bound='B', covariant=True)
class B(A):
x = 1
def copy(self: TB) -> TB:
reveal_type(self.x) # N: Revealed type is "builtins.int"
return cast(TB, None)
[builtins fixtures/bool.pyi]
-- # TODO: fail for this
-- [case testSelfTypeBare]
-- from typing import TypeVar, Type
--
-- T = TypeVar('T', bound='E')
--
-- class E:
-- def copy(self: T, other: T) -> T: pass
[case testSelfTypeClone]
from typing import TypeVar, Type
T = TypeVar('T', bound='C')
class C:
def copy(self: T) -> T:
return self
@classmethod
def new(cls: Type[T]) -> T:
return cls()
class D(C): pass
reveal_type(D.new) # N: Revealed type is "def () -> __main__.D"
reveal_type(D().new) # N: Revealed type is "def () -> __main__.D"
reveal_type(D.new()) # N: Revealed type is "__main__.D"
reveal_type(D().new()) # N: Revealed type is "__main__.D"
Q = TypeVar('Q', bound=C)
def clone(arg: Q) -> Q:
reveal_type(arg.copy) # N: Revealed type is "def () -> Q`-1"
reveal_type(arg.copy()) # N: Revealed type is "Q`-1"
reveal_type(arg.new) # N: Revealed type is "def () -> Q`-1"
reveal_type(arg.new()) # N: Revealed type is "Q`-1"
return arg.copy()
def make(cls: Type[Q]) -> Q:
reveal_type(cls.new) # N: Revealed type is "def () -> Q`-1"
reveal_type(cls().new) # N: Revealed type is "def () -> Q`-1"
reveal_type(cls().new()) # N: Revealed type is "Q`-1"
return cls.new()
[builtins fixtures/classmethod.pyi]
[case testSelfTypeGeneric]
from typing import TypeVar
T = TypeVar('T', int, str)
class A:
pass
class B(A):
def __init__(self, arg: T) -> None:
super(B, self).__init__()
[case testSelfTypeNonsensical]
from typing import TypeVar, Type
T = TypeVar('T', bound=str)
class A:
def foo(self: T) -> T: # E: The erased type of self "builtins.str" is not a supertype of its class "__main__.A"
return self
@classmethod
def cfoo(cls: Type[T]) -> T: # E: The erased type of self "Type[builtins.str]" is not a supertype of its class "Type[__main__.A]"
return cls()
Q = TypeVar('Q', bound='B')
class B:
def foo(self: Q) -> Q:
return self
@classmethod
def cfoo(cls: Type[Q]) -> Q:
return cls()
class C:
def foo(self: C) -> C: return self
@classmethod
def cfoo(cls: Type[C]) -> C:
return cls()
class D:
def foo(self: Q) -> Q: # E: The erased type of self "__main__.B" is not a supertype of its class "__main__.D"
return self
@staticmethod
def bar(self: str) -> str:
return self
@classmethod
def cfoo(cls: Type[Q]) -> Q: # E: The erased type of self "Type[__main__.B]" is not a supertype of its class "Type[__main__.D]"
return cls()
[builtins fixtures/classmethod.pyi]
[case testSelfTypeLambdaDefault]
from typing import Callable
class C:
@classmethod
def foo(cls,
arg: Callable[[int], str] = lambda a: ''
) -> None:
pass
def bar(self,
arg: Callable[[int], str] = lambda a: ''
) -> None:
pass
[builtins fixtures/classmethod.pyi]
[case testSelfTypeNew]
from typing import TypeVar, Type
T = TypeVar('T', bound='A')
class A:
def __new__(cls: Type[T]) -> T:
return cls()
def __init_subclass__(cls: Type[T]) -> None:
pass
class B:
def __new__(cls: Type[T]) -> T: # E: The erased type of self "Type[__main__.A]" is not a supertype of its class "Type[__main__.B]"
return cls()
def __init_subclass__(cls: Type[T]) -> None: # E: The erased type of self "Type[__main__.A]" is not a supertype of its class "Type[__main__.B]"
pass
class C:
def __new__(cls: Type[C]) -> C:
return cls()
def __init_subclass__(cls: Type[C]) -> None:
pass
class D:
def __new__(cls: D) -> D: # E: The erased type of self "__main__.D" is not a supertype of its class "Type[__main__.D]"
return cls
def __init_subclass__(cls: D) -> None: # E: The erased type of self "__main__.D" is not a supertype of its class "Type[__main__.D]"
pass
class E:
def __new__(cls) -> E:
reveal_type(cls) # N: Revealed type is "Type[__main__.E]"
return cls()
def __init_subclass__(cls) -> None:
reveal_type(cls) # N: Revealed type is "Type[__main__.E]"
[case testSelfTypeNew_explicit]
from typing import TypeVar, Type
T = TypeVar('T', bound='A')
class A:
@staticmethod
def __new__(cls: Type[T]) -> T:
return cls()
@classmethod
def __init_subclass__(cls: Type[T]) -> None:
pass
class B:
@staticmethod
def __new__(cls: Type[T]) -> T: # E: The erased type of self "Type[__main__.A]" is not a supertype of its class "Type[__main__.B]"
return cls()
@classmethod
def __init_subclass__(cls: Type[T]) -> None: # E: The erased type of self "Type[__main__.A]" is not a supertype of its class "Type[__main__.B]"
pass
class C:
@staticmethod
def __new__(cls: Type[C]) -> C:
return cls()
@classmethod
def __init_subclass__(cls: Type[C]) -> None:
pass
class D:
@staticmethod
def __new__(cls: D) -> D: # E: The erased type of self "__main__.D" is not a supertype of its class "Type[__main__.D]"
return cls
@classmethod
def __init_subclass__(cls: D) -> None: # E: The erased type of self "__main__.D" is not a supertype of its class "Type[__main__.D]"
pass
class E:
@staticmethod
def __new__(cls) -> E:
reveal_type(cls) # N: Revealed type is "Type[__main__.E]"
return cls()
@classmethod
def __init_subclass__(cls) -> None:
reveal_type(cls) # N: Revealed type is "Type[__main__.E]"
[builtins fixtures/classmethod.pyi]
[case testSelfTypePropertyUnion]
from typing import Union
class A:
@property
def f(self: A) -> int: pass
class B:
@property
def f(self: B) -> int: pass
x: Union[A, B]
reveal_type(x.f) # N: Revealed type is "builtins.int"
[builtins fixtures/property.pyi]
[case testSelfTypeProperSupertypeAttribute]
from typing import Callable, TypeVar, ClassVar
class K: pass
T = TypeVar('T', bound=K)
class A(K):
@property
def g(self: K) -> int: return 0
@property
def gt(self: T) -> T: return self
f: ClassVar[Callable[[object], int]]
ft: ClassVar[Callable[[T], T]]
class B(A):
pass
reveal_type(A().g) # N: Revealed type is "builtins.int"
reveal_type(A().gt) # N: Revealed type is "__main__.A"
reveal_type(A().f()) # N: Revealed type is "builtins.int"
reveal_type(A().ft()) # N: Revealed type is "__main__.A"
reveal_type(B().g) # N: Revealed type is "builtins.int"
reveal_type(B().gt) # N: Revealed type is "__main__.B"
reveal_type(B().f()) # N: Revealed type is "builtins.int"
reveal_type(B().ft()) # N: Revealed type is "__main__.B"
[builtins fixtures/property.pyi]
[case testSelfTypeProperSupertypeAttributeTuple]
from typing import Callable, TypeVar, Tuple, ClassVar
T = TypeVar('T')
class A(Tuple[int, int]):
@property
def g(self: object) -> int: return 0
@property
def gt(self: T) -> T: return self
f: ClassVar[Callable[[object], int]]
ft: ClassVar[Callable[[T], T]]
class B(A):
pass
reveal_type(A().g) # N: Revealed type is "builtins.int"
reveal_type(A().gt) # N: Revealed type is "Tuple[builtins.int, builtins.int, fallback=__main__.A]"
reveal_type(A().f()) # N: Revealed type is "builtins.int"
reveal_type(A().ft()) # N: Revealed type is "Tuple[builtins.int, builtins.int, fallback=__main__.A]"
reveal_type(B().g) # N: Revealed type is "builtins.int"
reveal_type(B().gt) # N: Revealed type is "Tuple[builtins.int, builtins.int, fallback=__main__.B]"
reveal_type(B().f()) # N: Revealed type is "builtins.int"
reveal_type(B().ft()) # N: Revealed type is "Tuple[builtins.int, builtins.int, fallback=__main__.B]"
[builtins fixtures/property.pyi]
[case testSelfTypeProperSupertypeAttributeMeta]
from typing import Callable, TypeVar, Type, ClassVar
T = TypeVar('T')
class A(type):
@property
def g(cls: object) -> int: return 0
@property
def gt(cls: T) -> T: return cls
f: ClassVar[Callable[[object], int]]
ft: ClassVar[Callable[[T], T]]
class B(A):
pass
class X(metaclass=B):
def __init__(self, x: int) -> None: pass
class Y(X): pass
X1: Type[X]
reveal_type(X.g) # N: Revealed type is "builtins.int"
reveal_type(X.gt) # N: Revealed type is "def (x: builtins.int) -> __main__.X"
reveal_type(X.f()) # N: Revealed type is "builtins.int"
reveal_type(X.ft()) # N: Revealed type is "def (x: builtins.int) -> __main__.X"
reveal_type(Y.g) # N: Revealed type is "builtins.int"
reveal_type(Y.gt) # N: Revealed type is "def (x: builtins.int) -> __main__.Y"
reveal_type(Y.f()) # N: Revealed type is "builtins.int"
reveal_type(Y.ft()) # N: Revealed type is "def (x: builtins.int) -> __main__.Y"
reveal_type(X1.g) # N: Revealed type is "builtins.int"
reveal_type(X1.gt) # N: Revealed type is "Type[__main__.X]"
reveal_type(X1.f()) # N: Revealed type is "builtins.int"
reveal_type(X1.ft()) # N: Revealed type is "Type[__main__.X]"
[builtins fixtures/property.pyi]
[case testSelfTypeProperSupertypeAttributeGeneric]
from typing import Callable, TypeVar, Generic, ClassVar
Q = TypeVar('Q', covariant=True)
class K(Generic[Q]):
q: Q
T = TypeVar('T')
class A(K[Q]):
@property
def g(self: K[object]) -> int: return 0
@property
def gt(self: K[T]) -> T: return self.q
f: ClassVar[Callable[[object], int]]
ft: ClassVar[Callable[[T], T]]
class B(A[Q]):
pass
a: A[int]
b: B[str]
reveal_type(a.g) # N: Revealed type is "builtins.int"
reveal_type(a.gt) # N: Revealed type is "builtins.int"
reveal_type(a.f()) # N: Revealed type is "builtins.int"
reveal_type(a.ft()) # N: Revealed type is "__main__.A[builtins.int]"
reveal_type(b.g) # N: Revealed type is "builtins.int"
reveal_type(b.gt) # N: Revealed type is "builtins.str"
reveal_type(b.f()) # N: Revealed type is "builtins.int"
reveal_type(b.ft()) # N: Revealed type is "__main__.B[builtins.str]"
[builtins fixtures/property.pyi]
[case testSelfTypeRestrictedMethod]
from typing import TypeVar, Generic
T = TypeVar('T')
class C(Generic[T]):
def from_item(self: C[str]) -> None: ...
i: C[int]
s: C[str]
i.from_item() # E: Invalid self argument "C[int]" to attribute function "from_item" with type "Callable[[C[str]], None]"
s.from_item()
[case testSelfTypeRestrictedClassMethod]
from typing import TypeVar, Generic, Type
T = TypeVar('T')
class C(Generic[T]):
@classmethod
def from_item(cls: Type[C[str]]) -> None: ...
class DI(C[int]): ...
class DS(C[str]): ...
DI().from_item() # E: Invalid self argument "Type[DI]" to class attribute function "from_item" with type "Callable[[Type[C[str]]], None]"
DS().from_item()
DI.from_item() # E: Invalid self argument "Type[DI]" to attribute function "from_item" with type "Callable[[Type[C[str]]], None]"
DS.from_item()
[builtins fixtures/classmethod.pyi]
[case testSelfTypeRestrictedMethodOverload]
from typing import TypeVar, Generic, overload, Tuple
T = TypeVar('T')
class C(Generic[T]):
@overload
def from_item(self: C[str], item: str) -> None: ...
@overload
def from_item(self: C[int], item: Tuple[int]) -> None: ...
def from_item(self, item):
...
ci: C[int]
cs: C[str]
reveal_type(ci.from_item) # N: Revealed type is "def (item: Tuple[builtins.int])"
reveal_type(cs.from_item) # N: Revealed type is "def (item: builtins.str)"
[builtins fixtures/tuple.pyi]
[case testSelfTypeRestrictedMethodOverloadFallback]
from typing import TypeVar, Generic, overload, Callable
T = TypeVar('T')
class C(Generic[T]):
@overload
def from_item(self: C[str]) -> str: ...
@overload
def from_item(self, converter: Callable[[T], str]) -> str: ...
def from_item(self, converter):
...
ci: C[int]
cs: C[str]
reveal_type(cs.from_item()) # N: Revealed type is "builtins.str"
ci.from_item() # E: Missing positional argument "converter" in call to "from_item" of "C"
def conv(x: int) -> str: ...
def bad(x: str) -> str: ...
reveal_type(ci.from_item(conv)) # N: Revealed type is "builtins.str"
ci.from_item(bad) # E: Argument 1 to "from_item" of "C" has incompatible type "Callable[[str], str]"; expected "Callable[[int], str]"
[case testSelfTypeRestrictedMethodOverloadInit]
from typing import TypeVar
from lib import P, C
reveal_type(P) # N: Revealed type is "Overload(def [T] (use_str: Literal[True]) -> lib.P[builtins.str], def [T] (use_str: Literal[False]) -> lib.P[builtins.int])"
reveal_type(P(use_str=True)) # N: Revealed type is "lib.P[builtins.str]"
reveal_type(P(use_str=False)) # N: Revealed type is "lib.P[builtins.int]"
reveal_type(C) # N: Revealed type is "Overload(def [T] (item: T`1, use_tuple: Literal[False]) -> lib.C[T`1], def [T] (item: T`1, use_tuple: Literal[True]) -> lib.C[builtins.tuple[T`1, ...]])"
reveal_type(C(0, use_tuple=False)) # N: Revealed type is "lib.C[builtins.int]"
reveal_type(C(0, use_tuple=True)) # N: Revealed type is "lib.C[builtins.tuple[builtins.int, ...]]"
T = TypeVar('T')
class SubP(P[T]):
pass
SubP('no') # E: No overload variant of "SubP" matches argument type "str" \
# N: Possible overload variants: \
# N: def [T] __init__(self, use_str: Literal[True]) -> SubP[T] \
# N: def [T] __init__(self, use_str: Literal[False]) -> SubP[T]
# This is a bit unfortunate: we don't have a way to map the overloaded __init__ to subtype.
x = SubP(use_str=True) # E: Need type annotation for "x"
reveal_type(x) # N: Revealed type is "__main__.SubP[Any]"
y: SubP[str] = SubP(use_str=True)
[file lib.pyi]
from typing import TypeVar, Generic, overload, Tuple
from typing_extensions import Literal
T = TypeVar('T')
class P(Generic[T]):
@overload
def __init__(self: P[str], use_str: Literal[True]) -> None: ...
@overload
def __init__(self: P[int], use_str: Literal[False]) -> None: ...
class C(Generic[T]):
@overload
def __init__(self: C[T], item: T, use_tuple: Literal[False]) -> None: ...
@overload
def __init__(self: C[Tuple[T, ...]], item: T, use_tuple: Literal[True]) -> None: ...
[builtins fixtures/bool.pyi]
[case testSelfTypeRestrictedMethodOverloadInitFallBacks]
from lib import PFallBack, PFallBackAny
t: bool
xx = PFallBack(t) # E: Need type annotation for "xx"
yy = PFallBackAny(t) # OK
[file lib.pyi]
from typing import TypeVar, Generic, overload, Tuple, Any
from typing_extensions import Literal
class PFallBack(Generic[T]):
@overload
def __init__(self: PFallBack[str], use_str: Literal[True]) -> None: ...
@overload
def __init__(self: PFallBack[int], use_str: Literal[False]) -> None: ...
@overload
def __init__(self, use_str: bool) -> None: ...
class PFallBackAny(Generic[T]):
@overload
def __init__(self: PFallBackAny[str], use_str: Literal[True]) -> None: ...
@overload
def __init__(self: PFallBackAny[int], use_str: Literal[False]) -> None: ...
@overload
def __init__(self: PFallBackAny[Any], use_str: bool) -> None: ...
[builtins fixtures/bool.pyi]
[case testSelfTypeRestrictedMethodOverloadInitBadTypeNoCrash]
from lib import P
P(0)
[file lib.pyi]
from typing import overload
class P:
@overload
def __init__(self: Bad, x: int) -> None: ... # E: Name "Bad" is not defined
@overload
def __init__(self) -> None: ...
[case testSelfTypeNarrowBinding]
from typing import TypeVar, List, Generic
T = TypeVar('T')
S = TypeVar('S')
class Base(Generic[T]):
def get_item(self: Base[List[S]]) -> S: ...
class Sub(Base[List[int]]): ...
class BadSub(Base[int]): ...
reveal_type(Sub().get_item()) # N: Revealed type is "builtins.int"
BadSub().get_item() # E: Invalid self argument "BadSub" to attribute function "get_item" with type "Callable[[Base[List[S]]], S]"
[builtins fixtures/list.pyi]
[case testMixinAllowedWithProtocol]
from typing import TypeVar
from typing_extensions import Protocol
class Resource(Protocol):
def close(self) -> int: ...
class AtomicClose:
def atomic_close(self: Resource) -> int:
return self.close()
T = TypeVar('T', bound=Resource)
class Copyable:
def copy(self: T) -> T: ...
class File(AtomicClose, Copyable):
def close(self) -> int:
...
class Bad(AtomicClose, Copyable):
...
f: File
b: Bad
f.atomic_close() # OK
b.atomic_close() # E: Invalid self argument "Bad" to attribute function "atomic_close" with type "Callable[[Resource], int]"
reveal_type(f.copy()) # N: Revealed type is "__main__.File"
b.copy() # E: Invalid self argument "Bad" to attribute function "copy" with type "Callable[[T], T]"
[builtins fixtures/tuple.pyi]
[case testMixinProtocolSuper]
from typing import Protocol
class Base(Protocol):
def func(self) -> int:
...
class TweakFunc:
def func(self: Base) -> int:
return reveal_type(super().func()) # E: Call to abstract method "func" of "Base" with trivial body via super() is unsafe \
# N: Revealed type is "builtins.int"
class Good:
def func(self) -> int: ...
class C(TweakFunc, Good): pass
C().func() # OK
class Bad:
def func(self) -> str: ...
class CC(TweakFunc, Bad): pass # E: Definition of "func" in base class "TweakFunc" is incompatible with definition in base class "Bad"
[case testBadClassLevelDecoratorHack]
from typing_extensions import Protocol
from typing import TypeVar, Any
class FuncLike(Protocol):
__call__: Any
F = TypeVar('F', bound=FuncLike)
class Test:
def _deco(func: F) -> F: ...
@_deco
def meth(self, x: str) -> int: ...
reveal_type(Test().meth) # N: Revealed type is "def (x: builtins.str) -> builtins.int"
Test()._deco # E: Invalid self argument "Test" to attribute function "_deco" with type "Callable[[F], F]"
[builtins fixtures/tuple.pyi]
[case testSelfTypeTrickyExample]
from typing import *
In = TypeVar('In')
Out = TypeVar('Out')
Mid = TypeVar('Mid')
NewOut = TypeVar('NewOut')
class Lnk(Generic[In, Out]):
def test(self: Lnk[In, Mid], other: Lnk[Mid, NewOut]) -> Lnk[In, NewOut]: ...
class X: pass
class Y: pass
class Z: pass
a: Lnk[X, Y] = Lnk()
b: Lnk[Y, Z] = Lnk()
a.test(b)
b.test(a) # E: Argument 1 to "test" of "Lnk" has incompatible type "Lnk[X, Y]"; expected "Lnk[Z, Y]"
[case testSelfTypeReallyTrickyExample]
from typing import *
In = TypeVar('In')
Out = TypeVar('Out')
Other = TypeVar('Other')
_1 = TypeVar('_1')
_2 = TypeVar('_2')
__1 = TypeVar('__1')
__2 = TypeVar('__2')
class Lnk(Generic[In, Out]):
@overload
def __rshift__(self, other: Lnk[Out, Other]) -> Lnk[In,Other]: ...
@overload
def __rshift__(self: Lnk[In, Tuple[_1, _2]],
other: Tuple[Lnk[_1, __1], Lnk[_2, __2]]) -> Lnk[In, Tuple[__1, __2]]: ...
def __rshift__(self: Any, other: Any) -> Any:
...
a: Lnk[str, Tuple[str, int]] = Lnk()
b: Lnk[str, int] = Lnk()
c: Lnk[int, float] = Lnk()
d: Lnk[str, float] = b >> c # OK
e: Lnk[str, Tuple[int, float]] = a >> (b, c) # OK
f: Lnk[str, Tuple[float, int]] = a >> (c, b) # E: Unsupported operand types for >> ("Lnk[str, Tuple[str, int]]" and "Tuple[Lnk[int, float], Lnk[str, int]]")
[builtins fixtures/tuple.pyi]
[case testSelfTypeMutuallyExclusiveRestrictions]
from typing import Generic, TypeVar
T = TypeVar('T')
class Foo(Generic[T]):
def f1(self: Foo[str]) -> None:
self.f2() # E: Invalid self argument "Foo[str]" to attribute function "f2" with type "Callable[[Foo[int]], None]"
def f2(self: Foo[int]) -> None:
self.f1() # E: Invalid self argument "Foo[int]" to attribute function "f1" with type "Callable[[Foo[str]], None]"
[case testSelfTypeStructureMetaclassMatch]
from typing import TypeVar, Type, Generic, cast
Cls = TypeVar('Cls')
T = TypeVar('T')
class Manager(Generic[Cls]):
def create(self: Manager[Type[T]]) -> T: ...