@@ -713,6 +713,7 @@ elif x is Foo.C:
713713 reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.C]'
714714else:
715715 reveal_type(x) # No output here: this branch is unreachable
716+ reveal_type(x) # N: Revealed type is '__main__.Foo'
716717
717718if Foo.A is x:
718719 reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
@@ -722,6 +723,7 @@ elif Foo.C is x:
722723 reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.C]'
723724else:
724725 reveal_type(x) # No output here: this branch is unreachable
726+ reveal_type(x) # N: Revealed type is '__main__.Foo'
725727
726728y: Foo
727729if y is Foo.A:
@@ -732,6 +734,7 @@ elif y is Foo.C:
732734 reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.C]'
733735else:
734736 reveal_type(y) # No output here: this branch is unreachable
737+ reveal_type(y) # N: Revealed type is '__main__.Foo'
735738
736739if Foo.A is y:
737740 reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
@@ -741,6 +744,7 @@ elif Foo.C is y:
741744 reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.C]'
742745else:
743746 reveal_type(y) # No output here: this branch is unreachable
747+ reveal_type(y) # N: Revealed type is '__main__.Foo'
744748[builtins fixtures/bool.pyi]
745749
746750[case testEnumReachabilityChecksWithOrdering]
@@ -815,12 +819,14 @@ if x is y:
815819else:
816820 reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
817821 reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
822+ reveal_type(x) # N: Revealed type is '__main__.Foo'
818823if y is x:
819824 reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
820825 reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
821826else:
822827 reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
823828 reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
829+ reveal_type(x) # N: Revealed type is '__main__.Foo'
824830
825831if x is z:
826832 reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
@@ -830,6 +836,7 @@ else:
830836 reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
831837 reveal_type(z) # N: Revealed type is 'Literal[__main__.Foo.A]?'
832838 accepts_foo_a(z)
839+ reveal_type(x) # N: Revealed type is '__main__.Foo'
833840if z is x:
834841 reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
835842 reveal_type(z) # N: Revealed type is 'Literal[__main__.Foo.A]?'
@@ -838,6 +845,7 @@ else:
838845 reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
839846 reveal_type(z) # N: Revealed type is 'Literal[__main__.Foo.A]?'
840847 accepts_foo_a(z)
848+ reveal_type(x) # N: Revealed type is '__main__.Foo'
841849
842850if y is z:
843851 reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
@@ -909,6 +917,7 @@ if x is Foo.A:
909917 reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
910918else:
911919 reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C], None]'
920+ reveal_type(x) # N: Revealed type is 'Union[__main__.Foo, None]'
912921[builtins fixtures/bool.pyi]
913922
914923[case testEnumReachabilityWithMultipleEnums]
@@ -928,18 +937,21 @@ if x1 is Foo.A:
928937 reveal_type(x1) # N: Revealed type is 'Literal[__main__.Foo.A]'
929938else:
930939 reveal_type(x1) # N: Revealed type is 'Union[Literal[__main__.Foo.B], __main__.Bar]'
940+ reveal_type(x1) # N: Revealed type is 'Union[__main__.Foo, __main__.Bar]'
931941
932942x2: Union[Foo, Bar]
933943if x2 is Bar.A:
934944 reveal_type(x2) # N: Revealed type is 'Literal[__main__.Bar.A]'
935945else:
936946 reveal_type(x2) # N: Revealed type is 'Union[__main__.Foo, Literal[__main__.Bar.B]]'
947+ reveal_type(x2) # N: Revealed type is 'Union[__main__.Foo, __main__.Bar]'
937948
938949x3: Union[Foo, Bar]
939950if x3 is Foo.A or x3 is Bar.A:
940951 reveal_type(x3) # N: Revealed type is 'Union[Literal[__main__.Foo.A], Literal[__main__.Bar.A]]'
941952else:
942953 reveal_type(x3) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Bar.B]]'
954+ reveal_type(x3) # N: Revealed type is 'Union[__main__.Foo, __main__.Bar]'
943955
944956[builtins fixtures/bool.pyi]
945957
@@ -1299,3 +1311,25 @@ reveal_type(a._value_) # N: Revealed type is 'Any'
12991311[builtins fixtures/__new__.pyi]
13001312[builtins fixtures/primitives.pyi]
13011313[typing fixtures/typing-medium.pyi]
1314+
1315+ [case testEnumNarrowedToTwoLiterals]
1316+ # Regression test: two literals of an enum would be joined
1317+ # as the full type, regardless of the amount of elements
1318+ # the enum contains.
1319+ from enum import Enum
1320+ from typing import Union
1321+ from typing_extensions import Literal
1322+
1323+ class Foo(Enum):
1324+ A = 1
1325+ B = 2
1326+ C = 3
1327+
1328+ def f(x: Foo):
1329+ if x is Foo.A:
1330+ return x
1331+ if x is Foo.B:
1332+ pass
1333+ reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
1334+
1335+ [builtins fixtures/bool.pyi]
0 commit comments