Skip to content

Commit e53c355

Browse files
committed
Apply improvements suggested by @ddfisher in his review
1 parent 9aa4799 commit e53c355

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

mypy/checkexpr.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,18 +1093,20 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
10931093
ctx = self.chk.type_context[-1]
10941094
left_type = self.accept(e.left, ctx)
10951095

1096+
assert e.op in ('and', 'or') # Checked by visit_op_expr
1097+
10961098
if e.op == 'and':
10971099
right_map, left_map = \
10981100
mypy.checker.find_isinstance_check(e.left, self.chk.type_map,
10991101
self.chk.typing_mode_weak())
1100-
truthiness_restriction = false_only
1102+
restricted_left_type = false_only(left_type)
1103+
result_is_left = not left_type.can_be_true
11011104
elif e.op == 'or':
11021105
left_map, right_map = \
11031106
mypy.checker.find_isinstance_check(e.left, self.chk.type_map,
11041107
self.chk.typing_mode_weak())
1105-
truthiness_restriction = true_only
1106-
# type_restriction tells about the possible truth values of the left operand
1107-
# if that's the result of the boolean operation
1108+
restricted_left_type = true_only(left_type)
1109+
result_is_left = not left_type.can_be_false
11081110

11091111
with self.chk.binder.frame_context():
11101112
if right_map:
@@ -1125,12 +1127,10 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
11251127
assert right_map is not None # find_isinstance_check guarantees this
11261128
return right_type
11271129

1128-
restricted_left_type = truthiness_restriction(left_type)
1129-
11301130
if isinstance(restricted_left_type, UninhabitedType):
11311131
# The left operand can never be the result
11321132
return right_type
1133-
elif left_type is restricted_left_type:
1133+
elif result_is_left:
11341134
# The left operand is always the result
11351135
return left_type
11361136
else:

test-data/unit/check-expressions.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ b = None # type: bool
309309
i = None # type: str
310310
j = not b and i
311311
if j:
312-
s = j # type: str
312+
reveal_type(j) # E: Revealed type is 'builtins.str'
313313

314314

315315
[builtins fixtures/bool.py]
@@ -320,7 +320,7 @@ b = None # type: bool
320320
i = None # type: str
321321
j = b or i
322322
if not j:
323-
s = j # type: str
323+
reveal_type(j) # E: Revealed type is 'builtins.str'
324324

325325
[builtins fixtures/bool.py]
326326

0 commit comments

Comments
 (0)