-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-132661: Document t-strings and templatelib
#135229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5a8dbfa
09a1e9e
ec44c2b
d8904b6
550aa6d
a20e058
1f30739
d935dd6
1e47362
7b660be
00a535d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,156 @@ | ||||||||||||||||||||||||||
:mod:`!string.templatelib` --- Templates and Interpolations for t-strings | ||||||||||||||||||||||||||
========================================================================= | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. module:: string.templatelib | ||||||||||||||||||||||||||
:synopsis: Support for t-string literals. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
**Source code:** :source:`Lib/string/templatelib.py` | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
-------------- | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. seealso:: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
:ref:`f-strings` -- Format strings (f-strings) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. _templatelib-template: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Template | ||||||||||||||||||||||||||
-------- | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
The :class:`Template` class describes the contents of a template string. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
The most common way to create a new :class:`Template` instance is to use the t-string literal syntax. This syntax is identical to that of :ref:`f-strings`, except that the string is prefixed with a ``t`` instead of an ``f``. For example, the following code creates a :class:`Template` that can be used to format strings: | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We try to avoid creating too many hyperlinks, see: So we don't need to link this repeated mentions. We can prevent Sphinx from linking like this, but it still applies the other formatting:
Suggested change
Also, please try and wrap at ~80 chars. https://devguide.python.org/documentation/markup/#use-of-whitespace |
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
>>> name = "World" | ||||||||||||||||||||||||||
>>> greeting = t"Hello {name}!" | ||||||||||||||||||||||||||
>>> type(greeting) | ||||||||||||||||||||||||||
<class 'string.templatelib.Template'> | ||||||||||||||||||||||||||
>>> print(list(greeting)) | ||||||||||||||||||||||||||
['Hello ', Interpolation('World', 'name', None, ''), '!'] | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
It is also possible to create a :class:`Template` directly, using its constructor. This takes an arbitrary collection of strings and :class:`Interpolation` instances: | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
>>> from string.templatelib import Interpolation, Template | ||||||||||||||||||||||||||
>>> name = "World" | ||||||||||||||||||||||||||
>>> greeting = Template("Hello, ", Interpolation(name, "name"), "!") | ||||||||||||||||||||||||||
>>> print(list(greeting)) | ||||||||||||||||||||||||||
['Hello, ', Interpolation('World', 'name', None, ''), '!'] | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. class:: Template(*args) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Create a new :class:`Template` object. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
:param args: A mix of strings and :class:`Interpolation` instances in any order. | ||||||||||||||||||||||||||
:type args: str | Interpolation | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
If two or more consecutive strings are passed, they will be concatenated into a single value in the :attr:`~Template.strings` attribute. For example, the following code creates a :class:`Template` with a single final string: | ||||||||||||||||||||||||||
Comment on lines
+43
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, we try and avoid linking back to the same unit.
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
>>> from string.templatelib import Template | ||||||||||||||||||||||||||
>>> greeting = Template("Hello ", "World", "!") | ||||||||||||||||||||||||||
>>> print(greeting.strings) | ||||||||||||||||||||||||||
('Hello World!',) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
If two or more consecutive interpolations are passed, they will be treated as separate interpolations and an empty string will be inserted between them. For example, the following code creates a template with a single value in the :attr:`~Template.strings` attribute: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
>>> from string.templatelib import Interpolation, Template | ||||||||||||||||||||||||||
>>> greeting = Template(Interpolation("World", "name"), Interpolation("!", "punctuation")) | ||||||||||||||||||||||||||
>>> print(greeting.strings) | ||||||||||||||||||||||||||
('', '', '') | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. attribute:: strings | ||||||||||||||||||||||||||
:type: tuple[str, ...] | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
A :ref:`tuple <tut-tuples>` of the static strings in the template. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
>>> name = "World" | ||||||||||||||||||||||||||
>>> print(t"Hello {name}!".strings) | ||||||||||||||||||||||||||
('Hello ', '!') | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Empty strings *are* included in the tuple: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
>>> name = "World" | ||||||||||||||||||||||||||
>>> print(t"Hello {name}{name}!".strings) | ||||||||||||||||||||||||||
('Hello ', '', '!') | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. attribute:: interpolations | ||||||||||||||||||||||||||
:type: tuple[Interpolation, ...] | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
A tuple of the interpolations in the template. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
>>> name = "World" | ||||||||||||||||||||||||||
>>> print(t"Hello {name}!".interpolations) | ||||||||||||||||||||||||||
(Interpolation('World', 'name', None, ''),) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. attribute:: values | ||||||||||||||||||||||||||
:type: tuple[Any, ...] | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
A tuple of all interpolated values in the template. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
>>> name = "World" | ||||||||||||||||||||||||||
>>> print(t"Hello {name}!".values) | ||||||||||||||||||||||||||
('World',) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. method:: __iter__() | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Iterate over the template, yielding each string and :class:`Interpolation` in order. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
>>> name = "World" | ||||||||||||||||||||||||||
>>> print(list(t"Hello {name}!")) | ||||||||||||||||||||||||||
['Hello ', Interpolation('World', 'name', None, ''), '!'] | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Empty strings are *not* included in the iteration: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
>>> name = "World" | ||||||||||||||||||||||||||
>>> print(list(t"Hello {name}{name}")) | ||||||||||||||||||||||||||
['Hello ', Interpolation('World', 'name', None, ''), Interpolation('World', 'name', None, '')] | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
:returns: An iterable of all the parts in the template. | ||||||||||||||||||||||||||
:rtype: typing.Iterator[str | Interpolation] | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. class:: Interpolation(*args) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Create a new :class:`Interpolation` object. | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
:param value: The evaluated, in-scope result of the interpolation. | ||||||||||||||||||||||||||
:type value: object | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
:param expression: The original *text* of the interpolation's Python :ref:`expressions <expressions>`. | ||||||||||||||||||||||||||
:type expression: str | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
:param conversion: The optional :ref:`conversion <formatstrings>` to be used, one of r, s, and a,. | ||||||||||||||||||||||||||
:type value: Literal["a", "r", "s"] | None | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
:param format_spec: An optional, arbitrary string used as the :ref:`format specification <formatspec>` to present the value. | ||||||||||||||||||||||||||
:type expression: str = "" | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
The :class:`Interpolation` type represents an expression inside a template string. It is shallow immutable -- its attributes cannot be reassigned. | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
>>> name = "World" | ||||||||||||||||||||||||||
>>> template = t"Hello {name}" | ||||||||||||||||||||||||||
>>> template.interpolations[0].value | ||||||||||||||||||||||||||
'World' | ||||||||||||||||||||||||||
>>> template.interpolations[0].value = "Galaxy" | ||||||||||||||||||||||||||
Traceback (most recent call last): | ||||||||||||||||||||||||||
File "<input>", line 1, in <module> | ||||||||||||||||||||||||||
AttributeError: readonly attribute | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
While f-strings and t-strings are largely similar in syntax and expectations, the :attr:`~Interpolation.conversion` and :attr:`~Interpolation.format_spec` behave differently. With f-strings, these are applied to the resulting value automatically. For example, in this ``format_spec``: | ||||||||||||||||||||||||||
Check warning on line 140 in Doc/library/string.templatelib.rst
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
>>> value = 42 | ||||||||||||||||||||||||||
>>> f"Value: {value:.2f}" | ||||||||||||||||||||||||||
'Value: 42.00' | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
With a t-string :class:`!Interpolation`, the template function is expected to apply this to the value: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
>>> value = 42 | ||||||||||||||||||||||||||
>>> template = t"Value: {value:.2f}" | ||||||||||||||||||||||||||
>>> template.interpolations[0].value | ||||||||||||||||||||||||||
42 | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. property:: __match_args__ | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
:returns: A tuple of the attributes to use for structural pattern matching. | ||||||||||||||||||||||||||
:rtype: (Literal["value"], Literal["expression"], Literal["conversion"], Literal["format_spec"]) |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -913,6 +913,31 @@ See also :pep:`498` for the proposal that added formatted string literals, | |||||||||||||||||
and :meth:`str.format`, which uses a related format string mechanism. | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
.. _t-strings: | ||||||||||||||||||
.. _template-string-literals: | ||||||||||||||||||
|
||||||||||||||||||
t-strings | ||||||||||||||||||
--------- | ||||||||||||||||||
|
||||||||||||||||||
.. versionadded:: 3.14 | ||||||||||||||||||
|
||||||||||||||||||
A :dfn:`template string literal` or :dfn:`t-string` is a string literal | ||||||||||||||||||
that is prefixed with ``'t'`` or ``'T'``. These strings follow the same | ||||||||||||||||||
syntax and evaluation rules as `formatted string literals <f-strings>`_, with | ||||||||||||||||||
the following differences: | ||||||||||||||||||
|
||||||||||||||||||
- Rather than evaluating to a `str` object, t-strings evaluate to a | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lint fix, either use double backticks (this is RST not MD):
Suggested change
Or create link:
Suggested change
|
||||||||||||||||||
`Template` object from the :mod:`string.templatelib` module. | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
- Evaluated expressions are *not* formatted using the | ||||||||||||||||||
:func:`format` protocol; :meth:`~object.__format__` is *not* invoked. Instead, | ||||||||||||||||||
the expressions are evaluated and a new `Interpolation` object (also from the | ||||||||||||||||||
:mod:`string.templatelib` module) is created, which contains the evaluated | ||||||||||||||||||
value of the expression. That `Interpolation` object is found in the containing | ||||||||||||||||||
`Template`. | ||||||||||||||||||
Comment on lines
+934
to
+937
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
.. _numbers: | ||||||||||||||||||
|
||||||||||||||||||
Numeric literals | ||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We try to avoid creating too many hyperlinks, see:
https://devguide.python.org/documentation/style-guide/#links
So we don't need to link this one back to this same section. We can prevent Sphinx from linking like this, but it still applies the other formatting: