Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

koxudaxi
Copy link

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:

  • t-string parsing in lexer/parser
  • Template and Interpolation objects
  • string.templatelib module
  • Conditional build with MICROPY_PY_TSTRINGS

Usage:

t = t"Hello {name}!"
str(t)  # "Hello World!"
t.args[1].expression  # "name" 
t.args[1].value  # "World"

Testing

Tested on unix port with both MICROPY_PY_TSTRINGS enabled and disabled.

Test coverage:

  • Basic template string functionality
  • Error handling and edge cases
  • Import system integration
  • Nested expression parsing

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:

  • Part of Python 3.14 standard
  • Optional feature (MICROPY_PY_TSTRINGS)
  • Useful for template processing

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

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]>
@WebReflection
Copy link

for what is worth it, we'd love to have this available at least for the PyScript WASM variant as this unlocks tons of UI related use cases we'd like to deliver to our users.

/cc @dpgeorge @ntoll

@koxudaxi koxudaxi changed the title py: Add PEP 750 template strings support. py: Add PEP 750 template strings support Jun 24, 2025
Copy link

codecov bot commented Jun 25, 2025

Codecov Report

Attention: Patch coverage is 37.57225% with 324 lines in your changes missing coverage. Please review.

Project coverage is 97.16%. Comparing base (e57aa7e) to head (16a0f73).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
py/modtstring.c 12.29% 214 Missing ⚠️
py/parse.c 70.89% 39 Missing ⚠️
py/objinterpolation.c 28.20% 28 Missing ⚠️
py/tstring_expr_parser.c 55.00% 18 Missing ⚠️
py/modstring.c 0.00% 13 Missing ⚠️
py/lexer.c 75.51% 12 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

github-actions bot commented Jun 25, 2025

Code size report:

   bare-arm:   +28 +0.049% 
minimal x86:   +58 +0.031% [incl +32(data)]
   unix x64: +10056 +1.176% standard[incl +608(data) +32(bss)]
      stm32: +4884 +1.245% PYBV10[incl +4(bss)]
     mimxrt: +4872 +1.306% TEENSY40
        rp2: +4600 +0.501% RPI_PICO_W
       samd: +5060 +1.883% ADAFRUIT_ITSYBITSY_M4_EXPRESS[incl +8(bss)]
  qemu rv32: +5711 +1.260% VIRT_RV32[incl +1(bss)]

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jun 25, 2025
Copy link
Member

@dpgeorge dpgeorge left a 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.

@@ -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]]])
Copy link
Member

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
Copy link
Member

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.

@koxudaxi
Copy link
Author

@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.

@ntoll
Copy link
Contributor

ntoll commented Jun 25, 2025

@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! 🇪🇺 🐍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants