Skip to content

Conversation

xmo-odoo
Copy link

@xmo-odoo xmo-odoo commented Oct 8, 2025

Python 3.14 introduces t-strings, a generalisation of f-strings which yields a processable Template object instead of reifying a string. The safety assumption around t-strings is that the literal parts of the template are considered "safe" and the interpolations are considered unsafe. So for markupsafe the literal parts are Markup-ed while the interpolations are escape-ed.

This commit adds support for t-strings to both Markup and escape, essentially matching the handling of __html__.

It does not add support for t-strings to:

  • Markup.__add__ / Markup.__radd__, the semantics seem a bit dubious and it should be easy enough to Markup or escape the t-string / template object in that case.
  • EscapeFormatter as I'm not entirely sure whether / how a template object should interact with format specs.

Fixes #511

Python 3.14 introduces [t-strings][PEP750], a generalisation of f-strings which yields a
processable `Template` object instead of reifying a string. The safety assumption around
t-strings is that the literal parts of the template are considered "safe" and the
interpolations are considered unsafe. So for markupsafe the literal parts are
`Markup`-ed while the interpolations are `escape`-ed.

This commit adds support for t-strings to both `Markup` and `escape`, essentially
matching the handling of `__html__`.

It does *not* add support for t-strings to:

- `Markup.__add__` / `Markup.__radd__`, the semantics seem a bit dubious and it should
  be easy enough to `Markup` or `escape` the t-string / template object in that case.
- `EscapeFormatter` as I'm not entirely sure whether / how a template object should
  interact with format specs.

Fixes pallets#511

[PEP750]: https://peps.python.org/pep-0750/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

t-strings support

1 participant