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
Take care of @hugovk's suggestions.
  • Loading branch information
davepeck committed Jul 10, 2025
commit 15dd810744f00bdbec3dc98b68d8654df4a55a8b
4 changes: 2 additions & 2 deletions 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
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
f-string
f-string
f-strings

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 @@ -1318,7 +1318,7 @@ Glossary
See also :term:`borrowed reference`.

t-string
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
t-string
t-string
t-strings

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

Expand Down
2 changes: 1 addition & 1 deletion Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ Literals
* -1: no formatting
* 115: ``!s`` string formatting
* 114: ``!r`` repr formatting
* 97: ``!a`` ascii formatting
* 97: ``!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
2 changes: 1 addition & 1 deletion Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ iterations of the loop.

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

conversion = format >> 2
Expand Down
8 changes: 1 addition & 7 deletions Doc/library/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ Template strings ($-strings)

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>` feature and
: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
Expand Down Expand Up @@ -983,9 +983,3 @@ Helper functions
or ``None``, runs of whitespace characters are replaced by a single space
and leading and trailing whitespace are removed, otherwise *sep* is used to
split and join the words.



.. toctree::

string.templatelib.rst
18 changes: 10 additions & 8 deletions Doc/library/string.templatelib.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
:mod:`!string.templatelib` --- Templates and Interpolations for t-strings
:mod:`!string.templatelib` --- Templates and interpolations for t-strings
=========================================================================

.. module:: string.templatelib
Expand All @@ -11,9 +11,9 @@

.. seealso::

:ref:`T-strings tutorial <tut-t-strings>`
:ref:`Format strings <f-strings>`
:ref:`T-string literal syntax <t-strings>`
* :ref:`T-strings tutorial <tut-t-strings>`
* :ref:`Format strings <f-strings>`
* :ref:`T-string literal syntax <t-strings>`

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
* :ref:`T-string literal syntax <t-strings>`
* :ref:`Template string literal (t-string) syntax <t-strings>`


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
* :pep:`750`

.. _template-strings:
Expand Down Expand Up @@ -109,6 +109,8 @@ reassigned.

>>> t"".strings
('',)
>>> t"".values
()
>>> t"{'cheese'}".strings
('', '')
>>> t"{'cheese'}".values
Expand Down Expand Up @@ -191,10 +193,10 @@ reassigned.
:param value: The evaluated, in-scope result of the interpolation.
:type value: object

:param expression: The text of a valid Python expression, or an empty string
:param expression: The text of a valid Python expression, or an empty string.
:type expression: str

:param conversion: The optional :ref:`conversion <formatstrings>` to be used, one of r, s, and a,.
:param conversion: The optional :ref:`conversion <formatstrings>` to be used, one of r, s, and a.
:type conversion: Literal["a", "r", "s"] | None

:param format_spec: An optional, arbitrary string used as the :ref:`format specification <formatspec>` to present the value.
Expand Down Expand Up @@ -229,8 +231,8 @@ reassigned.

.. property:: conversion

:returns: The conversion to apply to the value, or ``None``
:rtype: Literal["a", "r", "s"] | None
:returns: The conversion to apply to the value, or ``None``.
:rtype: ``Literal["a", "r", "s"] | None``

The :attr:`!Interpolation.conversion` is the optional conversion to apply
to the value:
Expand Down
1 change: 1 addition & 0 deletions Doc/library/text.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Python's built-in string type in :ref:`textseq`.
.. toctree::

string.rst
string.templatelib.rst
re.rst
difflib.rst
textwrap.rst
Expand Down
16 changes: 8 additions & 8 deletions Doc/reference/lexical_analysis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,9 @@ escapes are not treated specially.
single: f'; formatted string literal
single: f"; formatted string literal

A string literal with ``'f'`` or ``'F'`` in its prefix is a
:dfn:`formatted string literal`; see :ref:`f-strings`. The ``'f'`` may be
combined with ``'r'``, but not with ``'b'`` or ``'u'``, therefore raw
A string literal with ``f`` or ``F`` in its prefix is a
:dfn:`formatted string literal`; see :ref:`f-strings`. The ``f`` may be
combined with ``r``, but not with ``b`` or ``u``, therefore raw
Comment on lines +564 to +566
Copy link
Member

Choose a reason for hiding this comment

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

unrelated changes; please revert. Also, arguably in lexical analysis we care about the character qua character.

Suggested change
A string literal with ``f`` or ``F`` in its prefix is a
:dfn:`formatted string literal`; see :ref:`f-strings`. The ``f`` may be
combined with ``r``, but not with ``b`` or ``u``, therefore raw
A string literal with ``'f'`` or ``'F'`` in its prefix is a
:dfn:`formatted string literal`; see :ref:`f-strings`. The ``'f'`` may be
combined with ``'r'``, but not with ``'b'`` or ``'u'``, therefore raw

formatted strings are possible, but formatted bytes literals are not.

In triple-quoted literals, unescaped newlines and quotes are allowed (and are
Expand Down Expand Up @@ -756,7 +756,7 @@ f-strings
.. versionadded:: 3.6

A :dfn:`formatted string literal` or :dfn:`f-string` is a string literal
that is prefixed with ``'f'`` or ``'F'``. These strings may contain
that is prefixed with ``f`` or ``F``. These strings may contain
Copy link
Member

Choose a reason for hiding this comment

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

likewise

Suggested change
that is prefixed with ``f`` or ``F``. These strings may contain
that is prefixed with ``'f'`` or ``'F'``. These strings may contain

replacement fields, which are expressions delimited by curly braces ``{}``.
While other string literals always have a constant value, formatted strings
are really expressions evaluated at run time.
Expand Down Expand Up @@ -922,7 +922,7 @@ 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
that is prefixed with ``t`` or ``T``. These strings follow the same
Copy link
Member

Choose a reason for hiding this comment

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

Per the above, I'd keep the quotation marks here, we're in lexical analysis.

Suggested change
that is prefixed with ``t`` or ``T``. These strings follow the same
that is prefixed with ``'t'`` or ``'T'``. These strings follow the same

syntax and evaluation rules as :ref:`formatted string literals <f-strings>`, with
the following differences:

Expand Down Expand Up @@ -950,9 +950,9 @@ the following differences:
:attr:`~string.templatelib.Template.interpolations` attribute will also
contain an ``Interpolation`` instance for the expression. By default, the
:attr:`~string.templatelib.Interpolation.conversion` attribute will be set to
``'r'`` (i.e. :func:`repr`), unless there is a conversion explicitly specified
(in which case it overrides the default) or a format specifier is provided (in
which case, the ``conversion`` defaults to ``None``).
``'r'`` (that is, :func:`repr`), unless there is a conversion explicitly
specified (in which case it overrides the default) or a format specifier is
provided (in which case, the ``conversion`` defaults to ``None``).


.. _numbers:
Expand Down
Empty file removed Doc/sphinx-warnings.txt
Empty file.
4 changes: 2 additions & 2 deletions Doc/tutorial/inputoutput.rst
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,11 @@ representation of the HTML:
...
>>> def parse_html(template: Template) -> Element:
... """
... Uses python's built-in HTMLParser to parse the template,
... Uses Python's built-in HTMLParser to parse the template,
... handle any interpolated values, and return a tree of
... Element instances.
... """
... pass
... ...
...

A full implementation of this function would be quite complex and is not
Expand Down
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