Skip to content

Commit c77e600

Browse files
authored
PEP 750: Collect spec fixes discovered during final implementation work (#4364)
1 parent 0465dd1 commit c77e600

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

peps/pep-0750.rst

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ Like ``Template``, it is a new class found in the :mod:`!string.templatelib` mod
209209
def __new__(
210210
cls,
211211
value: object,
212-
expression: str,
212+
expression: str = "",
213213
conversion: Literal["a", "r", "s"] | None = None,
214214
format_spec: str = "",
215215
):
@@ -226,14 +226,20 @@ The ``value`` attribute is the evaluated result of the interpolation:
226226
template = t"Hello {name}"
227227
assert template.interpolations[0].value == "World"
228228
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:
230231

231232
.. code-block:: python
232233
233234
name = "World"
234235
template = t"Hello {name}"
235236
assert template.interpolations[0].expression == "name"
236237
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+
237243
We expect that the ``expression`` attribute will not be used in most template
238244
processing code. It is provided for completeness and for use in debugging and
239245
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
462468
to how it behaves in f-strings, though due to limitations of the implementation
463469
there is a slight difference.
464470

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``:
466474

467475
.. code-block:: python
468476
@@ -472,8 +480,11 @@ In particular, ``t'{value=}'`` is treated as ``t'value={value!r}'``:
472480
assert template.interpolations[0].value == "World"
473481
assert template.interpolations[0].conversion == "r"
474482
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}'``.
477488

478489
Whitespace is preserved in the debug specifier, so ``t'{value = }'`` is
479490
treated as ``t'value = {value!r}'``.

0 commit comments

Comments
 (0)