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

Second expression in assert statement should be checked as conditional #7318

Merged
merged 5 commits into from Aug 22, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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]