-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathcheck-literal.test
More file actions
2962 lines (2359 loc) · 106 KB
/
check-literal.test
File metadata and controls
2962 lines (2359 loc) · 106 KB
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
--
-- Check to see how we handle raw types, error handling, and other
-- semantic analysis shenanigans
--
[case testLiteralInvalidString]
from typing_extensions import Literal
def f1(x: 'A[') -> None: pass # E: Invalid type comment or annotation
def g1(x: Literal['A[']) -> None: pass
reveal_type(f1) # N: Revealed type is "def (x: Any)"
reveal_type(g1) # N: Revealed type is "def (x: Literal['A['])"
def f2(x: 'A B') -> None: pass # E: Invalid type comment or annotation
def g2(x: Literal['A B']) -> None: pass
reveal_type(f2) # N: Revealed type is "def (x: Any)"
reveal_type(g2) # N: Revealed type is "def (x: Literal['A B'])"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralInvalidTypeComment]
from typing_extensions import Literal
def f(x): # E: syntax error in type comment "(A[) -> None"
# type: (A[) -> None
pass
[case testLiteralInvalidTypeComment2]
from typing_extensions import Literal
def f(x): # E: Invalid type comment or annotation
# type: ("A[") -> None
pass
def g(x):
# type: (Literal["A["]) -> None
pass
reveal_type(f) # N: Revealed type is "def (x: Any)"
reveal_type(g) # N: Revealed type is "def (x: Literal['A['])"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralFromTypingWorks]
from typing import Literal
x: Literal[42]
x = 43 # E: Incompatible types in assignment (expression has type "Literal[43]", variable has type "Literal[42]")
y: Literal[43]
y = 43
[typing fixtures/typing-medium.pyi]
[case testLiteralInsideOtherTypes]
from typing import Tuple
from typing_extensions import Literal
x: Tuple[1] # E: Invalid type: try using Literal[1] instead?
def foo(x: Tuple[1]) -> None: ... # E: Invalid type: try using Literal[1] instead?
y: Tuple[Literal[2]]
def bar(x: Tuple[Literal[2]]) -> None: ...
reveal_type(x) # N: Revealed type is "Tuple[Any]"
reveal_type(y) # N: Revealed type is "Tuple[Literal[2]]"
reveal_type(bar) # N: Revealed type is "def (x: Tuple[Literal[2]])"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralInsideOtherTypesTypeCommentsPython3]
# flags: --python-version 3.7
from typing import Tuple, Optional
from typing_extensions import Literal
x = None # type: Optional[Tuple[1]] # E: Invalid type: try using Literal[1] instead?
def foo(x): # E: Invalid type: try using Literal[1] instead?
# type: (Tuple[1]) -> None
pass
y = None # type: Optional[Tuple[Literal[2]]]
def bar(x):
# type: (Tuple[Literal[2]]) -> None
pass
reveal_type(x) # N: Revealed type is "Union[Tuple[Any], None]"
reveal_type(y) # N: Revealed type is "Union[Tuple[Literal[2]], None]"
reveal_type(bar) # N: Revealed type is "def (x: Tuple[Literal[2]])"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralValidExpressionsInStringsPython3]
from wrapper import *
[file wrapper.pyi]
from typing_extensions import Literal
alias_1 = Literal['a+b']
alias_2 = Literal['1+2']
alias_3 = Literal['3']
alias_4 = Literal['True']
alias_5 = Literal['None']
alias_6 = Literal['"foo"']
expr_of_alias_1: alias_1
expr_of_alias_2: alias_2
expr_of_alias_3: alias_3
expr_of_alias_4: alias_4
expr_of_alias_5: alias_5
expr_of_alias_6: alias_6
reveal_type(expr_of_alias_1) # N: Revealed type is "Literal['a+b']"
reveal_type(expr_of_alias_2) # N: Revealed type is "Literal['1+2']"
reveal_type(expr_of_alias_3) # N: Revealed type is "Literal['3']"
reveal_type(expr_of_alias_4) # N: Revealed type is "Literal['True']"
reveal_type(expr_of_alias_5) # N: Revealed type is "Literal['None']"
reveal_type(expr_of_alias_6) # N: Revealed type is "Literal['"foo"']"
expr_ann_1: Literal['a+b']
expr_ann_2: Literal['1+2']
expr_ann_3: Literal['3']
expr_ann_4: Literal['True']
expr_ann_5: Literal['None']
expr_ann_6: Literal['"foo"']
reveal_type(expr_ann_1) # N: Revealed type is "Literal['a+b']"
reveal_type(expr_ann_2) # N: Revealed type is "Literal['1+2']"
reveal_type(expr_ann_3) # N: Revealed type is "Literal['3']"
reveal_type(expr_ann_4) # N: Revealed type is "Literal['True']"
reveal_type(expr_ann_5) # N: Revealed type is "Literal['None']"
reveal_type(expr_ann_6) # N: Revealed type is "Literal['"foo"']"
expr_str_1: "Literal['a+b']"
expr_str_2: "Literal['1+2']"
expr_str_3: "Literal['3']"
expr_str_4: "Literal['True']"
expr_str_5: "Literal['None']"
expr_str_6: "Literal['\"foo\"']"
reveal_type(expr_str_1) # N: Revealed type is "Literal['a+b']"
reveal_type(expr_str_2) # N: Revealed type is "Literal['1+2']"
reveal_type(expr_str_3) # N: Revealed type is "Literal['3']"
reveal_type(expr_str_4) # N: Revealed type is "Literal['True']"
reveal_type(expr_str_5) # N: Revealed type is "Literal['None']"
reveal_type(expr_str_6) # N: Revealed type is "Literal['"foo"']"
expr_com_1 = ... # type: Literal['a+b']
expr_com_2 = ... # type: Literal['1+2']
expr_com_3 = ... # type: Literal['3']
expr_com_4 = ... # type: Literal['True']
expr_com_5 = ... # type: Literal['None']
expr_com_6 = ... # type: Literal['"foo"']
reveal_type(expr_com_1) # N: Revealed type is "Literal['a+b']"
reveal_type(expr_com_2) # N: Revealed type is "Literal['1+2']"
reveal_type(expr_com_3) # N: Revealed type is "Literal['3']"
reveal_type(expr_com_4) # N: Revealed type is "Literal['True']"
reveal_type(expr_com_5) # N: Revealed type is "Literal['None']"
reveal_type(expr_com_6) # N: Revealed type is "Literal['"foo"']"
[builtins fixtures/bool.pyi]
[out]
[case testLiteralMixingUnicodeAndBytesPython3]
from typing_extensions import Literal
a_ann: Literal[u"foo"]
b_ann: Literal["foo"]
c_ann: Literal[b"foo"]
a_hint = u"foo" # type: Literal[u"foo"]
b_hint = "foo" # type: Literal["foo"]
c_hint = b"foo" # type: Literal[b"foo"]
AAlias = Literal[u"foo"]
BAlias = Literal["foo"]
CAlias = Literal[b"foo"]
a_alias: AAlias
b_alias: BAlias
c_alias: CAlias
def accepts_str_1(x: Literal[u"foo"]) -> None: pass
def accepts_str_2(x: Literal["foo"]) -> None: pass
def accepts_bytes(x: Literal[b"foo"]) -> None: pass
reveal_type(a_ann) # N: Revealed type is "Literal['foo']"
reveal_type(b_ann) # N: Revealed type is "Literal['foo']"
reveal_type(c_ann) # N: Revealed type is "Literal[b'foo']"
reveal_type(a_hint) # N: Revealed type is "Literal['foo']"
reveal_type(b_hint) # N: Revealed type is "Literal['foo']"
reveal_type(c_hint) # N: Revealed type is "Literal[b'foo']"
reveal_type(a_alias) # N: Revealed type is "Literal['foo']"
reveal_type(b_alias) # N: Revealed type is "Literal['foo']"
reveal_type(c_alias) # N: Revealed type is "Literal[b'foo']"
accepts_str_1(a_ann)
accepts_str_1(b_ann)
accepts_str_1(c_ann) # E: Argument 1 to "accepts_str_1" has incompatible type "Literal[b'foo']"; expected "Literal['foo']"
accepts_str_1(a_hint)
accepts_str_1(b_hint)
accepts_str_1(c_hint) # E: Argument 1 to "accepts_str_1" has incompatible type "Literal[b'foo']"; expected "Literal['foo']"
accepts_str_1(a_alias)
accepts_str_1(b_alias)
accepts_str_1(c_alias) # E: Argument 1 to "accepts_str_1" has incompatible type "Literal[b'foo']"; expected "Literal['foo']"
accepts_str_2(a_ann)
accepts_str_2(b_ann)
accepts_str_2(c_ann) # E: Argument 1 to "accepts_str_2" has incompatible type "Literal[b'foo']"; expected "Literal['foo']"
accepts_str_2(a_hint)
accepts_str_2(b_hint)
accepts_str_2(c_hint) # E: Argument 1 to "accepts_str_2" has incompatible type "Literal[b'foo']"; expected "Literal['foo']"
accepts_str_2(a_alias)
accepts_str_2(b_alias)
accepts_str_2(c_alias) # E: Argument 1 to "accepts_str_2" has incompatible type "Literal[b'foo']"; expected "Literal['foo']"
accepts_bytes(a_ann) # E: Argument 1 to "accepts_bytes" has incompatible type "Literal['foo']"; expected "Literal[b'foo']"
accepts_bytes(b_ann) # E: Argument 1 to "accepts_bytes" has incompatible type "Literal['foo']"; expected "Literal[b'foo']"
accepts_bytes(c_ann)
accepts_bytes(a_hint) # E: Argument 1 to "accepts_bytes" has incompatible type "Literal['foo']"; expected "Literal[b'foo']"
accepts_bytes(b_hint) # E: Argument 1 to "accepts_bytes" has incompatible type "Literal['foo']"; expected "Literal[b'foo']"
accepts_bytes(c_hint)
accepts_bytes(a_alias) # E: Argument 1 to "accepts_bytes" has incompatible type "Literal['foo']"; expected "Literal[b'foo']"
accepts_bytes(b_alias) # E: Argument 1 to "accepts_bytes" has incompatible type "Literal['foo']"; expected "Literal[b'foo']"
accepts_bytes(c_alias)
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralMixingUnicodeAndBytesPython3ForwardStrings]
from typing import TypeVar, Generic
from typing_extensions import Literal
a_unicode_wrapper: u"Literal[u'foo']"
b_unicode_wrapper: u"Literal['foo']"
c_unicode_wrapper: u"Literal[b'foo']"
a_str_wrapper: "Literal[u'foo']"
b_str_wrapper: "Literal['foo']"
c_str_wrapper: "Literal[b'foo']"
# In Python 3, forward references MUST be str, not bytes
a_bytes_wrapper: b"Literal[u'foo']" # E: Invalid type comment or annotation
b_bytes_wrapper: b"Literal['foo']" # E: Invalid type comment or annotation
c_bytes_wrapper: b"Literal[b'foo']" # E: Invalid type comment or annotation
reveal_type(a_unicode_wrapper) # N: Revealed type is "Literal['foo']"
reveal_type(b_unicode_wrapper) # N: Revealed type is "Literal['foo']"
reveal_type(c_unicode_wrapper) # N: Revealed type is "Literal[b'foo']"
reveal_type(a_str_wrapper) # N: Revealed type is "Literal['foo']"
reveal_type(b_str_wrapper) # N: Revealed type is "Literal['foo']"
reveal_type(c_str_wrapper) # N: Revealed type is "Literal[b'foo']"
T = TypeVar('T')
class Wrap(Generic[T]): pass
AUnicodeWrapperAlias = Wrap[u"Literal[u'foo']"]
BUnicodeWrapperAlias = Wrap[u"Literal['foo']"]
CUnicodeWrapperAlias = Wrap[u"Literal[b'foo']"]
a_unicode_wrapper_alias: AUnicodeWrapperAlias
b_unicode_wrapper_alias: BUnicodeWrapperAlias
c_unicode_wrapper_alias: CUnicodeWrapperAlias
AStrWrapperAlias = Wrap["Literal[u'foo']"]
BStrWrapperAlias = Wrap["Literal['foo']"]
CStrWrapperAlias = Wrap["Literal[b'foo']"]
a_str_wrapper_alias: AStrWrapperAlias
b_str_wrapper_alias: BStrWrapperAlias
c_str_wrapper_alias: CStrWrapperAlias
ABytesWrapperAlias = Wrap[b"Literal[u'foo']"]
BBytesWrapperAlias = Wrap[b"Literal['foo']"]
CBytesWrapperAlias = Wrap[b"Literal[b'foo']"]
a_bytes_wrapper_alias: ABytesWrapperAlias
b_bytes_wrapper_alias: BBytesWrapperAlias
c_bytes_wrapper_alias: CBytesWrapperAlias
# In Python 3, we assume that Literal['foo'] and Literal[u'foo'] are always
# equivalent, no matter what.
reveal_type(a_unicode_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal['foo']]"
reveal_type(b_unicode_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal['foo']]"
reveal_type(c_unicode_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal[b'foo']]"
reveal_type(a_str_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal['foo']]"
reveal_type(b_str_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal['foo']]"
reveal_type(c_str_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal[b'foo']]"
reveal_type(a_bytes_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal['foo']]"
reveal_type(b_bytes_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal['foo']]"
reveal_type(c_bytes_wrapper_alias) # N: Revealed type is "__main__.Wrap[Literal[b'foo']]"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralUnicodeWeirdCharacters]
from typing import Any
from typing_extensions import Literal
a1: Literal["\x00\xAC\x62 \u2227 \u03bb(p)"]
b1: Literal["\x00¬b ∧ λ(p)"]
c1: Literal["¬b ∧ λ(p)"]
d1: Literal["\U0001F600"]
e1: Literal["😀"]
Alias1 = Literal["\x00\xAC\x62 \u2227 \u03bb(p)"]
Alias2 = Literal["\x00¬b ∧ λ(p)"]
Alias3 = Literal["¬b ∧ λ(p)"]
Alias4 = Literal["\U0001F600"]
Alias5 = Literal["😀"]
a2: Alias1
b2: Alias2
c2: Alias3
d2: Alias4
e2: Alias5
blah: Any
a3 = blah # type: Literal["\x00\xAC\x62 \u2227 \u03bb(p)"]
b3 = blah # type: Literal["\x00¬b ∧ λ(p)"]
c3 = blah # type: Literal["¬b ∧ λ(p)"]
d3 = blah # type: Literal["\U0001F600"]
e3 = blah # type: Literal["😀"]
reveal_type(a1) # N: Revealed type is "Literal['\x00¬b ∧ λ(p)']"
reveal_type(b1) # N: Revealed type is "Literal['\x00¬b ∧ λ(p)']"
reveal_type(c1) # N: Revealed type is "Literal['¬b ∧ λ(p)']"
reveal_type(d1) # N: Revealed type is "Literal['😀']"
reveal_type(e1) # N: Revealed type is "Literal['😀']"
reveal_type(a2) # N: Revealed type is "Literal['\x00¬b ∧ λ(p)']"
reveal_type(b2) # N: Revealed type is "Literal['\x00¬b ∧ λ(p)']"
reveal_type(c2) # N: Revealed type is "Literal['¬b ∧ λ(p)']"
reveal_type(d2) # N: Revealed type is "Literal['😀']"
reveal_type(e2) # N: Revealed type is "Literal['😀']"
reveal_type(a3) # N: Revealed type is "Literal['\x00¬b ∧ λ(p)']"
reveal_type(b3) # N: Revealed type is "Literal['\x00¬b ∧ λ(p)']"
reveal_type(c3) # N: Revealed type is "Literal['¬b ∧ λ(p)']"
reveal_type(d3) # N: Revealed type is "Literal['😀']"
reveal_type(e3) # N: Revealed type is "Literal['😀']"
a1 = b1
a1 = c1 # E: Incompatible types in assignment (expression has type "Literal['¬b ∧ λ(p)']", variable has type "Literal['\x00¬b ∧ λ(p)']")
a1 = a2
a1 = b2
a1 = c2 # E: Incompatible types in assignment (expression has type "Literal['¬b ∧ λ(p)']", variable has type "Literal['\x00¬b ∧ λ(p)']")
a1 = a3
a1 = b3
a1 = c3 # E: Incompatible types in assignment (expression has type "Literal['¬b ∧ λ(p)']", variable has type "Literal['\x00¬b ∧ λ(p)']")
[builtins fixtures/tuple.pyi]
[out skip-path-normalization]
[case testLiteralRenamingImportWorks]
from typing_extensions import Literal as Foo
x: Foo[3]
reveal_type(x) # N: Revealed type is "Literal[3]"
y: Foo["hello"]
reveal_type(y) # N: Revealed type is "Literal['hello']"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralRenamingImportViaAnotherImportWorks]
from other_module import Foo, Bar
x: Foo[3]
y: Bar
reveal_type(x) # N: Revealed type is "Literal[3]"
reveal_type(y) # N: Revealed type is "Literal[4]"
[file other_module.py]
from typing_extensions import Literal as Foo
Bar = Foo[4]
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralRenamingImportNameConfusion]
from typing_extensions import Literal as Foo
x: Foo["Foo"]
reveal_type(x) # N: Revealed type is "Literal['Foo']"
y: Foo[Foo] # E: Literal[...] must have at least one parameter
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralBadRawExpressionWithBadType]
NotAType = 3
def f() -> NotAType['also' + 'not' + 'a' + 'type']: ... # E: Variable "__main__.NotAType" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases \
# E: Invalid type comment or annotation
# Note: this makes us re-inspect the type (e.g. via '_patch_indirect_dependencies'
# in build.py) so we can confirm the RawExpressionType did not leak out.
indirect = f()
[out]
--
-- Check to make sure we can construct the correct range of literal
-- types (and correctly reject invalid literal types)
--
-- Note: the assignment tests exercise the logic in 'fastparse.py';
-- the type alias tests exercise the logic in 'exprtotype.py'.
--
[case testLiteralBasicIntUsage]
from typing_extensions import Literal
a1: Literal[4]
b1: Literal[0x2a]
c1: Literal[-300]
reveal_type(a1) # N: Revealed type is "Literal[4]"
reveal_type(b1) # N: Revealed type is "Literal[42]"
reveal_type(c1) # N: Revealed type is "Literal[-300]"
a2t = Literal[4]
b2t = Literal[0x2a]
c2t = Literal[-300]
a2: a2t
b2: b2t
c2: c2t
reveal_type(a2) # N: Revealed type is "Literal[4]"
reveal_type(b2) # N: Revealed type is "Literal[42]"
reveal_type(c2) # N: Revealed type is "Literal[-300]"
def f1(x: Literal[4]) -> Literal[4]: pass
def f2(x: Literal[0x2a]) -> Literal[0x2a]: pass
def f3(x: Literal[-300]) -> Literal[-300]: pass
reveal_type(f1) # N: Revealed type is "def (x: Literal[4]) -> Literal[4]"
reveal_type(f2) # N: Revealed type is "def (x: Literal[42]) -> Literal[42]"
reveal_type(f3) # N: Revealed type is "def (x: Literal[-300]) -> Literal[-300]"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralBasicBoolUsage]
from typing_extensions import Literal
a1: Literal[True]
b1: Literal[False]
reveal_type(a1) # N: Revealed type is "Literal[True]"
reveal_type(b1) # N: Revealed type is "Literal[False]"
a2t = Literal[True]
b2t = Literal[False]
a2: a2t
b2: b2t
reveal_type(a2) # N: Revealed type is "Literal[True]"
reveal_type(b2) # N: Revealed type is "Literal[False]"
def f1(x: Literal[True]) -> Literal[True]: pass
def f2(x: Literal[False]) -> Literal[False]: pass
reveal_type(f1) # N: Revealed type is "def (x: Literal[True]) -> Literal[True]"
reveal_type(f2) # N: Revealed type is "def (x: Literal[False]) -> Literal[False]"
[builtins fixtures/bool.pyi]
[out]
[case testLiteralBasicStrUsage]
from typing_extensions import Literal
a: Literal[""]
b: Literal[" foo bar "]
c: Literal[' foo bar ']
d: Literal["foo"]
e: Literal['foo']
reveal_type(a) # N: Revealed type is "Literal['']"
reveal_type(b) # N: Revealed type is "Literal[' foo bar ']"
reveal_type(c) # N: Revealed type is "Literal[' foo bar ']"
reveal_type(d) # N: Revealed type is "Literal['foo']"
reveal_type(e) # N: Revealed type is "Literal['foo']"
def f1(x: Literal[""]) -> Literal[""]: pass
def f2(x: Literal[" foo bar "]) -> Literal[" foo bar "]: pass
def f3(x: Literal[' foo bar ']) -> Literal[' foo bar ']: pass
def f4(x: Literal["foo"]) -> Literal["foo"]: pass
def f5(x: Literal['foo']) -> Literal['foo']: pass
reveal_type(f1) # N: Revealed type is "def (x: Literal['']) -> Literal['']"
reveal_type(f2) # N: Revealed type is "def (x: Literal[' foo bar ']) -> Literal[' foo bar ']"
reveal_type(f3) # N: Revealed type is "def (x: Literal[' foo bar ']) -> Literal[' foo bar ']"
reveal_type(f4) # N: Revealed type is "def (x: Literal['foo']) -> Literal['foo']"
reveal_type(f5) # N: Revealed type is "def (x: Literal['foo']) -> Literal['foo']"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralBasicStrUsageSlashes]
from typing_extensions import Literal
a: Literal[r"foo\nbar"]
b: Literal["foo\nbar"]
reveal_type(a)
reveal_type(b)
[builtins fixtures/tuple.pyi]
[out skip-path-normalization]
main:6: note: Revealed type is "Literal['foo\\nbar']"
main:7: note: Revealed type is "Literal['foo\nbar']"
[case testLiteralBasicNoneUsage]
# Note: Literal[None] and None are equivalent
from typing_extensions import Literal
a: Literal[None]
reveal_type(a) # N: Revealed type is "None"
def f1(x: Literal[None]) -> None: pass
def f2(x: None) -> Literal[None]: pass
def f3(x: Literal[None]) -> Literal[None]: pass
reveal_type(f1) # N: Revealed type is "def (x: None)"
reveal_type(f2) # N: Revealed type is "def (x: None)"
reveal_type(f3) # N: Revealed type is "def (x: None)"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralCallingUnionFunction]
from typing_extensions import Literal
def func(x: Literal['foo', 'bar', ' foo ']) -> None: ...
func('foo')
func('bar')
func(' foo ')
func('baz') # E: Argument 1 to "func" has incompatible type "Literal['baz']"; expected "Literal['foo', 'bar', ' foo ']"
a: Literal['foo']
b: Literal['bar']
c: Literal[' foo ']
d: Literal['foo', 'bar']
e: Literal['foo', 'bar', ' foo ']
f: Literal['foo', 'bar', 'baz']
func(a)
func(b)
func(c)
func(d)
func(e)
func(f) # E: Argument 1 to "func" has incompatible type "Literal['foo', 'bar', 'baz']"; expected "Literal['foo', 'bar', ' foo ']"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralDisallowAny]
from typing import Any
from typing_extensions import Literal
from missing_module import BadAlias # E: Cannot find implementation or library stub for module named "missing_module" \
# N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
a: Literal[Any] # E: Parameter 1 of Literal[...] cannot be of type "Any"
b: Literal[BadAlias] # E: Parameter 1 of Literal[...] cannot be of type "Any"
reveal_type(a) # N: Revealed type is "Any"
reveal_type(b) # N: Revealed type is "Any"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralDisallowActualTypes]
from typing_extensions import Literal
a: Literal[int] # E: Parameter 1 of Literal[...] is invalid
b: Literal[float] # E: Parameter 1 of Literal[...] is invalid
c: Literal[bool] # E: Parameter 1 of Literal[...] is invalid
d: Literal[str] # E: Parameter 1 of Literal[...] is invalid
reveal_type(a) # N: Revealed type is "Any"
reveal_type(b) # N: Revealed type is "Any"
reveal_type(c) # N: Revealed type is "Any"
reveal_type(d) # N: Revealed type is "Any"
[builtins fixtures/primitives.pyi]
[out]
[case testLiteralDisallowFloatsAndComplex]
from typing_extensions import Literal
a1: Literal[3.14] # E: Parameter 1 of Literal[...] cannot be of type "float"
b1: 3.14 # E: Invalid type: float literals cannot be used as a type
c1: Literal[3j] # E: Parameter 1 of Literal[...] cannot be of type "complex"
d1: 3j # E: Invalid type: complex literals cannot be used as a type
a2t = Literal[3.14] # E: Parameter 1 of Literal[...] cannot be of type "float"
b2t = 3.14
c2t = Literal[3j] # E: Parameter 1 of Literal[...] cannot be of type "complex"
d2t = 3j
a2: a2t
reveal_type(a2) # N: Revealed type is "Any"
b2: b2t # E: Variable "__main__.b2t" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
c2: c2t
reveal_type(c2) # N: Revealed type is "Any"
d2: d2t # E: Variable "__main__.d2t" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
[builtins fixtures/complex_tuple.pyi]
[out]
[case testLiteralDisallowComplexExpressions]
from typing_extensions import Literal
def dummy() -> int: return 3
a: Literal[3 + 4] # E: Invalid type: Literal[...] cannot contain arbitrary expressions
b: Literal[" foo ".trim()] # E: Invalid type: Literal[...] cannot contain arbitrary expressions
c: Literal[+42] # E: Invalid type: Literal[...] cannot contain arbitrary expressions
d: Literal[~12] # E: Invalid type: Literal[...] cannot contain arbitrary expressions
e: Literal[dummy()] # E: Invalid type: Literal[...] cannot contain arbitrary expressions
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralDisallowCollections]
from typing_extensions import Literal
a: Literal[{"a": 1, "b": 2}] # E: Invalid type: Literal[...] cannot contain arbitrary expressions
b: Literal[{1, 2, 3}] # E: Invalid type: Literal[...] cannot contain arbitrary expressions
c: {"a": 1, "b": 2} # E: Invalid type comment or annotation
d: {1, 2, 3} # E: Invalid type comment or annotation
[builtins fixtures/tuple.pyi]
[case testLiteralDisallowCollections2]
from typing_extensions import Literal
a: (1, 2, 3) # E: Syntax error in type annotation \
# N: Suggestion: Use Tuple[T1, ..., Tn] instead of (T1, ..., Tn)
b: Literal[[1, 2, 3]] # E: Parameter 1 of Literal[...] is invalid
c: [1, 2, 3] # E: Bracketed expression "[...]" is not valid as a type \
# N: Did you mean "List[...]"?
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralDisallowCollectionsTypeAlias]
from typing_extensions import Literal
at = Literal[{"a": 1, "b": 2}] # E: Invalid type alias: expression is not a valid type
bt = {"a": 1, "b": 2}
a: at # E: Variable "__main__.at" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
b: bt # E: Variable "__main__.bt" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
[builtins fixtures/dict.pyi]
[out]
[case testLiteralDisallowCollectionsTypeAlias2]
from typing_extensions import Literal
at = Literal[{1, 2, 3}] # E: Invalid type alias: expression is not a valid type
bt = {1, 2, 3}
a: at # E: Variable "__main__.at" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
b: bt # E: Variable "__main__.bt" is not valid as a type \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
[builtins fixtures/set.pyi]
[out]
[case testLiteralDisallowTypeVar]
from typing import TypeVar, Tuple
from typing_extensions import Literal
T = TypeVar('T')
at = Literal[T] # E: Parameter 1 of Literal[...] is invalid
a: at
def foo(b: Literal[T]) -> Tuple[T]: pass # E: Parameter 1 of Literal[...] is invalid
[builtins fixtures/tuple.pyi]
[out]
--
-- Test mixing and matching literals with other types
--
[case testLiteralMultipleValues]
# flags: --strict-optional
from typing_extensions import Literal
a: Literal[1, 2, 3]
b: Literal["a", "b", "c"]
c: Literal[1, "b", True, None]
d: Literal[1, 1, 1]
e: Literal[None, None, None]
reveal_type(a) # N: Revealed type is "Union[Literal[1], Literal[2], Literal[3]]"
reveal_type(b) # N: Revealed type is "Union[Literal['a'], Literal['b'], Literal['c']]"
reveal_type(c) # N: Revealed type is "Union[Literal[1], Literal['b'], Literal[True], None]"
# Note: I was thinking these should be simplified, but it seems like
# mypy doesn't simplify unions with duplicate values with other types.
reveal_type(d) # N: Revealed type is "Union[Literal[1], Literal[1], Literal[1]]"
reveal_type(e) # N: Revealed type is "Union[None, None, None]"
[builtins fixtures/bool.pyi]
[out]
[case testLiteralMultipleValuesExplicitTuple]
from typing_extensions import Literal
# Unfortunately, it seems like typed_ast is unable to distinguish this from
# Literal[1, 2, 3]. So we treat the two as being equivalent for now.
a: Literal[1, 2, 3]
b: Literal[(1, 2, 3)]
reveal_type(a) # N: Revealed type is "Union[Literal[1], Literal[2], Literal[3]]"
reveal_type(b) # N: Revealed type is "Union[Literal[1], Literal[2], Literal[3]]"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralNestedUsage]
# flags: --strict-optional
from typing_extensions import Literal
a: Literal[Literal[3], 4, Literal["foo"]]
reveal_type(a) # N: Revealed type is "Union[Literal[3], Literal[4], Literal['foo']]"
alias_for_literal = Literal[5]
b: Literal[alias_for_literal]
reveal_type(b) # N: Revealed type is "Literal[5]"
another_alias = Literal[1, None]
c: Literal[alias_for_literal, another_alias, "r"]
reveal_type(c) # N: Revealed type is "Union[Literal[5], Literal[1], None, Literal['r']]"
basic_mode = Literal["r", "w", "a"]
basic_with_plus = Literal["r+", "w+", "a+"]
combined: Literal[basic_mode, basic_with_plus]
reveal_type(combined) # N: Revealed type is "Union[Literal['r'], Literal['w'], Literal['a'], Literal['r+'], Literal['w+'], Literal['a+']]"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralBiasTowardsAssumingForwardReference]
from typing_extensions import Literal
a: "Foo"
reveal_type(a) # N: Revealed type is "__main__.Foo"
b: Literal["Foo"]
reveal_type(b) # N: Revealed type is "Literal['Foo']"
c: "Literal[Foo]" # E: Parameter 1 of Literal[...] is invalid
d: "Literal['Foo']"
reveal_type(d) # N: Revealed type is "Literal['Foo']"
class Foo: pass
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralBiasTowardsAssumingForwardReferenceForTypeAliases]
from typing_extensions import Literal
a: "Foo"
reveal_type(a) # N: Revealed type is "Literal[5]"
b: Literal["Foo"]
reveal_type(b) # N: Revealed type is "Literal['Foo']"
c: "Literal[Foo]"
reveal_type(c) # N: Revealed type is "Literal[5]"
d: "Literal['Foo']"
reveal_type(d) # N: Revealed type is "Literal['Foo']"
e: Literal[Foo, 'Foo']
reveal_type(e) # N: Revealed type is "Union[Literal[5], Literal['Foo']]"
Foo = Literal[5]
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralBiasTowardsAssumingForwardReferencesForTypeComments]
from typing_extensions import Literal
a = None # type: Foo
reveal_type(a) # N: Revealed type is "__main__.Foo"
b = None # type: "Foo"
reveal_type(b) # N: Revealed type is "__main__.Foo"
c = None # type: Literal["Foo"]
reveal_type(c) # N: Revealed type is "Literal['Foo']"
d = None # type: Literal[Foo] # E: Parameter 1 of Literal[...] is invalid
class Foo: pass
[builtins fixtures/tuple.pyi]
[out]
--
-- Check how we handle very basic subtyping and other useful things
--
[case testLiteralCallingFunction]
from typing_extensions import Literal
def foo(x: Literal[3]) -> None: pass
a: Literal[1]
b: Literal[2]
c: int
foo(a) # E: Argument 1 to "foo" has incompatible type "Literal[1]"; expected "Literal[3]"
foo(b) # E: Argument 1 to "foo" has incompatible type "Literal[2]"; expected "Literal[3]"
foo(c) # E: Argument 1 to "foo" has incompatible type "int"; expected "Literal[3]"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralCallingFunctionWithUnionLiteral]
from typing_extensions import Literal
def foo(x: Literal[1, 2, 3]) -> None: pass
a: Literal[1]
b: Literal[2, 3]
c: Literal[4, 5]
d: int
foo(a)
foo(b)
foo(c) # E: Argument 1 to "foo" has incompatible type "Literal[4, 5]"; expected "Literal[1, 2, 3]"
foo(d) # E: Argument 1 to "foo" has incompatible type "int"; expected "Literal[1, 2, 3]"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralCallingFunctionWithStandardBase]
from typing_extensions import Literal
def foo(x: int) -> None: pass
a: Literal[1]
b: Literal[1, -4]
c: Literal[4, 'foo']
foo(a)
foo(b)
foo(c) # E: Argument 1 to "foo" has incompatible type "Literal[4, 'foo']"; expected "int"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralCheckSubtypingStrictOptional]
# flags: --strict-optional
from typing import Any, NoReturn
from typing_extensions import Literal
lit: Literal[1]
def f_lit(x: Literal[1]) -> None: pass
def fa(x: Any) -> None: pass
def fb(x: NoReturn) -> None: pass
def fc(x: None) -> None: pass
a: Any
b: NoReturn
c: None
fa(lit)
fb(lit) # E: Argument 1 to "fb" has incompatible type "Literal[1]"; expected "NoReturn"
fc(lit) # E: Argument 1 to "fc" has incompatible type "Literal[1]"; expected "None"
f_lit(a)
f_lit(b)
f_lit(c) # E: Argument 1 to "f_lit" has incompatible type "None"; expected "Literal[1]"
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralCheckSubtypingNoStrictOptional]
# flags: --no-strict-optional
from typing import Any, NoReturn
from typing_extensions import Literal
lit: Literal[1]
def f_lit(x: Literal[1]) -> None: pass
def fa(x: Any) -> None: pass
def fb(x: NoReturn) -> None: pass
def fc(x: None) -> None: pass
a: Any
b: NoReturn
c: None
fa(lit)
fb(lit) # E: Argument 1 to "fb" has incompatible type "Literal[1]"; expected "NoReturn"
fc(lit) # E: Argument 1 to "fc" has incompatible type "Literal[1]"; expected "None"
f_lit(a)
f_lit(b)
f_lit(c)
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralCallingOverloadedFunction]
from typing import overload, Generic, TypeVar, Any
from typing_extensions import Literal
T = TypeVar('T')
class IOLike(Generic[T]): pass
@overload
def foo(x: Literal[1]) -> IOLike[int]: ...
@overload
def foo(x: Literal[2]) -> IOLike[str]: ...
@overload
def foo(x: int) -> IOLike[Any]: ...
def foo(x: int) -> IOLike[Any]:
if x == 1:
return IOLike[int]()
elif x == 2:
return IOLike[str]()
else:
return IOLike()
a: Literal[1]
b: Literal[2]
c: int
d: Literal[3]
reveal_type(foo(a)) # N: Revealed type is "__main__.IOLike[builtins.int]"
reveal_type(foo(b)) # N: Revealed type is "__main__.IOLike[builtins.str]"
reveal_type(foo(c)) # N: Revealed type is "__main__.IOLike[Any]"
foo(d)
[builtins fixtures/ops.pyi]
[out]
[case testLiteralVariance]
from typing import Generic, TypeVar
from typing_extensions import Literal
T = TypeVar('T')
T_co = TypeVar('T_co', covariant=True)
T_contra = TypeVar('T_contra', contravariant=True)
class Invariant(Generic[T]): pass
class Covariant(Generic[T_co]): pass
class Contravariant(Generic[T_contra]): pass
a1: Invariant[Literal[1]]
a2: Invariant[Literal[1, 2]]
a3: Invariant[Literal[1, 2, 3]]
a2 = a1 # E: Incompatible types in assignment (expression has type "Invariant[Literal[1]]", variable has type "Invariant[Literal[1, 2]]")
a2 = a3 # E: Incompatible types in assignment (expression has type "Invariant[Literal[1, 2, 3]]", variable has type "Invariant[Literal[1, 2]]")
b1: Covariant[Literal[1]]
b2: Covariant[Literal[1, 2]]
b3: Covariant[Literal[1, 2, 3]]
b2 = b1
b2 = b3 # E: Incompatible types in assignment (expression has type "Covariant[Literal[1, 2, 3]]", variable has type "Covariant[Literal[1, 2]]")
c1: Contravariant[Literal[1]]
c2: Contravariant[Literal[1, 2]]
c3: Contravariant[Literal[1, 2, 3]]
c2 = c1 # E: Incompatible types in assignment (expression has type "Contravariant[Literal[1]]", variable has type "Contravariant[Literal[1, 2]]")
c2 = c3
[builtins fixtures/tuple.pyi]
[out]
[case testLiteralInListAndSequence]
from typing import List, Sequence
from typing_extensions import Literal
def foo(x: List[Literal[1, 2]]) -> None: pass
def bar(x: Sequence[Literal[1, 2]]) -> None: pass
a: List[Literal[1]]
b: List[Literal[1, 2, 3]]
foo(a) # E: Argument 1 to "foo" has incompatible type "List[Literal[1]]"; expected "List[Literal[1, 2]]" \
# N: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
# N: Consider using "Sequence" instead, which is covariant
foo(b) # E: Argument 1 to "foo" has incompatible type "List[Literal[1, 2, 3]]"; expected "List[Literal[1, 2]]"
bar(a)
bar(b) # E: Argument 1 to "bar" has incompatible type "List[Literal[1, 2, 3]]"; expected "Sequence[Literal[1, 2]]"
[builtins fixtures/list.pyi]
[out]
[case testLiteralRenamingDoesNotChangeTypeChecking]
from typing_extensions import Literal as Foo
from other_module import Bar1, Bar2, c
def func(x: Foo[15]) -> None: pass
a: Bar1
b: Bar2
func(a)
func(b) # E: Argument 1 to "func" has incompatible type "Literal[14]"; expected "Literal[15]"
func(c)
[file other_module.py]
from typing_extensions import Literal
Bar1 = Literal[15]
Bar2 = Literal[14]
c: Literal[15]
[builtins fixtures/tuple.pyi]
--
-- Check to make sure we handle inference of literal values correctly,
-- especially when doing assignments or calls
--
[case testLiteralInferredInAssignment]
from typing_extensions import Literal
int1: Literal[1] = 1
int2 = 1
int3: int = 1
str1: Literal["foo"] = "foo"
str2 = "foo"
str3: str = "foo"
bool1: Literal[True] = True
bool2 = True
bool3: bool = True
none1: Literal[None] = None
none2 = None
none3: None = None