|
| 1 | +""" |
| 2 | +PEP 750 template string support. |
| 3 | +
|
| 4 | +MicroPython module: https://docs.micropython.org/en/v1.28.0/library/string.templatelib.html |
| 5 | +
|
| 6 | +This module provides support for template strings (t-strings) as defined in |
| 7 | +`PEP 750 <https://peps.python.org/pep-0750/>`_. Template strings are created |
| 8 | +using the ``t`` prefix and provide access to both the literal string parts and |
| 9 | +interpolated values before they are combined. |
| 10 | +
|
| 11 | +**Availability:** template strings require ``MICROPY_PY_TSTRINGS`` to be enabled |
| 12 | +at compile time. They are enabled by default at the full feature level, which |
| 13 | +includes the alif, mimxrt and samd (SAMD51 only) ports, the unix coverage variant |
| 14 | +and the webassembly pyscript variant. |
| 15 | +""" |
| 16 | + |
| 17 | +# source version: v1.28.0-preview |
| 18 | +# origin module:: repos/micropython/docs/library/string.templatelib.rst |
| 19 | +from __future__ import annotations |
| 20 | +from _typeshed import Incomplete |
| 21 | +from typing_extensions import TypeVar, TypeAlias, Awaitable |
| 22 | + |
| 23 | +class Template: |
| 24 | + """ |
| 25 | + Represents a template string. Template objects are typically created by |
| 26 | + t-string syntax (``t"..."``) but can also be constructed directly using |
| 27 | + the constructor. |
| 28 | + """ |
| 29 | + def __init__(self, *args) -> None: ... |
| 30 | + def __iter__(self) -> Incomplete: |
| 31 | + """ |
| 32 | + Iterate over the template contents, yielding string parts and |
| 33 | + :class:`Interpolation` objects in the order they appear. Empty strings |
| 34 | + are omitted. |
| 35 | + """ |
| 36 | + ... |
| 37 | + def __add__(self, other) -> Incomplete: |
| 38 | + """ |
| 39 | + Concatenate two templates. Returns a new :class:`Template` combining |
| 40 | + the strings and interpolations from both templates. |
| 41 | +
|
| 42 | + :raises TypeError: if *other* is not a :class:`Template` |
| 43 | +
|
| 44 | + Template concatenation with ``str`` is prohibited to avoid ambiguity |
| 45 | + about whether the string should be treated as a literal or interpolation:: |
| 46 | +
|
| 47 | + t1 = t"Hello " |
| 48 | + t2 = t"World" |
| 49 | + result = t1 + t2 # Valid |
| 50 | +
|
| 51 | + # TypeError: cannot concatenate str to Template |
| 52 | + result = t1 + "World" |
| 53 | + """ |
| 54 | + ... |
| 55 | + |
| 56 | +class Interpolation: |
| 57 | + """ |
| 58 | + Represents an interpolated expression within a template string. All |
| 59 | + arguments can be passed as keyword arguments. |
| 60 | + """ |
| 61 | + def __init__(self, value, expression="", conversion=None, format_spec="") -> None: ... |
0 commit comments