forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-type-aliases.test
1061 lines (849 loc) · 30.2 KB
/
check-type-aliases.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 testSimpleTypeAlias]
import typing
i = int
def f(x: i) -> None: pass
f(1)
f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
[targets __main__, __main__.f]
[case testUnionTypeAlias]
from typing import Union
U = Union[int, str]
def f(x: U) -> None: pass
f(1)
f('')
f(()) # E: Argument 1 to "f" has incompatible type "Tuple[]"; expected "Union[int, str]"
[targets __main__, __main__.f]
[builtins fixtures/tuple.pyi]
[case testTupleTypeAlias]
from typing import Tuple
T = Tuple[int, str]
def f(x: T) -> None: pass
f((1, 'x'))
f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "Tuple[int, str]"
[targets __main__, __main__.f]
[builtins fixtures/tuple.pyi]
[case testCallableTypeAlias]
from typing import Callable
A = Callable[[int], None]
f = None # type: A
f(1)
f('') # E: Argument 1 has incompatible type "str"; expected "int"
[targets __main__]
[case testListTypeAlias]
from typing import List
A = List[int]
def f(x: A) -> None: pass
f([1])
f(['x']) # E: List item 0 has incompatible type "str"; expected "int"
[builtins fixtures/list.pyi]
[targets __main__, __main__.f]
[case testAnyTypeAlias]
from typing import Any
A = Any
def f(x: A) -> None:
x.foo()
f(1)
f('x')
[case testNoReturnTypeAlias]
# https://github.com/python/mypy/issues/11903
from typing import NoReturn
Never = NoReturn
a: Never # Used to be an error here
def f(a: Never): ...
f(5) # E: Argument 1 to "f" has incompatible type "int"; expected "NoReturn"
[case testImportUnionAlias]
import typing
from _m import U
def f(x: U) -> None: pass
f(1)
f('x')
f(()) # E: Argument 1 to "f" has incompatible type "Tuple[]"; expected "Union[int, str]"
[file _m.py]
from typing import Union
U = Union[int, str]
[builtins fixtures/tuple.pyi]
[case testProhibitReassigningAliases]
A = float
if int():
A = int # E: Cannot assign multiple types to name "A" without an explicit "Type[...]" annotation
[out]
[case testProhibitReassigningSubscriptedAliases]
from typing import Callable
A = Callable[[], float]
if int():
A = Callable[[], int] \
# E: Cannot assign multiple types to name "A" without an explicit "Type[...]" annotation \
# E: Value of type "int" is not indexable
# the second error is because of `Callable = 0` in lib-stub/typing.pyi
[builtins fixtures/list.pyi]
[out]
[case testProhibitReassigningGenericAliases]
from typing import TypeVar, Union, Tuple
T = TypeVar('T')
A = Tuple[T, T]
if int():
A = Union[T, int] # E: Cannot assign multiple types to name "A" without an explicit "Type[...]" annotation \
# E: Value of type "int" is not indexable
# the second error is because of `Union = 0` in lib-stub/typing.pyi
[builtins fixtures/tuple.pyi]
[out]
[case testProhibitUsingVariablesAsTypesAndAllowAliasesAsTypes]
from typing import TypeVar, Sequence, Type
T = TypeVar('T')
A: Type[float] = int
if int():
A = float # OK
x: A # E: Variable "__main__.A" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
def bad(tp: A) -> None: # E: Variable "__main__.A" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
pass
Alias = int
GenAlias = Sequence[T]
def fun(x: Alias) -> GenAlias[int]: pass
[out]
[case testCorrectQualifiedAliasesAlsoInFunctions]
from typing import TypeVar, Generic
T = TypeVar('T')
S = TypeVar('S')
class X(Generic[T]):
A = X[S]
def f(self) -> X[T]:
pass
a: X[T]
b: A = a
c: A[T] = a
d: A[int] = a # E: Incompatible types in assignment (expression has type "X[T]", variable has type "X[int]")
def g(self) -> None:
a: X[T]
b: X.A = a
c: X.A[T] = a
d: X.A[int] = a # E: Incompatible types in assignment (expression has type "X[T]", variable has type "X[int]")
def g(arg: X[int]) -> None:
p: X[int] = arg.f()
q: X.A = arg.f()
r: X.A[str] = arg.f() # E: Incompatible types in assignment (expression has type "X[int]", variable has type "X[str]")
[out]
[case testProhibitBoundTypeVariableReuseForAliases]
from typing import TypeVar, Generic, List
T = TypeVar('T')
class C(Generic[T]):
A = List[T] # E: Can't use bound type variable "T" to define generic alias
x: C.A
reveal_type(x) # N: Revealed type is "builtins.list[Any]"
def f(x: T) -> T:
A = List[T] # E: Can't use bound type variable "T" to define generic alias
return x
[builtins fixtures/list.pyi]
[out]
[case testTypeAliasInBuiltins]
def f(x: bytes): pass
bytes
f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str"
[builtins fixtures/alias.pyi]
[case testEmptyTupleTypeAlias]
from typing import Tuple, Callable
EmptyTuple = Tuple[()]
x = None # type: EmptyTuple
reveal_type(x) # N: Revealed type is "Tuple[]"
EmptyTupleCallable = Callable[[Tuple[()]], None]
f = None # type: EmptyTupleCallable
reveal_type(f) # N: Revealed type is "def (Tuple[])"
[builtins fixtures/list.pyi]
[case testForwardTypeAlias]
def f(p: 'Alias') -> None:
pass
reveal_type(f) # N: Revealed type is "def (p: builtins.int)"
Alias = int
[out]
[case testForwardTypeAliasGeneric]
from typing import TypeVar, Tuple
def f(p: 'Alias[str]') -> None:
pass
reveal_type(f) # N: Revealed type is "def (p: Tuple[builtins.int, builtins.str])"
T = TypeVar('T')
Alias = Tuple[int, T]
[builtins fixtures/tuple.pyi]
[out]
[case testRecursiveAliasesErrors1]
# flags: --disable-recursive-aliases
# Recursive aliases are not supported yet.
from typing import Type, Callable, Union
A = Union[A, int] # E: Cannot resolve name "A" (possible cyclic definition)
B = Callable[[B], int] # E: Cannot resolve name "B" (possible cyclic definition)
C = Type[C] # E: Cannot resolve name "C" (possible cyclic definition)
[case testRecursiveAliasesErrors2]
# flags: --disable-recursive-aliases --disable-error-code=used-before-def
# Recursive aliases are not supported yet.
from typing import Type, Callable, Union
A = Union[B, int]
B = Callable[[C], int]
C = Type[A]
x: A
reveal_type(x)
[out]
main:5: error: Cannot resolve name "A" (possible cyclic definition)
main:5: error: Cannot resolve name "B" (possible cyclic definition)
main:6: error: Cannot resolve name "B" (possible cyclic definition)
main:6: error: Cannot resolve name "C" (possible cyclic definition)
main:7: error: Cannot resolve name "C" (possible cyclic definition)
main:9: note: Revealed type is "Union[Any, builtins.int]"
[case testDoubleForwardAlias]
# flags: --disable-error-code=used-before-def
from typing import List
x: A
A = List[B]
B = List[int]
reveal_type(x) # N: Revealed type is "builtins.list[builtins.list[builtins.int]]"
[builtins fixtures/list.pyi]
[out]
[case testDoubleForwardAliasWithNamedTuple]
# flags: --disable-error-code=used-before-def
from typing import List, NamedTuple
x: A
A = List[B]
class B(NamedTuple):
x: str
reveal_type(x[0].x) # N: Revealed type is "builtins.str"
[builtins fixtures/list.pyi]
[out]
[case testJSONAliasApproximation]
# flags: --disable-recursive-aliases
from typing import List, Union, Dict
x: JSON # E: Cannot resolve name "JSON" (possible cyclic definition)
JSON = Union[int, str, List[JSON], Dict[str, JSON]] # E: Cannot resolve name "JSON" (possible cyclic definition)
reveal_type(x) # N: Revealed type is "Any"
if isinstance(x, list):
reveal_type(x) # N: Revealed type is "builtins.list[Any]"
[builtins fixtures/isinstancelist.pyi]
[out]
[case testForwardRefToTypeVar]
# flags: --disable-error-code=used-before-def
from typing import TypeVar, List
reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
a: A[int]
A = List[T]
T = TypeVar('T')
[builtins fixtures/list.pyi]
[out]
[case testFunctionForwardRefAlias]
from typing import List, TypeVar
T = TypeVar('T')
def f(x: T) -> List[T]:
y: A[T]
reveal_type(y) # N: Revealed type is "builtins.list[T`-1]"
return [x] + y
A = List[T]
[builtins fixtures/list.pyi]
[out]
[case testFunctionForwardRefAlias2]
from typing import List, TypeVar
def f() -> None:
X = List[int]
x: A[X]
reveal_type(x) # N: Revealed type is "builtins.list[builtins.list[builtins.int]]"
T = TypeVar('T')
A = List[T]
[builtins fixtures/list.pyi]
[out]
[case testNoneAlias]
from typing import Union
void = type(None)
x: void
reveal_type(x) # N: Revealed type is "None"
y: Union[int, void]
reveal_type(y) # N: Revealed type is "Union[builtins.int, None]"
[builtins fixtures/bool.pyi]
[case testNoneAliasStrict]
# flags: --strict-optional
from typing import Optional, Union
void = type(None)
x: int
y: Union[int, void]
z: Optional[int]
x = y # E: Incompatible types in assignment (expression has type "Optional[int]", variable has type "int")
y = z
[builtins fixtures/bool.pyi]
[case testAliasToTupleAndCallable]
from typing import Callable, Tuple
C = Callable
T = Tuple
c: C
t: T
reveal_type(c) # N: Revealed type is "def (*Any, **Any) -> Any"
reveal_type(t) # N: Revealed type is "builtins.tuple[Any, ...]"
bad: C[int] # E: Bad number of arguments for type alias, expected: 0, given: 1
also_bad: T[int] # E: Bad number of arguments for type alias, expected: 0, given: 1
[builtins fixtures/tuple.pyi]
[out]
[case testAliasRefOnClass]
from typing import Generic, TypeVar, Type
T = TypeVar('T')
class C(Generic[T]):
pass
class N:
A = C[T]
B = C[int]
x: N.A[C]
reveal_type(x) # N: Revealed type is "__main__.C[__main__.C[Any]]"
xx = N.A[C]()
reveal_type(xx) # N: Revealed type is "__main__.C[__main__.C[Any]]"
y = N.A()
reveal_type(y) # N: Revealed type is "__main__.C[Any]"
M = N
b = M.A[int]()
reveal_type(b) # N: Revealed type is "__main__.C[builtins.int]"
n: Type[N]
w = n.B()
reveal_type(w) # N: Revealed type is "__main__.C[builtins.int]"
[out]
[case testTypeAliasesToNamedTuple]
from nt import C, D, E
A1 = C
A2 = D
A3 = E
class Cls:
A1 = C
A2 = D
A3 = E
A1('no') # E: Argument 1 to "C" has incompatible type "str"; expected "int"
a1 = A1(1)
reveal_type(a1) # N: Revealed type is "Tuple[builtins.int, fallback=nt.C]"
A2(0) # E: Argument 1 to "D" has incompatible type "int"; expected "str"
a2 = A2('yes')
reveal_type(a2) # N: Revealed type is "Tuple[builtins.str, fallback=nt.D]"
a3 = A3()
reveal_type(a3) # N: Revealed type is "Tuple[builtins.int, builtins.str, fallback=nt.E]"
Cls.A1('no') # E: Argument 1 has incompatible type "str"; expected "int"
ca1 = Cls.A1(1)
reveal_type(ca1) # N: Revealed type is "Tuple[builtins.int, fallback=nt.C]"
Cls.A2(0) # E: Argument 1 has incompatible type "int"; expected "str"
ca2 = Cls.A2('yes')
reveal_type(ca2) # N: Revealed type is "Tuple[builtins.str, fallback=nt.D]"
ca3 = Cls.A3()
reveal_type(ca3) # N: Revealed type is "Tuple[builtins.int, builtins.str, fallback=nt.E]"
[file nt.pyi]
from typing import NamedTuple, Tuple
class C(NamedTuple):
x: int
D = NamedTuple('D', [('y', str)])
class E(Tuple[int, str]):
pass
[builtins fixtures/tuple.pyi]
[out]
[case testTypeAliasesToAny]
from typing import Any
A = Any
A # this should not fail
[out]
[case testDoubleImportsOfAnAlias]
from b import *
from c import *
[file b.py]
from a import A
[file c.py]
from a import A
[file a.py]
A = int
[out]
[case testDoubleImportsOfAnAlias2]
from b import A
from c import A
[file b.py]
from a import A
[file c.py]
from a import A
[file a.py]
A = int
[out]
[case testDoubleImportsOfAnAlias3]
from b import *
from c import *
[file b.py]
from a import A
[file c.py]
from a import A
[file a.py]
from typing import Union
A = Union[None]
[out]
[case testAliasToClassMethod]
from typing import TypeVar, Generic, Union, Type
T = TypeVar('T', bound='C')
MYPY = False
if MYPY:
test = classmethod
class C:
@test
def meth(cls: Type[T], arg: int) -> Union[T, int]: ...
class D(C): ...
reveal_type(D.meth(1)) # N: Revealed type is "Union[__main__.D, builtins.int]"
reveal_type(D().meth(1)) # N: Revealed type is "Union[__main__.D, builtins.int]"
[builtins fixtures/classmethod.pyi]
[out]
[case testAliasInImportCycle]
# cmd: mypy -m t t2
[file t.py]
MYPY = False
if MYPY:
from t2 import A
x: A
[file t2.py]
import t
from typing import Callable
A = Callable[[], None]
[builtins fixtures/bool.pyi]
[out]
[case testAliasInImportCycle2]
import a
[file a.pyi]
from b import Parameter
class _ParamType:
p: Parameter
_ConvertibleType = _ParamType
def convert_type(ty: _ConvertibleType):
...
[file b.pyi]
from a import _ConvertibleType
class Parameter:
type: _ConvertibleType
[out]
[case testAliasInImportCycle3]
# cmd: mypy -m t t2
[file t.py]
MYPY = False
if MYPY:
from t2 import A
x: A
reveal_type(x) # N: Revealed type is "t2.D"
reveal_type(A) # N: Revealed type is "def () -> t2.D"
A()
[file t2.py]
import t
class D: pass
A = D
[builtins fixtures/bool.pyi]
[out]
[case testFlexibleAlias1]
from typing import TypeVar, List, Tuple
from mypy_extensions import FlexibleAlias
T = TypeVar('T')
U = TypeVar('U')
AnInt = FlexibleAlias[T, int]
x: AnInt[str]
reveal_type(x) # N: Revealed type is "builtins.int"
TwoArgs = FlexibleAlias[Tuple[T, U], bool]
TwoArgs2 = FlexibleAlias[Tuple[T, U], List[U]]
def welp(x: TwoArgs[str, int]) -> None:
reveal_type(x) # N: Revealed type is "builtins.bool"
def welp2(x: TwoArgs2[str, int]) -> None:
reveal_type(x) # N: Revealed type is "builtins.list[builtins.int]"
Id = FlexibleAlias[T, T]
def take_id(x: Id[int]) -> None:
reveal_type(x) # N: Revealed type is "builtins.int"
def id(x: Id[T]) -> T:
return x
# TODO: This doesn't work and maybe it should?
# Indirection = AnInt[T]
# y: Indirection[str]
# reveal_type(y) # E : Revealed type is "builtins.int"
# But this does
Indirection2 = FlexibleAlias[T, AnInt[T]]
z: Indirection2[str]
reveal_type(z) # N: Revealed type is "builtins.int"
Indirection3 = FlexibleAlias[Tuple[T, U], AnInt[T]]
w: Indirection3[str, int]
reveal_type(w) # N: Revealed type is "builtins.int"
[builtins fixtures/dict.pyi]
[case testFlexibleAlias2]
# flags: --always-true=BOGUS
from typing import TypeVar, Any
from mypy_extensions import FlexibleAlias
T = TypeVar('T')
BOGUS = True
if BOGUS:
Bogus = FlexibleAlias[T, Any]
else:
Bogus = FlexibleAlias[T, T]
class A:
x: Bogus[str]
reveal_type(A().x) # N: Revealed type is "Any"
def foo(x: Bogus[int]) -> None:
reveal_type(x) # N: Revealed type is "Any"
[builtins fixtures/dict.pyi]
[case testFlexibleAlias3]
# flags: --always-false=BOGUS
from typing import TypeVar, Any
from mypy_extensions import FlexibleAlias
T = TypeVar('T')
BOGUS = True
if BOGUS:
Bogus = FlexibleAlias[T, Any]
else:
Bogus = FlexibleAlias[T, T]
class A:
x: Bogus[str]
reveal_type(A().x) # N: Revealed type is "builtins.str"
def foo(x: Bogus[int]) -> None:
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/dict.pyi]
[case testOverrideByIdemAliasCorrectType]
C = C
class C: # type: ignore
pass
x: C
reveal_type(x) # N: Revealed type is "__main__.C"
[out]
[case testOverrideByIdemAliasCorrectTypeReversed]
class C:
pass
C = C # type: ignore
x: C
reveal_type(x) # N: Revealed type is "__main__.C"
[out]
[case testOverrideByIdemAliasCorrectTypeImported]
from other import C as B
C = B
x: C
reveal_type(x) # N: Revealed type is "other.C"
[file other.py]
class C:
pass
[out]
[case testConditionalExceptionAlias]
try:
E = E
except BaseException:
class E(BaseException): pass # type: ignore
try:
pass
except E as e:
reveal_type(e) # N: Revealed type is "__main__.E"
[builtins fixtures/exception.pyi]
[out]
[case testNestedClassOnAliasAsType]
class Out:
class In:
class Inner:
pass
O = Out
I = Out.In
OI = O.In
A = Out
B = A
w: O.In
x: I.Inner
y: OI.Inner
z: B.In
reveal_type(w) # N: Revealed type is "__main__.Out.In"
reveal_type(x) # N: Revealed type is "__main__.Out.In.Inner"
reveal_type(y) # N: Revealed type is "__main__.Out.In.Inner"
reveal_type(z) # N: Revealed type is "__main__.Out.In"
[case testSimplePep613]
from typing_extensions import TypeAlias
x: TypeAlias = str
a: x
reveal_type(a) # N: Revealed type is "builtins.str"
y: TypeAlias = "str"
b: y
reveal_type(b) # N: Revealed type is "builtins.str"
z: TypeAlias = "int | str"
c: z
reveal_type(c) # N: Revealed type is "Union[builtins.int, builtins.str]"
[builtins fixtures/tuple.pyi]
[case testForwardRefPep613]
from typing_extensions import TypeAlias
x: TypeAlias = "MyClass"
a: x
reveal_type(a) # N: Revealed type is "__main__.MyClass"
class MyClass: ...
[builtins fixtures/tuple.pyi]
[case testInvalidPep613]
from typing_extensions import TypeAlias
x: TypeAlias = list(int) # E: Invalid type alias: expression is not a valid type \
# E: Too many arguments for "list"
a: x
[builtins fixtures/tuple.pyi]
[case testAliasedImportPep613]
import typing as tpp
import typing_extensions as tpx
from typing import TypeAlias as TPA
from typing_extensions import TypeAlias as TXA
import typing
import typing_extensions
Int1: tpp.TypeAlias = int
Int2: tpx.TypeAlias = int
Int3: TPA = int
Int4: TXA = int
Int5: typing.TypeAlias = int
Int6: typing_extensions.TypeAlias = int
x1: Int1 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
x2: Int2 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
x3: Int3 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
x4: Int4 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
x5: Int5 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
x6: Int6 = "str" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-medium.pyi]
[case testFunctionScopePep613]
from typing_extensions import TypeAlias
def f() -> None:
x: TypeAlias = str
a: x
reveal_type(a) # N: Revealed type is "builtins.str"
y: TypeAlias = "str"
b: y
reveal_type(b) # N: Revealed type is "builtins.str"
[builtins fixtures/tuple.pyi]
[case testImportCyclePep613]
# cmd: mypy -m t t2
[file t.py]
MYPY = False
if MYPY:
from t2 import A
x: A
reveal_type(x) # N: Revealed type is "builtins.str"
[file t2.py]
from typing_extensions import TypeAlias
A: TypeAlias = str
[builtins fixtures/bool.pyi]
[out]
[case testLiteralStringPep675]
# flags: --python-version 3.11
from typing import LiteralString as tpLS
from typing_extensions import LiteralString as tpxLS
def f(a: tpLS, b: tpxLS) -> None:
reveal_type(a) # N: Revealed type is "builtins.str"
reveal_type(b) # N: Revealed type is "builtins.str"
# This isn't the correct behaviour, but should unblock use of LiteralString in typeshed
f("asdf", "asdf")
string: str
f(string, string)
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-medium.pyi]
[case testForwardTypeVarRefWithRecursiveFlag]
import c
[file a.py]
from typing import TypeVar, List, Any, Generic
from b import Alias
T = TypeVar("T", bound=Alias[Any])
def foo(x: T) -> T: ...
[file b.py]
from c import C
from typing import TypeVar, List
S = TypeVar("S")
Alias = List[C[S]]
[file c.py]
from typing import TypeVar, List, Generic
import a
S = TypeVar("S")
class C(Generic[S], List[Defer]): ...
class Defer: ...
[builtins fixtures/list.pyi]
[case testClassLevelTypeAliasesInUnusualContexts]
from typing import Union
from typing_extensions import TypeAlias
class Foo: pass
NormalImplicit = Foo
NormalExplicit: TypeAlias = Foo
SpecialImplicit = Union[int, str]
SpecialExplicit: TypeAlias = Union[int, str]
class Parent:
NormalImplicit = Foo
NormalExplicit: TypeAlias = Foo
SpecialImplicit = Union[int, str]
SpecialExplicit: TypeAlias = Union[int, str]
class Child(Parent): pass
p = Parent()
c = Child()
# Use type aliases in a runtime context
reveal_type(NormalImplicit) # N: Revealed type is "def () -> __main__.Foo"
reveal_type(NormalExplicit) # N: Revealed type is "def () -> __main__.Foo"
reveal_type(SpecialImplicit) # N: Revealed type is "typing._SpecialForm"
reveal_type(SpecialExplicit) # N: Revealed type is "typing._SpecialForm"
reveal_type(Parent.NormalImplicit) # N: Revealed type is "def () -> __main__.Foo"
reveal_type(Parent.NormalExplicit) # N: Revealed type is "def () -> __main__.Foo"
reveal_type(Parent.SpecialImplicit) # N: Revealed type is "typing._SpecialForm"
reveal_type(Parent.SpecialExplicit) # N: Revealed type is "typing._SpecialForm"
reveal_type(Child.NormalImplicit) # N: Revealed type is "def () -> __main__.Foo"
reveal_type(Child.NormalExplicit) # N: Revealed type is "def () -> __main__.Foo"
reveal_type(Child.SpecialImplicit) # N: Revealed type is "typing._SpecialForm"
reveal_type(Child.SpecialExplicit) # N: Revealed type is "typing._SpecialForm"
reveal_type(p.NormalImplicit) # N: Revealed type is "def () -> __main__.Foo"
reveal_type(p.NormalExplicit) # N: Revealed type is "def () -> __main__.Foo"
reveal_type(p.SpecialImplicit) # N: Revealed type is "typing._SpecialForm"
reveal_type(p.SpecialExplicit) # N: Revealed type is "typing._SpecialForm"
reveal_type(c.NormalImplicit) # N: Revealed type is "def () -> __main__.Foo"
reveal_type(p.NormalExplicit) # N: Revealed type is "def () -> __main__.Foo"
reveal_type(c.SpecialImplicit) # N: Revealed type is "typing._SpecialForm"
reveal_type(c.SpecialExplicit) # N: Revealed type is "typing._SpecialForm"
# Use type aliases in a type alias context in a plausible way
def plausible_top_1() -> NormalImplicit: pass
def plausible_top_2() -> NormalExplicit: pass
def plausible_top_3() -> SpecialImplicit: pass
def plausible_top_4() -> SpecialExplicit: pass
reveal_type(plausible_top_1) # N: Revealed type is "def () -> __main__.Foo"
reveal_type(plausible_top_2) # N: Revealed type is "def () -> __main__.Foo"
reveal_type(plausible_top_3) # N: Revealed type is "def () -> Union[builtins.int, builtins.str]"
reveal_type(plausible_top_4) # N: Revealed type is "def () -> Union[builtins.int, builtins.str]"
def plausible_parent_1() -> Parent.NormalImplicit: pass # E: Variable "__main__.Parent.NormalImplicit" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
def plausible_parent_2() -> Parent.NormalExplicit: pass
def plausible_parent_3() -> Parent.SpecialImplicit: pass
def plausible_parent_4() -> Parent.SpecialExplicit: pass
reveal_type(plausible_parent_1) # N: Revealed type is "def () -> Parent.NormalImplicit?"
reveal_type(plausible_parent_2) # N: Revealed type is "def () -> __main__.Foo"
reveal_type(plausible_parent_3) # N: Revealed type is "def () -> Union[builtins.int, builtins.str]"
reveal_type(plausible_parent_4) # N: Revealed type is "def () -> Union[builtins.int, builtins.str]"
def plausible_child_1() -> Child.NormalImplicit: pass # E: Variable "__main__.Parent.NormalImplicit" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
def plausible_child_2() -> Child.NormalExplicit: pass
def plausible_child_3() -> Child.SpecialImplicit: pass
def plausible_child_4() -> Child.SpecialExplicit: pass
reveal_type(plausible_child_1) # N: Revealed type is "def () -> Child.NormalImplicit?"
reveal_type(plausible_child_2) # N: Revealed type is "def () -> __main__.Foo"
reveal_type(plausible_child_3) # N: Revealed type is "def () -> Union[builtins.int, builtins.str]"
reveal_type(plausible_child_4) # N: Revealed type is "def () -> Union[builtins.int, builtins.str]"
# Use type aliases in a type alias context in an implausible way
def weird_parent_1() -> p.NormalImplicit: pass # E: Name "p.NormalImplicit" is not defined
def weird_parent_2() -> p.NormalExplicit: pass # E: Name "p.NormalExplicit" is not defined
def weird_parent_3() -> p.SpecialImplicit: pass # E: Name "p.SpecialImplicit" is not defined
def weird_parent_4() -> p.SpecialExplicit: pass # E: Name "p.SpecialExplicit" is not defined
reveal_type(weird_parent_1) # N: Revealed type is "def () -> Any"
reveal_type(weird_parent_2) # N: Revealed type is "def () -> Any"
reveal_type(weird_parent_3) # N: Revealed type is "def () -> Any"
reveal_type(weird_parent_4) # N: Revealed type is "def () -> Any"
def weird_child_1() -> c.NormalImplicit: pass # E: Name "c.NormalImplicit" is not defined
def weird_child_2() -> c.NormalExplicit: pass # E: Name "c.NormalExplicit" is not defined
def weird_child_3() -> c.SpecialImplicit: pass # E: Name "c.SpecialImplicit" is not defined
def weird_child_4() -> c.SpecialExplicit: pass # E: Name "c.SpecialExplicit" is not defined
reveal_type(weird_child_1) # N: Revealed type is "def () -> Any"
reveal_type(weird_child_2) # N: Revealed type is "def () -> Any"
reveal_type(weird_child_3) # N: Revealed type is "def () -> Any"
reveal_type(weird_child_4) # N: Revealed type is "def () -> Any"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-medium.pyi]
[case testMalformedTypeAliasRuntimeReassignments]
from typing import Union
from typing_extensions import TypeAlias
class Foo: pass
NormalImplicit = Foo
NormalExplicit: TypeAlias = Foo
SpecialImplicit = Union[int, str]
SpecialExplicit: TypeAlias = Union[int, str]
class Parent:
NormalImplicit = Foo
NormalExplicit: TypeAlias = Foo
SpecialImplicit = Union[int, str]
SpecialExplicit: TypeAlias = Union[int, str]
class Child(Parent): pass
p = Parent()
c = Child()
NormalImplicit = 4 # E: Cannot assign multiple types to name "NormalImplicit" without an explicit "Type[...]" annotation \
# E: Incompatible types in assignment (expression has type "int", variable has type "Type[Foo]")
NormalExplicit = 4 # E: Cannot assign multiple types to name "NormalExplicit" without an explicit "Type[...]" annotation \
# E: Incompatible types in assignment (expression has type "int", variable has type "Type[Foo]")
SpecialImplicit = 4 # E: Cannot assign multiple types to name "SpecialImplicit" without an explicit "Type[...]" annotation
SpecialExplicit = 4 # E: Cannot assign multiple types to name "SpecialExplicit" without an explicit "Type[...]" annotation
Parent.NormalImplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "Type[Foo]")
Parent.NormalExplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "Type[Foo]")
Parent.SpecialImplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "<typing special form>")
Parent.SpecialExplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "<typing special form>")
Child.NormalImplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "Type[Foo]")
Child.NormalExplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "Type[Foo]")
Child.SpecialImplicit = 4
Child.SpecialExplicit = 4
p.NormalImplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "Type[Foo]")
p.NormalExplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "Type[Foo]")
p.SpecialImplicit = 4
p.SpecialExplicit = 4
c.NormalImplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "Type[Foo]")
c.NormalExplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "Type[Foo]")
c.SpecialImplicit = 4
c.SpecialExplicit = 4
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-medium.pyi]
[case testNewStyleUnionInTypeAliasWithMalformedInstance]
# flags: --python-version 3.10
from typing import List
A = List[int, str] | int # E: "list" expects 1 type argument, but 2 given
B = int | list[int, str] # E: "list" expects 1 type argument, but 2 given
a: A
b: B
reveal_type(a) # N: Revealed type is "Union[builtins.list[Any], builtins.int]"
reveal_type(b) # N: Revealed type is "Union[builtins.int, builtins.list[Any]]"
[case testValidTypeAliasValues]
from typing import TypeVar, Generic, List
T = TypeVar("T", int, str)
S = TypeVar("S", int, bytes)
class C(Generic[T]): ...
class D(C[S]): ... # E: Invalid type argument value for "C"
U = TypeVar("U")
A = List[C[U]]
x: A[bytes] # E: Value of type variable "T" of "C" cannot be "bytes"
V = TypeVar("V", bound=int)
class E(Generic[V]): ...
B = List[E[U]]
y: B[str] # E: Type argument "str" of "E" must be a subtype of "int"
[case testValidTypeAliasValuesMoreRestrictive]
from typing import TypeVar, Generic, List
T = TypeVar("T")
S = TypeVar("S", int, str)
U = TypeVar("U", bound=int)
class C(Generic[T]): ...
A = List[C[S]]
x: A[int]
x_bad: A[bytes] # E: Value of type variable "S" of "A" cannot be "bytes"
B = List[C[U]]
y: B[int]
y_bad: B[str] # E: Type argument "str" of "B" must be a subtype of "int"
[case testTupleWithDifferentArgsPy38]