Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ def restrict_subtype_away(t: Type, s: Type) -> Type:
Currently just remove elements of a union type.
"""
if isinstance(t, UnionType):
new_items = [item for item in t.items if not is_subtype(item, s)]
new_items = [item for item in t.items if (not is_subtype(item, s)
or isinstance(item, AnyType))]
return UnionType.make_union(new_items)
else:
return 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 @@ -35,6 +35,17 @@ def f(x: Union[int, str]) -> None:
[out]
main: note: In function "f":

[case testUnionAnyIsInstance]
from typing import Any, Union

def func(v:Union[int, Any]) -> None:
if isinstance(v, int):
reveal_type(v) # E: Revealed type is 'builtins.int'
else:
reveal_type(v) # E: Revealed type is 'Any'
[builtins fixtures/isinstance.pyi]
[out]
main: note: In function "func":

[case testUnionAttributeAccess]
from typing import Union
Expand Down