Skip to content

Commit 0dbfd75

Browse files
author
Roy Williams
committed
Suppress any errors found in a boolean expression after filtering out all possible types.
This fixes #3643
1 parent 0d045cd commit 0dbfd75

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

mypy/checkexpr.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,16 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
15191519
elif e.right_always:
15201520
left_map = None
15211521

1522-
right_type = self.analyze_cond_branch(right_map, e.right, left_type)
1522+
# If right_map is None then we know mypy considers the right branch
1523+
# to be unreachable and therefore any errors found in the right branch
1524+
# should be suppressed.
1525+
if right_map is None:
1526+
self.msg.disable_errors()
1527+
try:
1528+
right_type = self.analyze_cond_branch(right_map, e.right, left_type)
1529+
finally:
1530+
if right_map is None:
1531+
self.msg.enable_errors()
15231532

15241533
if right_map is None:
15251534
# The boolean expression is statically known to be the left value

test-data/unit/check-isinstance.test

+8
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,8 @@ if isinstance(x, B) and isinstance(y, int):
11871187
1() # type checking skipped
11881188
if isinstance(y, int) and isinstance(x, B):
11891189
1() # type checking skipped
1190+
if isinstance(y, int) and y > 42:
1191+
1() # type checking skipped
11901192
[builtins fixtures/isinstancelist.pyi]
11911193

11921194
[case testReturnWithCallExprAndIsinstance]
@@ -1754,3 +1756,9 @@ if isinstance(x, str, 1): # E: Too many arguments for "isinstance"
17541756
x = 1
17551757
reveal_type(x) # E: Revealed type is 'builtins.int'
17561758
[builtins fixtures/isinstancelist.pyi]
1759+
1760+
[case testTypesAfterNoneFilteredOutIgnored]
1761+
x:int = 32
1762+
not isinstance(x, int) and x.foobar()
1763+
1764+
[builtins fixtures/isinstancelist.pyi]

0 commit comments

Comments
 (0)