https://github.com/python/cpython/commit/880c9526f91960b9cba557a18b54e2c32d2f254e
commit: 880c9526f91960b9cba557a18b54e2c32d2f254e
branch: main
author: Dave Peck <[email protected]>
committer: AA-Turner <[email protected]>
date: 2025-10-04T22:06:56+01:00
summary:

gh-138558: Improve description of ``Interpolation.expression`` (#139187)

Co-authored-by: Adam Turner <[email protected]>

files:
M Doc/library/string.templatelib.rst

diff --git a/Doc/library/string.templatelib.rst 
b/Doc/library/string.templatelib.rst
index 85d65fa9de1e22..a5b2d796aaf4b8 100644
--- a/Doc/library/string.templatelib.rst
+++ b/Doc/library/string.templatelib.rst
@@ -24,7 +24,7 @@ Template strings
 Template strings are a mechanism for custom string processing.
 They have the full flexibility of Python's :ref:`f-strings`,
 but return a :class:`Template` instance that gives access
-to the static and interpolated (in curly braces) parts of a string
+to the static and interpolated (in curly brackets) parts of a string
 *before* they are combined.
 
 To write a t-string, use a ``'t'`` prefix instead of an ``'f'``, like so:
@@ -258,13 +258,16 @@ Types
    .. attribute:: expression
       :type: str
 
-      The text of a valid Python expression, or an empty string.
+      For interpolations created by t-string literals, :attr:`!expression`
+      is the expression text found inside the curly brackets (``{`` & ``}``),
+      including any whitespace, excluding the curly brackets themselves,
+      and ending before the first ``!``, ``:``, or ``=`` if any is present.
+      For manually created interpolations, :attr:`!expression` is the arbitrary
+      string provided when constructing the interpolation instance.
 
-      The :attr:`.expression` is the original text of the
-      interpolation's Python expression, if the interpolation was created
-      from a t-string literal. Developers creating interpolations manually
-      should either set this to an empty string or choose a suitable valid
-      Python expression.
+      We recommend using valid Python expressions or the empty string for the
+      ``expression`` field of manually created :class:`!Interpolation`
+      instances, although this is not enforced at runtime.
 
       >>> t'{1 + 2}'.interpolations[0].expression
       '1 + 2'

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to