From fbc45aabdf46c820e81e3fc85e3d50b3ebf28ad0 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 13 Aug 2020 18:48:54 +0100 Subject: [PATCH] Don't simplify away Any when joining unions (#9301) Previously join of `Union[int, Any]` and `Union[int, None]` could be `Union[int, None]` (we lost the `Any` type). Fix this by replacing a subtype check with a proper subtype check when joining unions. --- mypy/join.py | 2 +- test-data/unit/check-unions.test | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/mypy/join.py b/mypy/join.py index 736e10fd20f2..4cd0da163e13 100644 --- a/mypy/join.py +++ b/mypy/join.py @@ -119,7 +119,7 @@ def visit_unbound_type(self, t: UnboundType) -> ProperType: return AnyType(TypeOfAny.special_form) def visit_union_type(self, t: UnionType) -> ProperType: - if is_subtype(self.s, t): + if is_proper_subtype(self.s, t): return t else: return mypy.typeops.make_simplified_union([self.s, t]) diff --git a/test-data/unit/check-unions.test b/test-data/unit/check-unions.test index 4fcc1007ae48..4a163136d553 100644 --- a/test-data/unit/check-unions.test +++ b/test-data/unit/check-unions.test @@ -1048,3 +1048,14 @@ def foo(a: T2, b: T2) -> T2: def bar(a: T4, b: T4) -> T4: # test multi-level alias return a + b [builtins fixtures/ops.pyi] + +[case testJoinUnionWithUnionAndAny] +# flags: --strict-optional +from typing import TypeVar, Union, Any +T = TypeVar("T") +def f(x: T, y: T) -> T: + return x +x: Union[None, Any] +y: Union[int, None] +reveal_type(f(x, y)) # N: Revealed type is 'Union[None, Any, builtins.int]' +reveal_type(f(y, x)) # N: Revealed type is 'Union[builtins.int, None, Any]'