Skip to content

[mypyc] feat: try constant fold in convert_format_expr_to_str#20092

Open
BobTheBuidler wants to merge 9 commits intopython:masterfrom
BobTheBuidler:patch-22
Open

[mypyc] feat: try constant fold in convert_format_expr_to_str#20092
BobTheBuidler wants to merge 9 commits intopython:masterfrom
BobTheBuidler:patch-22

Conversation

@BobTheBuidler
Copy link
Copy Markdown
Contributor

No description provided.

Comment thread mypyc/irbuild/format_str_tokenizer.py Outdated
if is_str_rprimitive(node_type) or isinstance(x, StrExpr):
# NOTE: why does mypyc think our fake StrExprs are not str rprimitives?
var_str = builder.accept(x)
elif (folded := constant_fold_expr(builder, x)) is not None:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foldable non-string values can be easily converted to str

var_str = builder.primitive_op(str_op, [builder.accept(x)], line)
elif format_op == FormatOp.INT:
if is_int_rprimitive(node_type) or is_short_int_rprimitive(node_type):
if isinstance(folded := constant_fold_expr(builder, x), int):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the expr can fold as an int we can convert it to str at build time

@BobTheBuidler
Copy link
Copy Markdown
Contributor Author

These tests should start working once #19982 is merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants