Skip to content

Commit f11017f

Browse files
revert union behavior
1 parent 2235b79 commit f11017f

File tree

3 files changed

+5
-13
lines changed

3 files changed

+5
-13
lines changed

mypy/join.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,6 @@ def visit_unbound_type(self, t: UnboundType) -> ProperType:
274274
def visit_union_type(self, t: UnionType) -> ProperType:
275275
if is_proper_subtype(self.s, t):
276276
return t
277-
elif isinstance(self.s, LiteralType):
278-
# E.g. join("x", "y" | "z") -> "x" | "y" | "z"
279-
# and join(1, "y" | "z") -> object
280-
return mypy.typeops.make_simplified_union([join_types(self.s, x) for x in t.items])
281277
else:
282278
return mypy.typeops.make_simplified_union([self.s, t])
283279

@@ -636,9 +632,7 @@ def visit_literal_type(self, t: LiteralType) -> ProperType:
636632
if t == self.s:
637633
# E.g. Literal["x"], Literal["x"] -> Literal["x"]
638634
return t
639-
if (self.s.fallback.type == t.fallback.type) or (
640-
self.s.fallback.type.is_enum and t.fallback.type.is_enum
641-
):
635+
if self.s.fallback.type.is_enum and t.fallback.type.is_enum:
642636
return mypy.typeops.make_simplified_union([self.s, t])
643637
return join_types(self.s.fallback, t.fallback)
644638
elif isinstance(self.s, Instance) and self.s.last_known_value == t:

mypy/test/testtypes.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,8 +1015,7 @@ def test_literal_type(self) -> None:
10151015
self.assert_join(lit1, lit1, lit1)
10161016
self.assert_join(lit1, a, a)
10171017
self.assert_join(lit1, d, self.fx.o)
1018-
self.assert_simple_join(lit1, lit2, UnionType([lit1, lit2]))
1019-
self.assert_simple_join(lit2, lit1, UnionType([lit2, lit1]))
1018+
self.assert_join(lit1, lit2, a)
10201019
self.assert_join(lit1, lit3, self.fx.o)
10211020
self.assert_join(lit1, self.fx.anyt, self.fx.anyt)
10221021
self.assert_join(UnionType([lit1, lit2]), lit2, UnionType([lit1, lit2]))
@@ -1064,12 +1063,11 @@ def test_mixed_literal_types(self) -> None:
10641063
self.assert_join(str1_inst, str1_inst, str1_inst)
10651064

10661065
# other operand is a different literal
1067-
# "x" , "y" -> "x" | "y" (treat real literals like enum)
1066+
# "x" , "y" -> str (TODO: consider using "x" | "y" (treat real literals like enum))
10681067
# "x" , "y"? -> str
10691068
# "x"?, "y" -> str
10701069
# "x"?, "y"? -> str
1071-
self.assert_simple_join(str1, str2, UnionType([str1, str2]))
1072-
self.assert_simple_join(str2, str1, UnionType([str2, str1]))
1070+
self.assert_join(str1, str2, str_type)
10731071
self.assert_join(str1, str2_inst, str_type)
10741072
self.assert_join(str1_inst, str2_inst, str_type)
10751073

test-data/unit/check-literal.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ arr4 = [lit1, lit2, lit3]
11691169
arr5 = [object(), lit1]
11701170

11711171
reveal_type(arr1) # N: Revealed type is "builtins.list[Literal[1]]"
1172-
reveal_type(arr2) # N: Revealed type is "builtins.list[Union[Literal[1], Literal[2]]]"
1172+
reveal_type(arr2) # N: Revealed type is "builtins.list[builtins.int]"
11731173
reveal_type(arr3) # N: Revealed type is "builtins.list[builtins.int]"
11741174
reveal_type(arr4) # N: Revealed type is "builtins.list[builtins.object]"
11751175
reveal_type(arr5) # N: Revealed type is "builtins.list[builtins.object]"

0 commit comments

Comments
 (0)