Skip to content

Commit 50f5931

Browse files
revert union behavior
1 parent 17aee4f commit 50f5931

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

@@ -631,9 +627,7 @@ def visit_literal_type(self, t: LiteralType) -> ProperType:
631627
if t == self.s:
632628
# E.g. Literal["x"], Literal["x"] -> Literal["x"]
633629
return t
634-
if (self.s.fallback.type == t.fallback.type) or (
635-
self.s.fallback.type.is_enum and t.fallback.type.is_enum
636-
):
630+
if self.s.fallback.type.is_enum and t.fallback.type.is_enum:
637631
return mypy.typeops.make_simplified_union([self.s, t])
638632
return join_types(self.s.fallback, t.fallback)
639633
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
@@ -1027,8 +1027,7 @@ def test_literal_type(self) -> None:
10271027
self.assert_join(lit1, lit1, lit1)
10281028
self.assert_join(lit1, a, a)
10291029
self.assert_join(lit1, d, self.fx.o)
1030-
self.assert_simple_join(lit1, lit2, UnionType([lit1, lit2]))
1031-
self.assert_simple_join(lit2, lit1, UnionType([lit2, lit1]))
1030+
self.assert_join(lit1, lit2, a)
10321031
self.assert_join(lit1, lit3, self.fx.o)
10331032
self.assert_join(lit1, self.fx.anyt, self.fx.anyt)
10341033
self.assert_join(UnionType([lit1, lit2]), lit2, UnionType([lit1, lit2]))
@@ -1076,12 +1075,11 @@ def test_mixed_literal_types(self) -> None:
10761075
self.assert_join(str1_inst, str1_inst, str1_inst)
10771076

10781077
# other operand is a different literal
1079-
# "x" , "y" -> "x" | "y" (treat real literals like enum)
1078+
# "x" , "y" -> str (TODO: consider using "x" | "y" (treat real literals like enum))
10801079
# "x" , "y"? -> str
10811080
# "x"?, "y" -> str
10821081
# "x"?, "y"? -> str
1083-
self.assert_simple_join(str1, str2, UnionType([str1, str2]))
1084-
self.assert_simple_join(str2, str1, UnionType([str2, str1]))
1082+
self.assert_join(str1, str2, str_type)
10851083
self.assert_join(str1, str2_inst, str_type)
10861084
self.assert_join(str1_inst, str2_inst, str_type)
10871085

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)