Skip to content

Commit 3d62151

Browse files
Update constant_fold.py
1 parent 0018c4b commit 3d62151

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

mypyc/irbuild/constant_fold.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from mypy.constant_fold import constant_fold_binary_op, constant_fold_unary_op
1616
from mypy.nodes import (
1717
BytesExpr,
18+
CallExpr,
1819
ComplexExpr,
1920
Expression,
2021
FloatExpr,
@@ -72,7 +73,18 @@ def constant_fold_expr(builder: IRBuilder, expr: Expression) -> ConstantValue |
7273
value = constant_fold_expr(builder, expr.expr)
7374
if value is not None and not isinstance(value, bytes):
7475
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)
7586
return None
87+
7688

7789

7890
def constant_fold_binary_op_extended(

0 commit comments

Comments
 (0)