-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
py: Add PEP 750 template strings support #17557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
py: Add PEP 750 template strings support #17557
Conversation
Implements template strings (t-strings) as specified in PEP 750 for Python 3.14. Template strings are a generalization of f-strings that return Template objects instead of strings, enabling safer string processing and custom formatting. Key changes: - Add lexer/parser support for t"..." and rt"..." literals - Implement Template and Interpolation object types - Create string.templatelib module - Add comprehensive test suite and documentation This feature is opt-in via MICROPY_PY_TSTRINGS configuration and has zero impact when disabled. It requires MICROPY_PY_FSTRINGS to be enabled. Signed-off-by: Koudai Aono <[email protected]>
Signed-off-by: Koudai Aono <[email protected]>
Signed-off-by: Koudai Aono <[email protected]>
Signed-off-by: Koudai Aono <[email protected]>
Signed-off-by: Koudai Aono <[email protected]>
Signed-off-by: Koudai Aono <[email protected]>
Signed-off-by: Koudai Aono <[email protected]>
Signed-off-by: Koudai Aono <[email protected]>
Signed-off-by: Koudai Aono <[email protected]>
Signed-off-by: Koudai Aono <[email protected]>
Signed-off-by: Koudai Aono <[email protected]>
Signed-off-by: Koudai Aono <[email protected]>
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #17557 +/- ##
==========================================
- Coverage 98.57% 97.16% -1.41%
==========================================
Files 169 173 +4
Lines 21968 22473 +505
==========================================
+ Hits 21654 21835 +181
- Misses 314 638 +324 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…are disabled Signed-off-by: Koudai Aono <[email protected]>
Signed-off-by: Koudai Aono <[email protected]>
Signed-off-by: Koudai Aono <[email protected]>
Code size report:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution! This will be nice to have for the webassembly port.
I didn't do a review yet, but there will need to be tests to get full coverage of the new code.
docs/library/btree.rst
Outdated
@@ -130,31 +130,50 @@ Methods | |||
to get access to all keys in order. | |||
|
|||
.. method:: btree.keys([start_key, [end_key, [flags]]]) | |||
btree.values([start_key, [end_key, [flags]]]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an unrelated change, so please put it in a separate PR.
(This PR itself will be big enough, so let's aim to make it as easy as possible to review.)
py/modbuiltins.c
Outdated
@@ -525,6 +525,326 @@ static mp_obj_t mp_builtin_sum(size_t n_args, const mp_obj_t *args) { | |||
} | |||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum); | |||
|
|||
#if MICROPY_PY_TSTRINGS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this new code should go in its own, separate file.
@dpgeorge Thank you for the feedback and for taking time to look at this! I'm glad this will be useful for the webassembly port. I'll add more tests to ensure full coverage when I find time. |
Signed-off-by: Koudai Aono <[email protected]>
Signed-off-by: Koudai Aono <[email protected]>
…modtstring.c Signed-off-by: Koudai Aono <[email protected]>
@koxudaxi slightly off topic - but I notice you'll be at EuroPython in Prague, as will I. We should look out for each other and have a coffee or lunch together! 🇪🇺 🐍 |
…s for template strings Signed-off-by: Koudai Aono <[email protected]>
…ion and concatenation support Signed-off-by: Koudai Aono <[email protected]>
…ings Signed-off-by: Koudai Aono <[email protected]>
Summary
Implements PEP 750 template strings for MicroPython.
Started in discussion #17497. Template strings (t-strings) are new in Python 3.14 - they return Template objects instead of strings, so you can access the literal parts and expressions separately.
Changes:
Usage:
Testing
Tested on unix port with both MICROPY_PY_TSTRINGS enabled and disabled.
Test coverage:
All existing tests continue to pass. New tests added in
tests/basics/string_template*.py
.Ports tested: Unix (other ports need testing)
Trade-offs and Alternatives
Adds ~240 bytes when enabled on unix port. When disabled, no impact.
Worth it because:
Config: Enabled by default when MICROPY_CONFIG_ROM_LEVEL >= EXTRA_FEATURES.
Needs MICROPY_PY_FSTRINGS=1.
To disable:
make CFLAGS_EXTRA=-DMICROPY_PY_TSTRINGS=0