Skip to content

Commit 12e0a9d

Browse files
WIP [mypyc] optimize f-string building
1 parent db67fac commit 12e0a9d

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

mypyc/irbuild/specialize.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,15 @@ def translate_fstring(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Va
710710
format_ops.append(FormatOp.STR)
711711
exprs.append(item.args[0])
712712

713+
for i in range(len(exprs)):
714+
# TODO: instead of checking isinstance StrExpr, check with some new is_literal fn.
715+
# This can include IntExpr, BytesExpr, Final string RefExpr, and more future cases.
716+
while isinstance(exprs[i], StrExpr) and isinstance(exprs[i+1], StrExpr):
717+
first = exprs[i]
718+
concatenated = first.value + exprs[i+1].value
719+
exprs = [exprs[:i] + [StrExpr(concatenated, first.line)] + exprs[i+2:]]
720+
format_ops = format_ops[:i] + [format_ops[i+1]] + [format_ops[i+2:]
721+
713722
substitutions = convert_format_expr_to_str(builder, format_ops, exprs, expr.line)
714723
if substitutions is None:
715724
return None

0 commit comments

Comments
 (0)