8000 Keep Any when restricting Union in isinstance (#2446) · python/mypy@a488641 · GitHub
[go: up one dir, main page]

Skip to content

Commit a488641

Browse files
ilevkivskyiJukkaL
authored andcommitted
Keep Any when restricting Union in isinstance (#2446)
1 parent c901b71 commit a488641

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

mypy/subtypes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ def restrict_subtype_away(t: Type, s: Type) -> Type:
345345
Currently just remove elements of a union type.
346346
"""
347347
if isinstance(t, UnionType):
348-
new_items = [item for item in t.items if not is_subtype(item, s)]
348+
new_items = [item for item in t.items if (not is_subtype(item, s)
349+
or isinstance(item, AnyType))]
349350
return UnionType.make_union(new_items)
350351
else:
351352
return t

test-data/unit/check-unions.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ def f(x: Union[int, str]) -> None:
3535
[out]
3636
main: note: In function "f":
3737

38+
[case testUnionAnyIsInstance]
39+
from typing import Any, Union
40+
41+
def func(v:Union[int, Any]) -> None:
42+
if isinstance(v, int):
43+
reveal_type(v) # E: Revealed type is 'builtins.int'
44+
else:
45+
reveal_type(v) # E: Revealed type is 'Any'
46+
[builtins fixtures/isinstance.pyi]
47+
[out]
48+
main: note: In function "func":
3849

3950
[case testUnionAttributeAccess]
4051
from typing import Union

0 commit comments

Comments
 (0)
0