Skip to content

Commit 31adfb4

Browse files
authored
Avoid duplicate visit in check_boolean_op() (#19515)
Surprisingly, this simple change gives 0.5-1% perf boost on self-check (for some reasons this is quite hot function).
1 parent afcf6b2 commit 31adfb4

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

mypy/checkexpr.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3468,7 +3468,7 @@ def visit_op_expr(self, e: OpExpr) -> Type:
34683468
# It's actually a type expression X | Y.
34693469
return self.accept(e.analyzed)
34703470
if e.op == "and" or e.op == "or":
3471-
return self.check_boolean_op(e, e)
3471+
return self.check_boolean_op(e)
34723472
if e.op == "*" and isinstance(e.left, ListExpr):
34733473
# Expressions of form [...] * e get special type inference.
34743474
return self.check_list_multiply(e)
@@ -4255,20 +4255,18 @@ def check_op(
42554255
context=context,
42564256
)
42574257

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

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

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

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

0 commit comments

Comments
 (0)