Skip to content

Commit 15c0ea0

Browse files
committed
[mypyc]: support :s and :d in generate_format_ops
1 parent 4826244 commit 15c0ea0

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

mypyc/irbuild/format_str_tokenizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def generate_format_ops(specifiers: list[ConversionSpecifier]) -> list[FormatOp]
5353
format_ops = []
5454
for spec in specifiers:
5555
# TODO: Match specifiers instead of using whole_seq
56-
if spec.whole_seq == "%s" or spec.whole_seq == "{:{}}":
56+
if spec.whole_seq == "%s" or spec.whole_seq in ("{:{}}", ":s"):
5757
format_op = FormatOp.STR
58-
elif spec.whole_seq == "%d":
58+
elif spec.whole_seq in ("%d", ":d"):
5959
format_op = FormatOp.INT
6060
elif spec.whole_seq == "%b":
6161
format_op = FormatOp.BYTES

mypyc/test-data/irbuild-constant-fold.test

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ L0:
192192
from typing import Any, Final
193193

194194
FMT: Final = "{} {}"
195+
FMT_D: Final = "{:d} {:s}"
196+
FMT_S: Final = "{:s} {:s}"
195197

196198
def f() -> str:
197199
return FMT.format(400 + 20, "roll" + "up")
@@ -205,6 +207,30 @@ L0:
205207
r3 = CPyStr_Build(3, r0, r1, r2)
206208
return r3
207209

210+
def g() -> str:
211+
return FMT_D.format(400 + 20, "roll" + "up")
212+
[out]
213+
def g():
214+
r0, r1, r2, r3 :: str
215+
L0:
216+
r0 = '420'
217+
r1 = ' '
218+
r2 = 'rollup'
219+
r3 = CPyStr_Build(3, r0, r1, r2)
220+
return r3
221+
222+
def h() -> str:
223+
return FMT_S.format(400 + 20, "roll" + "up")
224+
[out]
225+
def h():
226+
r0, r1, r2, r3 :: str
227+
L0:
228+
r0 = '420'
229+
r1 = ' '
230+
r2 = 'rollup'
231+
r3 = CPyStr_Build(3, r0, r1, r2)
232+
return r3
233+
208234
[case testIntConstantFoldingFinal]
209235
from typing import Final
210236
X: Final = 5

0 commit comments

Comments
 (0)