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 variables inside isinstance #3070

Merged
merged 3 commits into from
Apr 3, 2017
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
6 changes: 2 additions & 4 deletions mypy/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ def _get(self, key: Key, index: int=-1) -> Type:
return None

def put(self, expr: Expression, typ: Type) -> None:
# TODO: replace with isinstance(expr, BindableTypes)
if not isinstance(expr, (IndexExpr, MemberExpr, NameExpr)):
if not isinstance(expr, BindableTypes):
return
if not expr.literal:
return
Expand Down Expand Up @@ -203,8 +202,7 @@ def assign_type(self, expr: Expression,
type: Type,
declared_type: Type,
restrict_any: bool = False) -> None:
# TODO: replace with isinstance(expr, BindableTypes)
if not isinstance(expr, (IndexExpr, MemberExpr, NameExpr)):
if not isinstance(expr, BindableTypes):
return None
if not expr.literal:
return
Expand Down
10 changes: 9 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2768,8 +2768,16 @@ def flatten(t: Expression) -> List[Expression]:
return [t]


def flatten_types(t: Type) -> List[Type]:
"""Flatten a nested sequence of tuples into one list of nodes."""
if isinstance(t, TupleType):
return [b for a in t.items for b in flatten_types(a)]
else:
return [t]


def get_isinstance_type(expr: Expression, type_map: Dict[Expression, Type]) -> List[TypeRange]:
all_types = [type_map[e] for e in flatten(expr)]
all_types = flatten_types(type_map[expr])
types = [] # type: List[TypeRange]
for type in all_types:
if isinstance(type, FunctionLike) and type.is_type_obj():
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -1412,3 +1412,19 @@ def f(x: Union[int, A], a: Type[A]) -> None:

[builtins fixtures/isinstancelist.pyi]


[case testIsinstanceVariableSubstitution]
T = (int, str)
U = (list, T)
x: object = None

if isinstance(x, T):
reveal_type(x) # E: Revealed type is 'Union[builtins.int, builtins.str]'

if isinstance(x, U):
reveal_type(x) # E: Revealed type is 'Union[builtins.list[Any], builtins.int, builtins.str]'

if isinstance(x, (set, (list, T))):
reveal_type(x) # E: Revealed type is 'Union[builtins.set[Any], builtins.list[Any], builtins.int, builtins.str]'

[builtins fixtures/isinstancelist.pyi]