Skip to content

Commit fb94f80

Browse files
authored
Fix join of Any against a union type (#12068)
Make the join of a union type and Any commutative. Previously the result depended on the order of the operands, which was clearly incorrect. Fix #12051, but other use cases are affected as well. This change was split off from #12054.
1 parent 3680449 commit fb94f80

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

mypy/join.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,15 @@ def join_types(s: Type, t: Type, instance_joiner: Optional[InstanceJoiner] = Non
175175
s = mypy.typeops.true_or_false(s)
176176
t = mypy.typeops.true_or_false(t)
177177

178+
if isinstance(s, UnionType) and not isinstance(t, UnionType):
179+
s, t = t, s
180+
178181
if isinstance(s, AnyType):
179182
return s
180183

181184
if isinstance(s, ErasedType):
182185
return t
183186

184-
if isinstance(s, UnionType) and not isinstance(t, UnionType):
185-
s, t = t, s
186-
187187
if isinstance(s, NoneType) and not isinstance(t, NoneType):
188188
s, t = t, s
189189

test-data/unit/check-enum.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,4 +1881,10 @@ class C(IntEnum):
18811881
def f1(c: C) -> None:
18821882
x = {'x': c.value}
18831883
reveal_type(x) # N: Revealed type is "builtins.dict[builtins.str*, builtins.int]"
1884+
1885+
def f2(c: C, a: Any) -> None:
1886+
x = {'x': c.value, 'y': a}
1887+
reveal_type(x) # N: Revealed type is "builtins.dict[builtins.str*, Any]"
1888+
y = {'y': a, 'x': c.value}
1889+
reveal_type(y) # N: Revealed type is "builtins.dict[builtins.str*, Any]"
18841890
[builtins fixtures/dict.pyi]

0 commit comments

Comments
 (0)