diff --git a/mypy/checker.py b/mypy/checker.py index 307afe8568d5..f31d4c300e19 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -2693,12 +2693,7 @@ def flatten(t: Expression) -> List[Expression]: def get_isinstance_type(expr: Expression, type_map: Dict[Expression, Type]) -> Type: - type = type_map[expr] - - if isinstance(type, TupleType): - all_types = type.items - else: - all_types = [type] + all_types = [type_map[e] for e in flatten(expr)] types = [] # type: List[Type] diff --git a/test-data/unit/check-isinstance.test b/test-data/unit/check-isinstance.test index 9a1b787391a9..43dbfd8321d1 100644 --- a/test-data/unit/check-isinstance.test +++ b/test-data/unit/check-isinstance.test @@ -31,6 +31,22 @@ z = [a.y for b in y for a in b] [builtins fixtures/list.pyi] +[case testIsinstanceNestedTuple] +from typing import Union, List, Tuple, Dict +def f(x: Union[int, str, List]) -> None: + if isinstance(x, (str, (int,))): + reveal_type(x) # E: Revealed type is 'Union[builtins.int, builtins.str]' + x[1] # E: Value of type "Union[int, str]" is not indexable + else: + reveal_type(x) # E: Revealed type is 'builtins.list[Any]' + x[1] + reveal_type(x) # E: Revealed type is 'Union[builtins.int, builtins.str, builtins.list[Any]]' + if isinstance(x, (str, (list,))): + reveal_type(x) # E: Revealed type is 'Union[builtins.str, builtins.list[Any]]' + x[1] + reveal_type(x) # E: Revealed type is 'Union[builtins.int, builtins.str, builtins.list[Any]]' +[builtins fixtures/isinstancelist.pyi] + [case testClassAttributeInitialization-skip] class A: x = None # type: int diff --git a/test-data/unit/fixtures/isinstancelist.pyi b/test-data/unit/fixtures/isinstancelist.pyi index 4b3569875ab9..9096961b06ab 100644 --- a/test-data/unit/fixtures/isinstancelist.pyi +++ b/test-data/unit/fixtures/isinstancelist.pyi @@ -1,4 +1,4 @@ -from typing import builtinclass, Iterable, Iterator, Generic, TypeVar, List, Mapping, overload, Tuple +from typing import builtinclass, Iterable, Iterator, TypeVar, List, Mapping, overload, Tuple, Set, Union @builtinclass class object: @@ -11,7 +11,7 @@ class type: class tuple: pass class function: pass -def isinstance(x: object, t: type) -> bool: pass +def isinstance(x: object, t: Union[type, Tuple]) -> bool: pass @builtinclass class int: @@ -27,14 +27,14 @@ T = TypeVar('T') KT = TypeVar('KT') VT = TypeVar('VT') -class list(Iterable[T], Generic[T]): +class list(Iterable[T]): def __iter__(self) -> Iterator[T]: pass def __mul__(self, x: int) -> list[T]: pass def __setitem__(self, x: int, v: T) -> None: pass def __getitem__(self, x: int) -> T: pass def __add__(self, x: List[T]) -> T: pass -class dict(Iterable[KT], Mapping[KT, VT], Generic[KT, VT]): +class dict(Iterable[KT], Mapping[KT, VT]): @overload def __init__(self, **kwargs: VT) -> None: pass @overload @@ -42,3 +42,9 @@ class dict(Iterable[KT], Mapping[KT, VT], Generic[KT, VT]): def __setitem__(self, k: KT, v: VT) -> None: pass def __iter__(self) -> Iterator[KT]: pass def update(self, a: Mapping[KT, VT]) -> None: pass + +class set(Iterable[T]): + def __iter__(self) -> Iterator[T]: pass + def add(self, x: T) -> None: pass + def discard(self, x: T) -> None: pass + def update(self, x: Set[T]) -> None: pass