@@ -24,7 +24,7 @@ Template strings
24
24
Template strings are a mechanism for custom string processing.
25
25
They have the full flexibility of Python's :ref: `f-strings `,
26
26
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
28
28
*before * they are combined.
29
29
30
30
To write a t-string, use a ``'t' `` prefix instead of an ``'f' ``, like so:
@@ -258,13 +258,16 @@ Types
258
258
.. attribute :: expression
259
259
:type: str
260
260
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.
262
267
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.
268
271
269
272
>>> t' {1 + 2}' .interpolations[0 ].expression
270
273
'1 + 2'
0 commit comments