@@ -632,6 +632,7 @@ elif x is Foo.C:
632
632
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.C]'
633
633
else:
634
634
reveal_type(x) # No output here: this branch is unreachable
635
+ reveal_type(y) # N: Revealed type is '__main__.Foo'
635
636
636
637
if Foo.A is x:
637
638
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
@@ -641,6 +642,7 @@ elif Foo.C is x:
641
642
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.C]'
642
643
else:
643
644
reveal_type(x) # No output here: this branch is unreachable
645
+ reveal_type(y) # N: Revealed type is '__main__.Foo'
644
646
645
647
y: Foo
646
648
if y is Foo.A:
@@ -651,6 +653,7 @@ elif y is Foo.C:
651
653
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.C]'
652
654
else:
653
655
reveal_type(y) # No output here: this branch is unreachable
656
+ reveal_type(y) # N: Revealed type is '__main__.Foo'
654
657
655
658
if Foo.A is y:
656
659
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
@@ -660,6 +663,7 @@ elif Foo.C is y:
660
663
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.C]'
661
664
else:
662
665
reveal_type(y) # No output here: this branch is unreachable
666
+ reveal_type(y) # N: Revealed type is '__main__.Foo'
663
667
[builtins fixtures/bool.pyi]
664
668
665
669
[case testEnumReachabilityChecksWithOrdering]
@@ -734,12 +738,14 @@ if x is y:
734
738
else:
735
739
reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
736
740
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
741
+ reveal_type(x) # N: Revealed type is '__main__.Foo'
737
742
if y is x:
738
743
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
739
744
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
740
745
else:
741
746
reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
742
747
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
748
+ reveal_type(x) # N: Revealed type is '__main__.Foo'
743
749
744
750
if x is z:
745
751
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
@@ -749,6 +755,7 @@ else:
749
755
reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
750
756
reveal_type(z) # N: Revealed type is 'Literal[__main__.Foo.A]?'
751
757
accepts_foo_a(z)
758
+ reveal_type(x) # N: Revealed type is '__main__.Foo'
752
759
if z is x:
753
760
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
754
761
reveal_type(z) # N: Revealed type is 'Literal[__main__.Foo.A]?'
@@ -757,6 +764,7 @@ else:
757
764
reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
758
765
reveal_type(z) # N: Revealed type is 'Literal[__main__.Foo.A]?'
759
766
accepts_foo_a(z)
767
+ reveal_type(x) # N: Revealed type is '__main__.Foo'
760
768
761
769
if y is z:
762
770
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
@@ -828,6 +836,7 @@ if x is Foo.A:
828
836
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
829
837
else:
830
838
reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C], None]'
839
+ reveal_type(x) # N: Revealed type is 'Union[__main__.Foo, None]'
831
840
[builtins fixtures/bool.pyi]
832
841
833
842
[case testEnumReachabilityWithMultipleEnums]
@@ -847,18 +856,21 @@ if x1 is Foo.A:
847
856
reveal_type(x1) # N: Revealed type is 'Literal[__main__.Foo.A]'
848
857
else:
849
858
reveal_type(x1) # N: Revealed type is 'Union[Literal[__main__.Foo.B], __main__.Bar]'
859
+ reveal_type(x1) # N: Revealed type is 'Union[__main__.Foo, __main__.Bar]'
850
860
851
861
x2: Union[Foo, Bar]
852
862
if x2 is Bar.A:
853
863
reveal_type(x2) # N: Revealed type is 'Literal[__main__.Bar.A]'
854
864
else:
855
865
reveal_type(x2) # N: Revealed type is 'Union[__main__.Foo, Literal[__main__.Bar.B]]'
866
+ reveal_type(x2) # N: Revealed type is 'Union[__main__.Foo, __main__.Bar]'
856
867
857
868
x3: Union[Foo, Bar]
858
869
if x3 is Foo.A or x3 is Bar.A:
859
870
reveal_type(x3) # N: Revealed type is 'Union[Literal[__main__.Foo.A], Literal[__main__.Bar.A]]'
860
871
else:
861
872
reveal_type(x3) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Bar.B]]'
873
+ reveal_type(x3) # N: Revealed type is 'Union[__main__.Foo, __main__.Bar]'
862
874
863
875
[builtins fixtures/bool.pyi]
864
876
@@ -877,7 +889,7 @@ def func(x: Union[int, None, Empty] = _empty) -> int:
877
889
# E: Unsupported left operand type for + ("Empty") \
878
890
# N: Left operand is of type "Union[int, None, Empty]"
879
891
if x is _empty:
880
- reveal_type(x) # N: Revealed type is 'Literal[ __main__.Empty.token] '
892
+ reveal_type(x) # N: Revealed type is '__main__.Empty'
881
893
return 0
882
894
elif x is None:
883
895
reveal_type(x) # N: Revealed type is 'None'
@@ -924,7 +936,7 @@ def func(x: Union[int, None, Empty] = _empty) -> int:
924
936
# E: Unsupported left operand type for + ("Empty") \
925
937
# N: Left operand is of type "Union[int, None, Empty]"
926
938
if x is _empty:
927
- reveal_type(x) # N: Revealed type is 'Literal[ __main__.Empty.token] '
939
+ reveal_type(x) # N: Revealed type is '__main__.Empty'
928
940
return 0
929
941
elif x is None:
930
942
reveal_type(x) # N: Revealed type is 'None'
@@ -953,7 +965,7 @@ def func(x: Union[int, None, Empty] = _empty) -> int:
953
965
# E: Unsupported left operand type for + ("Empty") \
954
966
# N: Left operand is of type "Union[int, None, Empty]"
955
967
if x is _empty:
956
- reveal_type(x) # N: Revealed type is 'Literal[ __main__.Empty.token] '
968
+ reveal_type(x) # N: Revealed type is '__main__.Empty'
957
969
return 0
958
970
elif x is None:
959
971
reveal_type(x) # N: Revealed type is 'None'
@@ -1162,3 +1174,25 @@ class Comparator(enum.Enum):
1162
1174
1163
1175
reveal_type(Comparator.__foo__) # N: Revealed type is 'builtins.dict[builtins.str, builtins.int]'
1164
1176
[builtins fixtures/dict.pyi]
1177
+
1178
+ [case testEnumNarrowedToTwoLiterals]
1179
+ # Regression test: two literals of an enum would be joined
1180
+ # as the full type, regardless of the amount of elements
1181
+ # the enum contains.
1182
+ from enum import Enum
1183
+ from typing import Union
1184
+ from typing_extensions import Literal
1185
+
1186
+ class Foo(Enum):
1187
+ A = 1
1188
+ B = 2
1189
+ C = 3
1190
+
1191
+ def f(x: Foo):
1192
+ if x is Foo.A:
1193
+ return x
1194
+ if x is Foo.B:
1195
+ pass
1196
+ reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
1197
+
1198
+ [builtins fixtures/bool.pyi]
0 commit comments