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

Open
wants to merge 48 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 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
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
7 changes: 6 additions & 1 deletion Doc/glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ Glossary
core and with user code.

f-string
String literals prefixed with ``'f'`` or ``'F'`` are commonly called
String literals prefixed with ``f`` or ``F`` are commonly called
"f-strings" which is short for
:ref:`formatted string literals <f-strings>`. See also :pep:`498`.

Expand Down Expand Up @@ -1317,6 +1317,11 @@ Glossary

See also :term:`borrowed reference`.

t-string
String literals prefixed with ``t`` or ``T`` are commonly called
"t-strings" which is short for
:ref:`template string literals <t-strings>`.

text encoding
A string in Python is a sequence of Unicode code points (in range
``U+0000``--``U+10FFFF``). To store or transfer a string, it needs to be
Expand Down
54 changes: 51 additions & 3 deletions Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ Literals
* ``conversion`` is an integer:

* -1: no formatting
* 115: ``!s`` string formatting
* 114: ``!r`` repr formatting
* 97: ``!a`` ascii formatting
* 115 (``ord('s')``): ``!s`` string formatting
* 114 (``ord('r')``): ``!r`` repr formatting
* 97 (``ord('a')``): ``!a`` ASCII formatting

* ``format_spec`` is a :class:`JoinedStr` node representing the formatting
of the value, or ``None`` if no format was specified. Both
Expand Down Expand Up @@ -325,6 +325,54 @@ Literals
Constant(value='.3')]))]))


.. class:: TemplateStr(values)

A t-string, comprising a series of :class:`Interpolation` and :class:`Constant`
nodes.

.. doctest::

>>> print(ast.dump(ast.parse('t"{name} finished {place:ordinal}"', mode='eval'), indent=4))
Expression(
body=TemplateStr(
values=[
Interpolation(
value=Name(id='name'),
str='name',
conversion=-1),
Constant(value=' finished '),
Interpolation(
value=Name(id='place'),
str='place',
conversion=-1,
format_spec=JoinedStr(
values=[
Constant(value='ordinal')]))]))

.. versionadded:: 3.14


.. class:: Interpolation(value, str, conversion, format_spec)

Node representing a single interpolation field in a t-string.

* ``value`` is any expression node (such as a literal, a variable, or a
function call).
* ``str`` is a constant containing the text of the interpolation expression.
* ``conversion`` is an integer:

* -1: no conversion
* 115: ``!s`` string conversion
* 114: ``!r`` repr conversion
* 97: ``!a`` ascii conversion

* ``format_spec`` is a :class:`JoinedStr` node representing the formatting
of the value, or ``None`` if no format was specified. Both
``conversion`` and ``format_spec`` can be set at the same time.

.. versionadded:: 3.14


.. class:: List(elts, ctx)
Tuple(elts, ctx)

Expand Down
42 changes: 42 additions & 0 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,48 @@ iterations of the loop.
.. versionadded:: 3.12


.. opcode:: BUILD_TEMPLATE

Constructs a new :class:`~string.templatelib.Template` from a tuple
of strings and a tuple of interpolations and pushes the resulting instance
onto the stack::

interpolations = STACK.pop()
strings = STACK.pop()
STACK.append(_build_template(strings, interpolations))

.. versionadded:: 3.14


.. opcode:: BUILD_INTERPOLATION (format)

Constructs a new :class:`~string.templatelib.Interpolation` from a
value and its source expression and pushes the resulting instance onto the
stack.

If no conversion or format specification is present, ``format`` is set to
``2``.

If the low bit of ``format`` is set, it indicates that the interpolation
contains a format specification.

If ``format >> 2`` is non-zero, it indicates that the interpolation
contains a conversion. The value of ``format >> 2`` is the conversion type
(``0`` for no conversion, ``1`` for ``!s``, ``2`` for ``!r``, and
``3`` for ``!a``)::

conversion = format >> 2
if format & 1:
format_spec = STACK.pop()
else:
format_spec = None
expression = STACK.pop()
value = STACK.pop()
STACK.append(_build_interpolation(value, expression, conversion, format_spec))

.. versionadded:: 3.14


.. opcode:: BUILD_TUPLE (count)

Creates a tuple consuming *count* items from the stack, and pushes the
Expand Down
6 changes: 3 additions & 3 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2673,9 +2673,9 @@ For example:
lead to a number of common errors (such as failing to display tuples and
dictionaries correctly). Using the newer :ref:`formatted string literals
<f-strings>`, the :meth:`str.format` interface, or :ref:`template strings
<template-strings>` may help avoid these errors. Each of these
alternatives provides their own trade-offs and benefits of simplicity,
flexibility, and/or extensibility.
($-strings) <template-strings-pep292>` may help avoid these errors.
Each of these alternatives provides their own trade-offs and benefits of
simplicity, flexibility, and/or extensibility.

String objects have one unique built-in operation: the ``%`` operator (modulo).
This is also known as the string *formatting* or *interpolation* operator.
Expand Down
23 changes: 17 additions & 6 deletions Doc/library/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ Format String Syntax
The :meth:`str.format` method and the :class:`Formatter` class share the same
syntax for format strings (although in the case of :class:`Formatter`,
subclasses can define their own format string syntax). The syntax is
related to that of :ref:`formatted string literals <f-strings>`, but it is
less sophisticated and, in particular, does not support arbitrary expressions.
related to that of :ref:`formatted string literals <f-strings>` and
:ref:`template string literals <t-strings>`, but it is less sophisticated
and, in particular, does not support arbitrary expressions.

.. index::
single: {} (curly brackets); in string formatting
Expand Down Expand Up @@ -306,7 +307,7 @@ Format Specification Mini-Language

"Format specifications" are used within replacement fields contained within a
format string to define how individual values are presented (see
:ref:`formatstrings` and :ref:`f-strings`).
:ref:`formatstrings`, :ref:`f-strings`, and :ref:`t-strings`).
They can also be passed directly to the built-in
:func:`format` function. Each formattable type may define how the format
specification is to be interpreted.
Expand Down Expand Up @@ -789,10 +790,20 @@ Nesting arguments and more complex examples::



.. _template-strings:
.. _template-strings-pep292:

Template strings
----------------
Template strings ($-strings)
----------------------------

.. note::

The feature described here was introduced in Python 2.4. It is unrelated
to, and should not be confused with, the newer
:ref:`template strings <template-strings>` and
:ref:`t-string literal syntax <t-strings>` introduced in Python 3.14.
T-string literals evaluate to instances of a different
:class:`~string.templatelib.Template` class, found in the
:mod:`string.templatelib` module.

Template strings provide simpler string substitutions as described in
:pep:`292`. A primary use case for template strings is for
Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy