Skip to content

Suppress any errors found in a boolean expression after filtering out all possible types. #3666

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 1 commit into from
Jul 12, 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
11 changes: 10 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,16 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
elif e.right_always:
left_map = None

right_type = self.analyze_cond_branch(right_map, e.right, left_type)
# If right_map is None then we know mypy considers the right branch
# to be unreachable and therefore any errors found in the right branch
# should be suppressed.
if right_map is None:
self.msg.disable_errors()
try:
right_type = self.analyze_cond_branch(right_map, e.right, left_type)
finally:
if right_map is None:
self.msg.enable_errors()

if right_map is None:
# The boolean expression is statically known to be the left value
Expand Down
2 changes: 2 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,8 @@ if isinstance(x, B) and isinstance(y, int):
1() # type checking skipped
if isinstance(y, int) and isinstance(x, B):
1() # type checking skipped
if isinstance(y, int) and y > 42:
1() # type checking skipped
[builtins fixtures/isinstancelist.pyi]

[case testReturnWithCallExprAndIsinstance]
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,21 @@ if x is not None:

[builtins fixtures/ops.pyi]

[case testOptionalTypeNarrowedInBooleanStatement]
from typing import Optional

x: Optional[int] = None
x is not None and x + 42
x is not None and x + '42' # E: Unsupported operand types for + ("int" and "str")
[builtins fixtures/isinstance.pyi]

[case testInvalidBooleanBranchIgnored]
from typing import Optional

x = None
x is not None and x + 42
[builtins fixtures/isinstance.pyi]

[case testOptionalLambdaInference]
from typing import Optional, Callable
f = None # type: Optional[Callable[[int], None]]
Expand Down