Skip to main content

Backport of tstrings to python <3.14, and full-syntax fstrings to python <3.12

Project description

future-tstrings

A backport of tstrings to python<3.14

Also serves as a backport of full-syntax fstrings (PEP701-style) to python <3.12.

Does nothing on 3.14 or higher.

Requires python >= 3.8

Installation

pip install future-tstrings

Usage

Include the following magic line at the top of the file (before regular imports)

# future-tstrings

And then write python 3.14 tstring and fstring code as usual!

- example.py

from string.templatelib import Template  # or, future_tstrings.templatelib

thing = "world"
template: Template = t"hello {thing}"

print(repr(template))
# t"hello {'world'}"

assert template.strings[0] == "hello "
assert template.interpolations[0].value == "world"
$ python -m example
t"hello {'world'}"

Showing compiled source

future-tstrings also includes a cli to show transformed source.

$ future-tstrings example.py

Integrating with template processing tools

Libraries that consume template strings (html parsers, etc) do not need to do anything extra to support future-tstrings, except:

They should NOT disable this behavior on python<3.14. To implicitly support future-tstrings without listing it as a dependency, use the following:

try:
    from string.templatelib import Template
except ImportError:
    class Template:
        pass

How does this work?

future-tstrings has two parts:

  1. An importer which transpiles t-strings and f-strings to code understood by older versions of python

  2. A .pth file which registers the importer on interpreter startup.

Alternative python environments

In environments (such as aws lambda) where packages are not installed via pip, the .pth magic will not work, so you'll need to manually initialize future-tstrings in a wrapper python module. For instance:

from future_tstrings import install

install()

from actual_main import main

if __name__ == '__main__':
    main()

Additionally, for zipped or frozen packages, the importer will not work. In such environments, you will need to use the future-tstrings command-line compiler before the code is zipped, frozen, etc.

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

future_tstrings-1.0.1.tar.gz (36.2 kB view details)

Uploaded Source

Built Distribution

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

future_tstrings-1.0.1-py3-none-any.whl (25.7 kB view details)

Uploaded Python 3

File details

Details for the file future_tstrings-1.0.1.tar.gz.

File metadata

  • Download URL: future_tstrings-1.0.1.tar.gz
  • Upload date:
  • Size: 36.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for future_tstrings-1.0.1.tar.gz
Algorithm Hash digest
SHA256 9dd8bf64f6d00a0a503831f720f0dec2876524707a0f8eca5c8a95cc2630d21a
MD5 575c13d5fed8843c6bd02f29b8757a03
BLAKE2b-256 1361ac517514abe2fd5ff7f9b66d9469bcaeb6d831f2f7dd9f4530ced5737618

See more details on using hashes here.

Provenance

The following attestation bundles were made for future_tstrings-1.0.1.tar.gz:

Publisher: python-publish.yml on mkzeender/future-tstrings

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file future_tstrings-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for future_tstrings-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2bc55c331e42ba72896a25a24a891aca30267cd5ac0f57e960b53db318780a8e
MD5 59526b40a2ea421b24cfe5d0ab752188
BLAKE2b-256 0c27a28ec6e1ad79b17356acfa9e5955a2e7d0f5df6d7e65762e524c889f093f

See more details on using hashes here.

Provenance

The following attestation bundles were made for future_tstrings-1.0.1-py3-none-any.whl:

Publisher: python-publish.yml on mkzeender/future-tstrings

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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