Skip to content
Merged
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
10 changes: 4 additions & 6 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3468,7 +3468,7 @@ def visit_op_expr(self, e: OpExpr) -> Type:
# It's actually a type expression X | Y.
return self.accept(e.analyzed)
if e.op == "and" or e.op == "or":
return self.check_boolean_op(e, e)
return self.check_boolean_op(e)
if e.op == "*" and isinstance(e.left, ListExpr):
# Expressions of form [...] * e get special type inference.
return self.check_list_multiply(e)
Expand Down Expand Up @@ -4255,20 +4255,18 @@ def check_op(
context=context,
)

def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
def check_boolean_op(self, e: OpExpr) -> Type:
"""Type check a boolean operation ('and' or 'or')."""

# A boolean operation can evaluate to either of the operands.

# We use the current type context to guide the type inference of of
# We use the current type context to guide the type inference of
# the left operand. We also use the left operand type to guide the type
# inference of the right operand so that expressions such as
# '[1] or []' are inferred correctly.
ctx = self.type_context[-1]
left_type = self.accept(e.left, ctx)
expanded_left_type = try_expanding_sum_type_to_union(
self.accept(e.left, ctx), "builtins.bool"
)
expanded_left_type = try_expanding_sum_type_to_union(left_type, "builtins.bool")

assert e.op in ("and", "or") # Checked by visit_op_expr

Expand Down