diff --git a/mypy/subtypes.py b/mypy/subtypes.py index a046cef23d68..bfb3c4b58e87 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -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 diff --git a/test-data/unit/check-unions.test b/test-data/unit/check-unions.test index 121413836014..3b2ea8f19f83 100644 --- a/test-data/unit/check-unions.test +++ b/test-data/unit/check-unions.test @@ -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