Skip to content

Commit

Permalink
Don't simplify away Any when joining unions (python#9301)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JukkaL authored Aug 13, 2020
1 parent bdc5afb commit fbc45aa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-unions.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]'

0 comments on commit fbc45aa

Please sign in to comment.