MCPcopy Index your code
hub / github.com/python/mypy / convert_format_expr_to_str

Function convert_format_expr_to_str

mypyc/irbuild/format_str_tokenizer.py:135–165  ·  view source on GitHub ↗

Convert expressions into string literal objects with the guidance of FormatOps. Return None when fails.

(
    builder: IRBuilder, format_ops: list[FormatOp], exprs: list[Expression], line: int
)

Source from the content-addressed store, hash-verified

133
134
135def convert_format_expr_to_str(
136 builder: IRBuilder, format_ops: list[FormatOp], exprs: list[Expression], line: int
137) -> list[Value] | None:
138 """Convert expressions into string literal objects with the guidance
139 of FormatOps. Return None when fails."""
140 if len(format_ops) != len(exprs):
141 return None
142
143 converted = []
144 for x, format_op in zip(exprs, format_ops):
145 node_type = builder.node_type(x)
146 if format_op == FormatOp.STR:
147 if isinstance(folded := constant_fold_expr(builder, x), str):
148 var_str = builder.load_literal_value(folded)
149 elif is_str_rprimitive(node_type):
150 var_str = builder.accept(x)
151 elif is_int_rprimitive(node_type) or is_short_int_rprimitive(node_type):
152 var_str = builder.primitive_op(int_to_str_op, [builder.accept(x)], line)
153 else:
154 var_str = builder.primitive_op(str_op, [builder.accept(x)], line)
155 elif format_op == FormatOp.INT:
156 if isinstance(folded := constant_fold_expr(builder, x), int):
157 var_str = builder.load_literal_value(str(folded))
158 elif is_int_rprimitive(node_type) or is_short_int_rprimitive(node_type):
159 var_str = builder.primitive_op(int_to_str_op, [builder.accept(x)], line)
160 else:
161 return None
162 else:
163 return None
164 converted.append(var_str)
165 return converted
166
167
168def join_formatted_strings(

Callers 3

translate_str_formatFunction · 0.90
translate_fstringFunction · 0.90

Calls 13

constant_fold_exprFunction · 0.90
is_str_rprimitiveFunction · 0.90
is_int_rprimitiveFunction · 0.90
is_short_int_rprimitiveFunction · 0.90
lenFunction · 0.85
zipFunction · 0.85
isinstanceFunction · 0.85
strClass · 0.85
node_typeMethod · 0.80
load_literal_valueMethod · 0.80
appendMethod · 0.80
acceptMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…