diff --git a/mypy/checker.py b/mypy/checker.py index a6cc173a91fb..6ff1f3923b0d 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -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 diff --git a/mypy/test/data/check-isinstance.test b/mypy/test/data/check-isinstance.test index 3d6c4b570b40..8ed10673668b 100644 --- a/mypy/test/data/check-isinstance.test +++ b/mypy/test/data/check-isinstance.test @@ -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]