From 06e7484983e3307ef96ee5beae5991be3b46c763 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 13 Aug 2020 17:39:30 +0100 Subject: [PATCH] Don't simplify away Any when joining unions 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]' pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy