Skip to content

Commit 67b0a3c

Browse files
davepeckAA-Turner
authored andcommitted
pythongh-138558: Improve description of Interpolation.expression (pythonGH-139187)
(cherry picked from commit 880c952) Co-authored-by: Dave Peck <[email protected]> Co-authored-by: Adam Turner <[email protected]>
1 parent 8566ee2 commit 67b0a3c

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

Doc/library/string.templatelib.rst

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Template strings
2424
Template strings are a mechanism for custom string processing.
2525
They have the full flexibility of Python's :ref:`f-strings`,
2626
but return a :class:`Template` instance that gives access
27-
to the static and interpolated (in curly braces) parts of a string
27+
to the static and interpolated (in curly brackets) parts of a string
2828
*before* they are combined.
2929

3030
To write a t-string, use a ``'t'`` prefix instead of an ``'f'``, like so:
@@ -258,13 +258,16 @@ Types
258258
.. attribute:: expression
259259
:type: str
260260

261-
The text of a valid Python expression, or an empty string.
261+
For interpolations created by t-string literals, :attr:`!expression`
262+
is the expression text found inside the curly brackets (``{`` & ``}``),
263+
including any whitespace, excluding the curly brackets themselves,
264+
and ending before the first ``!``, ``:``, or ``=`` if any is present.
265+
For manually created interpolations, :attr:`!expression` is the arbitrary
266+
string provided when constructing the interpolation instance.
262267

263-
The :attr:`.expression` is the original text of the
264-
interpolation's Python expression, if the interpolation was created
265-
from a t-string literal. Developers creating interpolations manually
266-
should either set this to an empty string or choose a suitable valid
267-
Python expression.
268+
We recommend using valid Python expressions or the empty string for the
269+
``expression`` field of manually created :class:`!Interpolation`
270+
instances, although this is not enforced at runtime.
268271

269272
>>> t'{1 + 2}'.interpolations[0].expression
270273
'1 + 2'

0 commit comments

Comments
 (0)