File tree 3 files changed +28
-1
lines changed 3 files changed +28
-1
lines changed Original file line number Diff line number Diff line change 67
67
UnionType ,
68
68
UnpackType ,
69
69
find_unpack_in_list ,
70
+ flatten_nested_unions ,
70
71
get_proper_type ,
71
72
is_named_instance ,
72
73
split_with_prefix_and_suffix ,
@@ -327,7 +328,9 @@ def _is_subtype(
327
328
and isinstance (left , Instance )
328
329
and (left .type .is_enum or left .type .fullname == "builtins.bool" )
329
330
):
330
- right = UnionType (mypy .typeops .try_contracting_literals_in_union (right .items ))
331
+ right = UnionType (
332
+ mypy .typeops .try_contracting_literals_in_union (flatten_nested_unions (right .items ))
333
+ )
331
334
if proper_subtype :
332
335
is_subtype_of_item = any (
333
336
is_proper_subtype (orig_left , item , subtype_context = subtype_context )
Original file line number Diff line number Diff line change @@ -1069,6 +1069,8 @@ class Status(Enum):
1069
1069
def try_contracting_literals_in_union (types : Sequence [Type ]) -> list [ProperType ]:
1070
1070
"""Contracts any literal types back into a sum type if possible.
1071
1071
1072
+ Requires a flattened union and does not descend into children.
1073
+
1072
1074
Will replace the first instance of the literal with the sum type and
1073
1075
remove all others.
1074
1076
Original file line number Diff line number Diff line change @@ -2765,6 +2765,28 @@ reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.A]"
2765
2765
reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A]"
2766
2766
[builtins fixtures/tuple.pyi]
2767
2767
2768
+ [case testLiteralUnionEnumAliasAssignable]
2769
+ from enum import Enum
2770
+ from typing import Literal, Union
2771
+
2772
+ class E(Enum):
2773
+ A = 'a'
2774
+ B = 'b'
2775
+ C = 'c'
2776
+
2777
+ A = Literal[E.A]
2778
+ B = Literal[E.B, E.C]
2779
+
2780
+ def f(x: Union[A, B]) -> None: ...
2781
+ def f2(x: Union[A, Literal[E.B, E.C]]) -> None: ...
2782
+ def f3(x: Union[Literal[E.A], B]) -> None: ...
2783
+
2784
+ def main(x: E) -> None:
2785
+ f(x)
2786
+ f2(x)
2787
+ f3(x)
2788
+ [builtins fixtures/tuple.pyi]
2789
+
2768
2790
[case testStrictEqualityLiteralTrueVsFalse]
2769
2791
# mypy: strict-equality
2770
2792
You can’t perform that action at this time.
0 commit comments