-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
check-type-aliases.test
730 lines (584 loc) · 16.7 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
[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]
# 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]
# 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]
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]
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]
# Recursive aliases are not supported yet.
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]
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 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]