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 1 commit 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

@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 97.73371% with 16 lines in your changes missing coverage. Please review.

Project coverage is 98.53%. Comparing base (e57aa7e) to head (50dd4d1).
Report is 25 commits behind head on master.

Files with missing lines Patch % Lines
py/parse.c 95.48% 8 Missing ⚠️
py/modtstring.c 99.10% 3 Missing ⚠️
py/tstring_expr_parser.c 95.31% 3 Missing ⚠️
py/objtype.c 33.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #17557      +/-   ##
==========================================
- Coverage   98.57%   98.53%   -0.04%     
==========================================
  Files         169      172       +3     
  Lines       21968    22637     +669     
==========================================
+ Hits        21654    22306     +652     
- Misses        314      331      +17     

☔ 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: +11712 +1.370% standard[incl +576(data)]
      stm32: +5884 +1.500% PYBV10
     mimxrt: +5856 +1.569% TEENSY40
        rp2: +5944 +0.647% RPI_PICO_W
       samd: +6104 +2.272% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32: +7003 +1.546% VIRT_RV32

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

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

@koxudaxi
Copy link
Author

@ntoll Sounds good! See you in Prague. ☕

@koxudaxi koxudaxi force-pushed the feature/pep750-template-strings branch 9 times, most recently from 330b05f to 7a1dc11 Compare July 2, 2025 15:26
Implements template strings (t-strings) as specified in PEP 750.
Includes Template and Interpolation objects, expression parser,
string.templatelib module, tests and documentation.

Enabled for Unix, Windows, and WebAssembly ports.

Signed-off-by: Koudai Aono <[email protected]>
@koxudaxi koxudaxi force-pushed the feature/pep750-template-strings branch from 7a1dc11 to 50dd4d1 Compare July 2, 2025 15:42
@koxudaxi koxudaxi requested a review from dpgeorge July 2, 2025 15:59
@koxudaxi
Copy link
Author

koxudaxi commented Jul 2, 2025

@dpgeorge

I didn't do a review yet, but there will need to be tests to get full coverage of the new code.

I tried really hard to make 100% coverage but some code is never called and I cannot cover it. Do you know how to fix this?

Comment on lines 153 to +159

A flag for `keys()`, `values()`, `items()` methods to specify that
A flag for :meth:`btree.keys`, :meth:`btree.values`, :meth:`btree.items` methods to specify that
scanning should be inclusive of the end key.

.. data:: DESC

A flag for `keys()`, `values()`, `items()` methods to specify that
A flag for :meth:`btree.keys`, :meth:`btree.values`, :meth:`btree.items` methods to specify that
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I faced an error when building with Sphinx because it conflicted with string.templatelib.Template.values. To solve this problem, I renamed the values attribute in btree to be more explicit.

@koxudaxi
Copy link
Author

koxudaxi commented Jul 2, 2025

@dpgeorge
I'm not very familiar with the micropython codebase, so please let me know if you notice any issues with how I'm handling memory constraints. I think everything looks good, but I'd appreciate your review.

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