Skip to content

Commit b8aa2e5

Browse files
Update constant_fold.py
1 parent 28ca76b commit b8aa2e5

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

mypyc/irbuild/constant_fold.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,15 @@ def constant_fold_expr(builder: IRBuilder, expr: Expression) -> ConstantValue |
7373
value = constant_fold_expr(builder, expr.expr)
7474
if value is not None and not isinstance(value, bytes):
7575
return constant_fold_unary_op(expr.op, value)
76-
elif isinstance(expr, CallExpr) and isinstance(callee := expr.callee, MemberExpr):
77-
# --- str.format constant folding
78-
if callee.name == "format":
79-
folded_args: list[ConstantValue] = []
80-
for arg in expr.args:
81-
arg_val = constant_fold_expr(arg, cur_mod_id)
82-
if arg_val is None:
83-
return None
84-
folded_args.append(arg_val)
85-
return folded_callee.format(*folded_args)
76+
# --- str.format constant folding
77+
elif isinstance(expr, CallExpr) and isinstance(callee := expr.callee, MemberExpr) and callee.name == "format" and (folded_callee := constant_fold_expr(callee)) is not None:
78+
folded_args: list[ConstantValue] = []
79+
for arg in expr.args:
80+
arg_val = constant_fold_expr(builder, arg)
81+
if arg_val is None:
82+
return None
83+
folded_args.append(arg_val)
84+
return folded_callee.format(*folded_args)
8685
return None
8786

8887

0 commit comments

Comments
 (0)