summaryrefslogtreecommitdiffstatshomepage
path: root/tests/basics/string_fstring.py
diff options
context:
space:
mode:
authorJared Hancock <jared.hancock@centeredsolutions.com>2022-05-30 10:22:57 -0400
committerDamien George <damien@micropython.org>2023-06-14 19:11:04 +1000
commitb3cd41dd4ba9d44910b7efc6c4a1fa5f531858d4 (patch)
tree1702c82fea36e6eb99fd7e6203c0eadd28495763 /tests/basics/string_fstring.py
parent5ce1a03a78bf87c914d53f63a35cf2b81963e6e4 (diff)
downloadmicropython-b3cd41dd4ba9d44910b7efc6c4a1fa5f531858d4.tar.gz
micropython-b3cd41dd4ba9d44910b7efc6c4a1fa5f531858d4.zip
py/lexer: Allow conversion specifiers in f-strings (e.g. !r).
PEP-498 allows for conversion specifiers like !r and !s to convert the expression declared in braces to be passed through repr() and str() respectively. This updates the logic that detects the end of the expression to also stop when it sees "![rs]" that is either at the end of the f-string or before the ":" indicating the start of the format specifier. The "![rs]" is now retained in the format string, whereas previously it stayed on the end of the expression leading to a syntax error. Previously: `f"{x!y:z}"` --> `"{:z}".format(x!y)` Now: `f"{x!y:z}"` --> `"{!y:z}".format(x)` Note that "!a" is not supported by `str.format` as MicroPython has no `ascii()`, but now this will raise the correct error. Updated cpydiff and added tests. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'tests/basics/string_fstring.py')
-rw-r--r--tests/basics/string_fstring.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/basics/string_fstring.py b/tests/basics/string_fstring.py
index 8907a5c478..1a3960680e 100644
--- a/tests/basics/string_fstring.py
+++ b/tests/basics/string_fstring.py
@@ -61,3 +61,22 @@ except (ValueError, SyntaxError):
print(f"a {1,} b")
print(f"a {x,y,} b")
print(f"a {x,1} b")
+
+# f-strings with conversion specifiers (only support !r and !s).
+a = "123"
+print(f"{a!r}")
+print(f"{a!s}")
+try:
+ eval('print(f"{a!x}")')
+except (ValueError, SyntaxError):
+ # CPython detects this at compile time, MicroPython fails with ValueError
+ # when the str.format is executed.
+ print("ValueError")
+
+# Mixing conversion specifiers with formatting.
+print(f"{a!r:8s}")
+print(f"{a!s:8s}")
+
+# Still allow ! in expressions.
+print(f"{'1' if a != '456' else '0'!r:8s}")
+print(f"{'1' if a != '456' else '0'!s:8s}")