From e7a5c78a88e25ca047b36b2354f912a0271b64ea Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Mon, 24 Jan 2022 10:05:46 +0000 Subject: [PATCH 1/2] Fix join of Any against a union type Make the join of a union type and Any commutative. Previously the result dependend on the order of the operands, which was clearly incorrect. Fix #12051, but other use cases are affected as well. --- mypy/join.py | 6 +++--- test-data/unit/check-enum.test | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/mypy/join.py b/mypy/join.py index d298b495fdc5..161c65ea800c 100644 --- a/mypy/join.py +++ b/mypy/join.py @@ -175,15 +175,15 @@ def join_types(s: Type, t: Type, instance_joiner: Optional[InstanceJoiner] = Non s = mypy.typeops.true_or_false(s) t = mypy.typeops.true_or_false(t) + if isinstance(s, UnionType) and not isinstance(t, UnionType): + s, t = t, s + if isinstance(s, AnyType): return s if isinstance(s, ErasedType): return t - if isinstance(s, UnionType) and not isinstance(t, UnionType): - s, t = t, s - if isinstance(s, NoneType) and not isinstance(t, NoneType): s, t = t, s diff --git a/test-data/unit/check-enum.test b/test-data/unit/check-enum.test index 92982ef30b4e..a7c8ef15e474 100644 --- a/test-data/unit/check-enum.test +++ b/test-data/unit/check-enum.test @@ -1881,4 +1881,8 @@ class C(IntEnum): def f1(c: C) -> None: x = {'x': c.value} reveal_type(x) # N: Revealed type is "builtins.dict[builtins.str*, builtins.int]" + +def f2(c: C, a: Any) -> None: + x = {'x': c.value, 'y': a} + reveal_type(x) # N: Revealed type is "builtins.dict[builtins.str*, Any]" [builtins fixtures/dict.pyi] From a4b03d4544fb426bfdb48eb6e2957f8b51cd0339 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Tue, 25 Jan 2022 15:52:40 +0000 Subject: [PATCH 2/2] Update test case --- test-data/unit/check-enum.test | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test-data/unit/check-enum.test b/test-data/unit/check-enum.test index a7c8ef15e474..3d7c3fc3a392 100644 --- a/test-data/unit/check-enum.test +++ b/test-data/unit/check-enum.test @@ -1885,4 +1885,6 @@ def f1(c: C) -> None: def f2(c: C, a: Any) -> None: x = {'x': c.value, 'y': a} reveal_type(x) # N: Revealed type is "builtins.dict[builtins.str*, Any]" + y = {'y': a, 'x': c.value} + reveal_type(y) # N: Revealed type is "builtins.dict[builtins.str*, Any]" [builtins fixtures/dict.pyi]