Skip to content

Commit fbc45aa

Browse files
authored
Don't simplify away Any when joining unions (python#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.
1 parent bdc5afb commit fbc45aa

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

mypy/join.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def visit_unbound_type(self, t: UnboundType) -> ProperType:
119119
return AnyType(TypeOfAny.special_form)
120120

121121
def visit_union_type(self, t: UnionType) -> ProperType:
122-
if is_subtype(self.s, t):
122+
if is_proper_subtype(self.s, t):
123123
return t
124124
else:
125125
return mypy.typeops.make_simplified_union([self.s, t])

test-data/unit/check-unions.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,3 +1048,14 @@ def foo(a: T2, b: T2) -> T2:
10481048
def bar(a: T4, b: T4) -> T4: # test multi-level alias
10491049
return a + b
10501050
[builtins fixtures/ops.pyi]
1051+
1052+
[case testJoinUnionWithUnionAndAny]
1053+
# flags: --strict-optional
1054+
from typing import TypeVar, Union, Any
1055+
T = TypeVar("T")
1056+
def f(x: T, y: T) -> T:
1057+
return x
1058+
x: Union[None, Any]
1059+
y: Union[int, None]
1060+
reveal_type(f(x, y)) # N: Revealed type is 'Union[None, Any, builtins.int]'
1061+
reveal_type(f(y, x)) # N: Revealed type is 'Union[builtins.int, None, Any]'

0 commit comments

Comments
 (0)
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