Skip to content

Get type information from isinstance assertions #1346

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

Merged
merged 2 commits into from
Apr 8, 2016
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
10 changes: 10 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,16 @@ def visit_operator_assignment_stmt(self,
def visit_assert_stmt(self, s: AssertStmt) -> Type:
self.accept(s.expr)

# If this is asserting some isinstance check, bind that type in the following code
true_map, _ = find_isinstance_check(
s.expr, self.type_map,
self.typing_mode_weak()
)

if true_map:
for var, type in true_map.items():
self.binder.push(var, type)

def visit_raise_stmt(self, s: RaiseStmt) -> Type:
"""Type check a raise statement."""
self.breaking_out = True
Expand Down
7 changes: 7 additions & 0 deletions mypy/test/data/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -817,3 +817,10 @@ def f(x: Union[A, B]) -> None:
if not isinstance(x, B):
f(x)
[builtins fixtures/isinstance.py]

[case testAssertIsinstance]
def f(x: object):
assert isinstance(x, int)
y = 0 # type: int
y = x
[builtins fixtures/isinstance.py]