tstrings-backport


Nametstrings-backport JSON
Version 0.1.3 PyPI version JSON
download
home_pageNone
SummaryBackport of t-strings (PEP 750)
upload_time2025-08-05 18:27:02
maintainerNone
docs_urlNone
authorStefane Fermigier
requires_python>=3.9
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # tstrings: PEP 750-style Template Strings for Python < 3.14

This package provides a backport of the new [PEP 750](https://peps.python.org/pep-0750/) "t-string" template string syntax, for use with Python versions **before 3.14**.

## Usage

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

```python
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, closely matching the PEP 750 API.

## 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

```python
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:

```sh
nox
```

or just:

```sh
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 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.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "tstrings-backport",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "Stefane Fermigier",
    "author_email": "Stefane Fermigier <sf@abilian.com>",
    "download_url": "https://files.pythonhosted.org/packages/94/bb/5d9a7360992c4bd8ac41907df7453dbb06ce17b9772c19c19f11375a46f3/tstrings_backport-0.1.3.tar.gz",
    "platform": null,
    "description": "# tstrings: PEP 750-style Template Strings for Python < 3.14\n\nThis package provides a backport of the new [PEP 750](https://peps.python.org/pep-0750/) \"t-string\" template string syntax, for use with Python versions **before 3.14**.\n\n## Usage\n\nInstead of the new syntax `t\"...\"` (which is only available in Python 3.14+), use the function call form:\n\n```python\nfrom tstrings import t\n\nname = \"World\"\ntpl = t(\"Hello, {name}!\")\nprint(tpl.strings)         # (\"Hello, \", \"!\")\nprint(tpl.interpolations)  # (Interpolation(value=\"World\", expression=\"name\", ...),)\n```\n\nThe returned object is a `Template` with `.strings` and `.interpolations` attributes, closely matching the PEP 750 API.\n\n## Features\n\n- **String interpolation**: Supports `{expr}` expressions, including complex expressions.\n- **Debug specifier**: `{var=}` and `{var=:.2f}` forms, as in f-strings.\n- **Conversion specifiers**: `{val!r}`, `{val!s}`, `{val!a}`.\n- **Format specifiers**: `{num:.2f}`.\n- **Multiline expressions**: Supported.\n- **Error handling**: Raises `NameError` or `SyntaxError` for invalid expressions, as in f-strings (but at runtime, not at compile time).\n- **PEP 750 API**: Returns `Template` and `Interpolation` dataclasses matching the PEP.\n\n## Limitations\n\n- **No t-string literal syntax**: You must use `t(\"...\")`, not `t\"...\"`.\n- **No support for all edge cases**: Some advanced t-string edge cases may not be fully supported.\n\n## Examples\n\n```python\nfrom tstrings import t\n\nname = \"\"\nvalue = 1\nnum = 3.1415\nobj = {}\ndata = [1, 2, 3]\n\n# Simple\ntpl = t(\"Hello, {name}!\")\n# Debug\ntpl = t(\"{value=}\")\n# Format\ntpl = t(\"{num:.2f}\")\n# Conversion\ntpl = t(\"{obj!r}\")\n# Multiline\ntpl = t(\"\"\"\n    {sum(data)}\n\"\"\")\n```\n\n## Development & Testing\n\nInstall with `uv sync` and activate the virtualenv (`. .venv/bin/activate` or similar).\n\nRun the test suite with:\n\n```sh\nnox\n```\n\nor just:\n\n```sh\npytest\n```\n\nSee `tests/test_all.py` for coverage of all supported features.\n\nTests also include the tdom source code (modified for pre-3.14 syntax) and test suite.\n\n## How to help\n\nThis was hacked together in less than 2 hours. If you find it useful, please consider contributing fixes, improvements, or documentation!\n\nPull requests and issues are welcome.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Backport of t-strings (PEP 750)",
    "version": "0.1.3",
    "project_urls": {
        "Documentation": "https://github.com/abilian/tstrings-backport",
        "Issues": "https://github.com/abilian/tstrings-backport/issues",
        "Source": "https://github.com/abilian/tstrings-backport"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "5c6562a456e607719a37be7d235ab2187f1cfaa5c58a48eb629cb0cc165b46df",
                "md5": "1e0adf6d4fac257b6c9c93568e0bb59c",
                "sha256": "e814b7264aa540839c0eae025a2135130021bd49d53f16c79a576ec8b0ed6e85"
            },
            "downloads": -1,
            "filename": "tstrings_backport-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1e0adf6d4fac257b6c9c93568e0bb59c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 4262,
            "upload_time": "2025-08-05T18:27:01",
            "upload_time_iso_8601": "2025-08-05T18:27:01.276958Z",
            "url": "https://files.pythonhosted.org/packages/5c/65/62a456e607719a37be7d235ab2187f1cfaa5c58a48eb629cb0cc165b46df/tstrings_backport-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "94bb5d9a7360992c4bd8ac41907df7453dbb06ce17b9772c19c19f11375a46f3",
                "md5": "63bc082904c4d36b4ea4d8d6e9ee8629",
                "sha256": "29a3c09d4b7e17789abbdb3d09d3ae3ea794f149f9e4bf01282c8f6fc54cb6ff"
            },
            "downloads": -1,
            "filename": "tstrings_backport-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "63bc082904c4d36b4ea4d8d6e9ee8629",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 3553,
            "upload_time": "2025-08-05T18:27:02",
            "upload_time_iso_8601": "2025-08-05T18:27:02.467983Z",
            "url": "https://files.pythonhosted.org/packages/94/bb/5d9a7360992c4bd8ac41907df7453dbb06ce17b9772c19c19f11375a46f3/tstrings_backport-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-05 18:27:02",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "abilian",
    "github_project": "tstrings-backport",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "tstrings-backport"
}
        
Elapsed time: 2.16892s