Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow isinstance/issubclass with nested tuples #2995

Merged
merged 4 commits into from
Mar 18, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 1 addition & 6 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ 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, Tuple]) -> None:
if isinstance(x, (str, (int, tuple))):
x[1] # E: Value of type "Union[int, str, tuple]" is not indexable
else:
x[1]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add reveal_type(x) here.

if isinstance(x, (str, (list,))):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add reveal_type(x) before the if statement and within the if body.

x[1]
[builtins fixtures/isinstancelist.pyi]

[case testClassAttributeInitialization-skip]
class A:
x = None # type: int
Expand Down
14 changes: 10 additions & 4 deletions test-data/unit/fixtures/isinstancelist.pyi
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand All @@ -27,18 +27,24 @@ 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
def __init__(self, arg: Iterable[Tuple[KT, VT]], **kwargs: VT) -> None: pass
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