Skip to content

Commit

Permalink
Second expression in assert statement should be checked as conditional (
Browse files Browse the repository at this point in the history
#7318)

Fixes #7284
  • Loading branch information
gantsevdenis authored and ilevkivskyi committed Aug 22, 2019
1 parent 8194d79 commit 95e8c7a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 3 additions & 4 deletions mypy/checker.py
Expand Up @@ -2979,14 +2979,13 @@ def visit_operator_assignment_stmt(self,
def visit_assert_stmt(self, s: AssertStmt) -> None:
self.expr_checker.accept(s.expr)

if s.msg is not None:
self.expr_checker.accept(s.msg)

if isinstance(s.expr, TupleExpr) and len(s.expr.items) > 0:
self.fail(message_registry.MALFORMED_ASSERT, s)

# If this is asserting some isinstance check, bind that type in the following code
true_map, _ = self.find_isinstance_check(s.expr)
true_map, else_map = self.find_isinstance_check(s.expr)
if s.msg is not None:
self.expr_checker.analyze_cond_branch(else_map, s.msg, None)
self.push_type_map(true_map)

def visit_raise_stmt(self, s: RaiseStmt) -> None:
Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/check-expressions.test
Expand Up @@ -2331,3 +2331,22 @@ def f(x: any) -> None: # E: Name 'any' is not defined \
def f(x: Optional[str]) -> None: # E: Name 'Optional' is not defined \
# N: Did you forget to import it from "typing"? (Suggestion: "from typing import Optional")
pass

[case testAssertionLazilyWithIsNone]
from typing import Optional, List
li: Optional[List] = []
assert li is None, li[0]
[builtins fixtures/list.pyi]


[case testAssertionLazilyWithIsInstance]
from typing import Optional, List
li: Optional[List] = []
assert not isinstance(li,list), li[0]
[builtins fixtures/isinstancelist.pyi]

[case testAssertCurrentFrameIsNotUnreachable]
def f() -> int: # E: Missing return statement
x: int
assert isinstance(x, int), '...'
[builtins fixtures/isinstance.pyi]

0 comments on commit 95e8c7a

Please sign in to comment.