Skip to main content

Backport of t-strings (PEP 750)

Project description

tstrings: PEP 750-style Template Strings for Python < 3.14

This package provides a backport of the new PEP 750 "t-string" template string syntax, for use with Python versions before 3.14.

Usage

Install from PyPI with uv add tstrings-backport or python -m pip install tstrings-backport.

Instead of the new syntax t"..." (which is only available in Python 3.14+), use the function call form:

from tstrings import t

name = "World"
tpl = t("Hello, {name}!")
print(tpl.strings)         # ("Hello, ", "!")
print(tpl.interpolations)  # (Interpolation(value="World", expression="name", ...),)

The returned object is a Template with .strings and .interpolations attributes, which should be a drop-in replacement for the built-in t-strings.

Features

  • String interpolation: Supports {expr} expressions, including complex expressions.
  • Debug specifier: {var=} and {var=:.2f} forms, as in f-strings.
  • Conversion specifiers: {val!r}, {val!s}, {val!a}.
  • Format specifiers: {num:.2f}.
  • Multiline expressions: Supported.
  • Error handling: Raises NameError or SyntaxError for invalid expressions, as in f-strings (but at runtime, not at compile time).
  • PEP 750 API: Returns Template and Interpolation dataclasses matching the PEP.

Limitations

  • No t-string literal syntax: You must use t("..."), not t"...".
  • No support for all edge cases: Some advanced t-string edge cases may not be fully supported.

Examples

from tstrings import t

name = ""
value = 1
num = 3.1415
obj = {}
data = [1, 2, 3]

# Simple
tpl = t("Hello, {name}!")
# Debug
tpl = t("{value=}")
# Format
tpl = t("{num:.2f}")
# Conversion
tpl = t("{obj!r}")
# Multiline
tpl = t("""
    {sum(data)}
""")

Development & Testing

Install with uv sync and activate the virtualenv (. .venv/bin/activate or similar).

Run the test suite with:

make test

or just:

uv run --python 3.13 pytest

See tests/test_all.py for coverage of all supported features.

Tests also include the tdom source code (modified for pre-3.14 syntax) and test suite.

How to help

This was (initially) hacked together in less than 2 hours. If you find it useful, please consider contributing fixes, improvements, or documentation!

Pull requests and issues are welcome.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tstrings_backport-0.1.5.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tstrings_backport-0.1.5-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file tstrings_backport-0.1.5.tar.gz.

File metadata

  • Download URL: tstrings_backport-0.1.5.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.4

File hashes

Hashes for tstrings_backport-0.1.5.tar.gz
Algorithm Hash digest
SHA256 9b39455fee76a9b986f517489253d8408a1cbaf5259f81665a51b8f464b1455c
MD5 63bedf9ae63ff6808406c5755414c473
BLAKE2b-256 bbfa7ec2ee014a55b9a98c763e472993b2563b065caa8777bcdde8a784effe5b

See more details on using hashes here.

File details

Details for the file tstrings_backport-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for tstrings_backport-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 835e1e8ec6f87342605f5a71d16cb3a303f1b1840c4935d8107f7038f0065127
MD5 dfa0cbb18c7f4255b71074a2d66cc2f6
BLAKE2b-256 5bb8dea0b1f3b3d78915983d03a2bcce2fbfb422c95cc173bb644f6b08309b81

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page