Skip to content

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

Merged
merged 57 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
5a8dbfa
Initial commit.
davepeck Jun 6, 2025
09a1e9e
Make clear the preceding discussion about t-strings creating an insta…
pauleveritt Jun 8, 2025
ec44c2b
Get the import order sorted.
pauleveritt Jun 8, 2025
d8904b6
Correct the Interpolation() calls.
pauleveritt Jun 8, 2025
550aa6d
Add documentation for Interpolation.
pauleveritt Jun 8, 2025
a20e058
Convert to use Sphinx modifiers for type, returns, and rtype.
pauleveritt Jun 8, 2025
1f30739
Add an entry to the glossary.
pauleveritt Jun 8, 2025
d935dd6
Find any occurrences of f-strings that should also mention t-strings.
pauleveritt Jun 8, 2025
1e47362
Fix doctests for str.templatelib
davepeck Jun 12, 2025
7b660be
Start writing the t-strings part of lexical analysis
davepeck Jun 12, 2025
00a535d
Merge branch 'main' into docs/pep750-first-pass
davepeck Jun 12, 2025
fcd74e6
Fix lint issues
davepeck Jun 17, 2025
a796f5d
Fix further sphinx warnings
davepeck Jun 17, 2025
21d337c
More!
davepeck Jun 17, 2025
d043381
Merge branch 'main' into docs/pep750-first-pass
davepeck Jun 26, 2025
9a0a301
Wrap up lexical analysis updates... I think?
davepeck Jun 26, 2025
f2e5ca4
Document the new AST nodes (TemplateStr and Interpolation)
davepeck Jun 26, 2025
8484b81
First pass at documenting BUILD_TEMPLATE and BUILD_INTERPOLATION
davepeck Jun 26, 2025
56ef703
Merge branch 'main' into docs/pep750-first-pass
davepeck Jun 27, 2025
5a8cf1b
Clarify distinction between string.Template and string.templatelib.Te…
davepeck Jun 27, 2025
127ebc6
Simplify the NOTE
davepeck Jun 27, 2025
73e1222
Okay, I'll stop wordsmithing this for now
davepeck Jun 27, 2025
530cb6d
Fix formatting/parameters for TemplateStr/Interpolation AST nodes
davepeck Jun 27, 2025
eaec534
Further ast/dis documentation cleanup
davepeck Jun 27, 2025
05b5beb
Substantial updates to string.templatelib.rst docs
davepeck Jun 27, 2025
680189a
Clarify BUILD_INTERPOLATION behavior
davepeck Jun 27, 2025
71ddbf4
Write a template string "tutorial" in the "fancy input output formatt…
davepeck Jun 29, 2025
1c0ed70
Fix lint issue in inputoutput.rst
davepeck Jun 30, 2025
ecc86c3
Merge branch 'main' into docs/pep750-first-pass
davepeck Jun 30, 2025
64c6758
Fix further lint issues in inputoutput.rst
davepeck Jun 30, 2025
16a995d
Merge branch 'main' into docs/pep750-first-pass
davepeck Jun 30, 2025
7f376d7
Merge branch 'main' into docs/pep750-first-pass
davepeck Jul 8, 2025
9e87880
Minor updates for clarity.
davepeck Jul 8, 2025
574c29c
Merge branch 'main' into docs/pep750-first-pass
davepeck Jul 8, 2025
6f0f95a
Update Doc/reference/lexical_analysis.rst
davepeck Jul 8, 2025
95df68f
Merge branch 'main' into docs/pep750-first-pass
davepeck Jul 9, 2025
e562c4e
Merge branch 'main' into docs/pep750-first-pass
davepeck Jul 9, 2025
25d9a6f
Merge branch 'main' into docs/pep750-first-pass
davepeck Jul 10, 2025
72b36fe
Merge branch 'main' into docs/pep750-first-pass
davepeck Jul 10, 2025
15dd810
Take care of @hugovk's suggestions.
davepeck Jul 10, 2025
fd38fba
Take care of all but one of @encukou's feedbacks
davepeck Jul 10, 2025
84dbc16
Attempt to improve "Template strings" intro paragraph
davepeck Jul 10, 2025
54914fe
Update Doc/library/string.templatelib.rst
davepeck Jul 12, 2025
7eca01e
Update Doc/library/string.templatelib.rst
davepeck Jul 12, 2025
687c506
Better use of ..describe per @encukou.
davepeck Jul 12, 2025
3827427
Merge branch 'docs/pep750-first-pass' of github.com:t-strings/cpython…
davepeck Jul 12, 2025
79169d1
Update Doc/library/string.templatelib.rst
davepeck Jul 12, 2025
5d7491c
Remove t-string tutorial (for now)
davepeck Jul 12, 2025
115eaa5
Merge branch 'main' into docs/pep750-first-pass
davepeck Jul 15, 2025
04c2e8d
Add documentation for string.templatelib.convert()
davepeck Jul 15, 2025
2da418b
Update Doc/library/string.templatelib.rst
davepeck Jul 19, 2025
9070840
Merge branch 'main' into docs/pep750-first-pass
davepeck Jul 21, 2025
5af0434
Revert unrelated change in inputoutput.rst
davepeck Jul 21, 2025
437320d
"shallow immutable" -> "immutable"
davepeck Jul 21, 2025
d69d8e0
Use :type:, not :rtype:, for attributes.
davepeck Jul 21, 2025
ae6f072
'is always' -> 'always has'
davepeck Jul 21, 2025
6446762
Merge branch 'main' into docs/pep750-first-pass
davepeck Jul 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix lint issues
  • Loading branch information
davepeck committed Jun 17, 2025
commit fcd74e64c74492873d42533d45fc5200919a7a80
12 changes: 6 additions & 6 deletions Doc/library/string.templatelib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
.. _templatelib-template:

Template
--------

Check warning on line 20 in Doc/library/string.templatelib.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:class reference target not found: str = "" [ref.class]

The :class:`Template` class describes the contents of a template string.
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:
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:

>>> name = "World"
>>> greeting = t"Hello {name}!"
Expand All @@ -30,7 +30,7 @@
>>> 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:
It is also possible to create a :class:`!Template` directly, using its constructor. This takes an arbitrary collection of strings and :class:`Interpolation` instances:

>>> from string.templatelib import Interpolation, Template
>>> name = "World"
Expand All @@ -40,7 +40,7 @@

.. class:: Template(*args)

Create a new :class:`Template` object.
Create a new :class:`!Template` object.

:param args: A mix of strings and :class:`Interpolation` instances in any order.
:type args: str | Interpolation
Expand Down Expand Up @@ -112,7 +112,7 @@

.. class:: Interpolation(*args)

Create a new :class:`Interpolation` object.
Create a new :class:`!Interpolation` object.

:param value: The evaluated, in-scope result of the interpolation.
:type value: object
Expand All @@ -126,7 +126,7 @@
: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.
The :class:`!Interpolation` type represents an expression inside a template string. It is shallow immutable -- its attributes cannot be reassigned.

>>> name = "World"
>>> template = t"Hello {name}"
Expand All @@ -137,7 +137,7 @@
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

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:attr reference target not found: Interpolation.format_spec [ref.attr]

Check warning on line 140 in Doc/library/string.templatelib.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:attr reference target not found: Interpolation.conversion [ref.attr]

>>> value = 42
>>> f"Value: {value:.2f}"
Expand Down
13 changes: 7 additions & 6 deletions Doc/reference/lexical_analysis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -926,15 +926,16 @@
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
`Template` object from the :mod:`string.templatelib` module.
- Rather than evaluating to a ``str`` object, t-strings evaluate to a
:class:`~string.templatelib.Template` object from the
Comment on lines +929 to +930
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Rather than evaluating to a ``str`` object, t-strings evaluate to a
:class:`~string.templatelib.Template` object from the
* Rather than evaluating to a ``str`` object, template string literals evaluate
to a :class:`~string.templatelib.Template` object from the

:mod:`string.templatelib` module.

- Evaluated expressions are *not* formatted using the

Check warning on line 933 in Doc/reference/lexical_analysis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:class reference target not found: string.templatelib.Interpolation` [ref.class]
: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`.
the expressions are evaluated and a new :class:`~string.templatelib.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``.



Expand Down
Loading