@@ -209,7 +209,7 @@ Like ``Template``, it is a new class found in the :mod:`!string.templatelib` mod
209
209
def __new__ (
210
210
cls ,
211
211
value : object ,
212
- expression : str ,
212
+ expression : str = " " ,
213
213
conversion : Literal[" a" , " r" , " s" ] | None = None ,
214
214
format_spec : str = " " ,
215
215
):
@@ -226,14 +226,20 @@ The ``value`` attribute is the evaluated result of the interpolation:
226
226
template = t" Hello {name} "
227
227
assert template.interpolations[0 ].value == " World"
228
228
229
- The ``expression `` attribute is the *original text * of the interpolation:
229
+ When interpolations are created from a template string literal, the
230
+ ``expression `` attribute contains the *original text * of the interpolation:
230
231
231
232
.. code-block :: python
232
233
233
234
name = " World"
234
235
template = t" Hello {name} "
235
236
assert template.interpolations[0 ].expression == " name"
236
237
238
+ When developers explicitly construct an ``Interpolation ``, they may optionally
239
+ provide a value for the ``expression `` attribute. Even though it is stored as
240
+ a string, this *should * be a valid Python expression. If no value is provided,
241
+ the ``expression `` attribute defaults to the empty string (``"" ``).
242
+
237
243
We expect that the ``expression `` attribute will not be used in most template
238
244
processing code. It is provided for completeness and for use in debugging and
239
245
introspection. See both the `Common Patterns Seen in Processing Templates `_
@@ -462,7 +468,9 @@ The debug specifier, ``=``, is supported in template strings and behaves similar
462
468
to how it behaves in f-strings, though due to limitations of the implementation
463
469
there is a slight difference.
464
470
465
- In particular, ``t'{value=}' `` is treated as ``t'value={value!r}' ``:
471
+ In particular, ``t'{value=}' `` is treated as ``t'value={value!r}' ``. The first
472
+ static string is rewritten from ``"" `` to ``"value=" `` and the ``conversion ``
473
+ defaults to ``r ``:
466
474
467
475
.. code-block :: python
468
476
@@ -472,8 +480,11 @@ In particular, ``t'{value=}'`` is treated as ``t'value={value!r}'``:
472
480
assert template.interpolations[0 ].value == " World"
473
481
assert template.interpolations[0 ].conversion == " r"
474
482
475
- If a separate format string is also provided, ``t'{value=:fmt} `` is treated
476
- instead as ``t'value={value!s:fmt}' ``.
483
+ If a conversion is explicitly provided, it is kept: ``t'{value=!s}' ``
484
+ is treated as ``t'value={value!s}' ``.
485
+
486
+ If a format string is provided without a conversion, the ``conversion ``
487
+ is set to ``None ``: ``t'{value=:fmt}' `` is treated as ``t'value={value:fmt}' ``.
477
488
478
489
Whitespace is preserved in the debug specifier, so ``t'{value = }' `` is
479
490
treated as ``t'value = {value!r}' ``.
0 commit comments