Skip to content

Commit 3e38704

Browse files
committed
Use fully spelled-out attribute names for conv and expr.
1 parent 9fa905f commit 3e38704

1 file changed

Lines changed: 49 additions & 45 deletions

File tree

peps/pep-0750.rst

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,17 @@ Like ``Template``, it is a new class found in the ``string.templatelib`` module:
199199
200200
class Interpolation:
201201
value: object
202-
expr: str
203-
conv: Literal["a", "r", "s"] | None
202+
expression: str
203+
conversion: Literal["a", "r", "s"] | None
204204
format_spec: str
205205
206-
__match_args__ = ("value", "expr", "conv", "format_spec")
206+
__match_args__ = ("value", "expression", "conversion", "format_spec")
207207
208208
def __new__(
209209
cls,
210210
value: object,
211-
expr: str,
212-
conv: Literal["a", "r", "s"] | None = None,
211+
expression: str,
212+
conversion: Literal["a", "r", "s"] | None = None,
213213
format_spec: str = "",
214214
):
215215
...
@@ -225,20 +225,21 @@ The ``value`` attribute is the evaluated result of the interpolation:
225225
template = t"Hello {name}"
226226
assert template.interpolations[0].value == "World"
227227
228-
The ``expr`` attribute is the *original text* of the interpolation:
228+
The ``expression`` attribute is the *original text* of the interpolation:
229229

230230
.. code-block:: python
231231
232232
name = "World"
233233
template = t"Hello {name}"
234-
assert template.interpolations[0].expr == "name"
234+
assert template.interpolations[0].expression == "name"
235235
236-
We expect that the ``expr`` attribute will not be used in most template processing
237-
code. It is provided for completeness and for use in debugging and introspection.
238-
See both the `Common Patterns Seen in Processing Templates`_ section and the
239-
`Examples`_ section for more information on how to process template strings.
236+
We expect that the ``expression`` attribute will not be used in most template
237+
processing code. It is provided for completeness and for use in debugging and
238+
introspection. See both the `Common Patterns Seen in Processing Templates`_
239+
section and the `Examples`_ section for more information on how to process
240+
template strings.
240241

241-
The ``conv`` attribute is the :ref:`optional conversion <python:formatstrings>`
242+
The ``conversion`` attribute is the :ref:`optional conversion <python:formatstrings>`
242243
to be used, one of ``r``, ``s``, and ``a``, corresponding to ``repr()``,
243244
``str()``, and ``ascii()`` conversions. As with f-strings, no other conversions
244245
are supported:
@@ -247,9 +248,9 @@ are supported:
247248
248249
name = "World"
249250
template = t"Hello {name!r}"
250-
assert template.interpolations[0].conv == "r"
251+
assert template.interpolations[0].conversion == "r"
251252
252-
If no conversion is provided, ``conv`` is ``None``.
253+
If no conversion is provided, ``conversion`` is ``None``.
253254

254255
The ``format_spec`` attribute is the :ref:`format specification <python:formatspec>`.
255256
As with f-strings, this is an arbitrary string that defines how to present the value:
@@ -276,7 +277,7 @@ string (``""``). This matches the ``format_spec`` parameter of Python's
276277
:func:`python:format` built-in.
277278

278279
Unlike f-strings, it is up to code that processes the template to determine how to
279-
interpret the ``conv`` and ``format_spec`` attributes.
280+
interpret the ``conversion`` and ``format_spec`` attributes.
280281
Such code is not required to use these attributes, but when present they should
281282
be respected, and to the extent possible match the behavior of f-strings.
282283
It would be surprising if, for example, a template string that uses ``{value:.2f}``
@@ -405,21 +406,21 @@ The debug specifier, ``=``, is supported in template strings and behaves similar
405406
to how it behaves in f-strings, though due to limitations of the implementation
406407
there is a slight difference.
407408

408-
In particular, ``t'{expr=}'`` is treated as ``t'expr={expr!r}'``:
409+
In particular, ``t'{expression=}'`` is treated as ``t'expression={expression!r}'``:
409410

410411
.. code-block:: python
411412
412413
name = "World"
413414
template = t"Hello {name=}"
414415
assert template.strings[0] == "Hello name="
415416
assert template.interpolations[0].value == "World"
416-
assert template.interpolations[0].conv == "r"
417+
assert template.interpolations[0].conversion == "r"
417418
418-
If a separate format string is also provided, ``t'{expr=:fmt}`` is treated instead as
419-
``t'expr={expr!s:fmt}'``.
419+
If a separate format string is also provided, ``t'{expression=:fmt}`` is treated
420+
instead as ``t'expression={expression!s:fmt}'``.
420421

421-
Whitespace is preserved in the debug specifier, so ``t'{expr = }'`` is treated as
422-
``t'expr = {expr!r}'``.
422+
Whitespace is preserved in the debug specifier, so ``t'{expression = }'`` is
423+
treated as ``t'expression = {expression!r}'``.
423424

424425

425426
Raw Template Strings
@@ -509,12 +510,12 @@ specifiers like ``:.2f``. The full code is fairly simple:
509510
510511
from string.templatelib import Template, Interpolation
511512
512-
def convert(value: object, conv: Literal["a", "r", "s"] | None) -> object:
513-
if conv == "a":
513+
def convert(value: object, conversion: Literal["a", "r", "s"] | None) -> object:
514+
if conversion == "a":
514515
return ascii(value)
515-
elif conv == "r":
516+
elif conversion == "r":
516517
return repr(value)
517-
elif conv == "s":
518+
elif conversion == "s":
518519
return str(value)
519520
return value
520521
@@ -524,8 +525,8 @@ specifiers like ``:.2f``. The full code is fairly simple:
524525
match item:
525526
case str() as s:
526527
parts.append(s)
527-
case Interpolation(value, _, conv, format_spec):
528-
value = convert(value, conv)
528+
case Interpolation(value, _, conversion, format_spec):
529+
value = convert(value, conversion)
529530
value = format(value, format_spec)
530531
parts.append(value)
531532
return "".join(parts)
@@ -595,7 +596,7 @@ We can implement an improved version of ``StructuredMessage`` using template str
595596
@property
596597
def values(self) -> Mapping[str, object]:
597598
return {
598-
item.expr: item.value
599+
item.expression: item.value
599600
for item in self.template
600601
if isinstance(item, Interpolation)
601602
}
@@ -615,7 +616,8 @@ class. With template strings it is no longer necessary for developers to make
615616
sure that their format string and values dictionary are kept in sync; a single
616617
template string literal is all that is needed. The ``TemplateMessage``
617618
implementation can automatically extract structured keys and values from
618-
the ``Interpolation.expr`` and ``Interpolation.value`` attributes, respectively.
619+
the ``Interpolation.expression`` and ``Interpolation.value`` attributes,
620+
respectively.
619621

620622

621623
Approach 2: Custom Formatters
@@ -656,7 +658,7 @@ and a ``ValuesFormatter`` for JSON output:
656658
class ValuesFormatter(Formatter):
657659
def values(self, template: Template) -> Mapping[str, Any]:
658660
return {
659-
item.expr: item.value
661+
item.expression: item.value
660662
for item in template
661663
if isinstance(item, Interpolation)
662664
}
@@ -1193,10 +1195,10 @@ Earlier versions of this PEP proposed that the ``Template`` and ``Interpolation`
11931195
types should have their own implementations of ``__eq__`` and ``__hash__``.
11941196

11951197
``Templates`` were considered equal if their ``strings`` and ``interpolations``
1196-
were equal; ``Interpolations`` were considered equal if their ``value``, ``expr``,
1197-
``conv``, and ``format_spec`` were equal. Interpolation hashing was similar to
1198-
tuple hashing: an ``Interpolation`` was hashable if and only if its ``value``
1199-
was hashable.
1198+
were equal; ``Interpolations`` were considered equal if their ``value``,
1199+
``expression``, ``conversion``, and ``format_spec`` were equal. Interpolation
1200+
hashing was similar to tuple hashing: an ``Interpolation`` was hashable if and
1201+
only if its ``value`` was hashable.
12001202

12011203
This was rejected because ``Template.__hash__`` so defined was not useful as a
12021204
cache key in template processing code; we were concerned that it would be
@@ -1244,9 +1246,10 @@ There are several limitations with respect to round-tripping to the original
12441246
source text:
12451247

12461248
- ``Interpolation.format_spec`` defaults to ``""`` if not provided. It is therefore
1247-
impossible to distinguish ``t"{expr}"`` from ``t"{expr:}"``.
1249+
impossible to distinguish ``t"{expression}"`` from ``t"{expression:}"``.
12481250
- The debug specifier, ``=``, is treated as a special case. It is therefore not
1249-
possible to distinguish ``t"{expr=}"`` from ``t"expr={expr}"``.
1251+
possible to distinguish ``t"{expression=}"`` from
1252+
``t"expression={expression}"``.
12501253
- Finally, format specifiers in f-strings allow arbitrary nesting. In this PEP
12511254
and in the reference implementation, the specifier is eagerly evaluated
12521255
to set the ``format_spec`` in the ``Interpolation``, thereby losing
@@ -1302,23 +1305,24 @@ PEP adheres closely to :pep:`701`. Any changes to allowed values should be in a
13021305
separate PEP.
13031306

13041307

1305-
Removing ``conv`` From ``Interpolation``
1306-
----------------------------------------
1308+
Removing ``conversion`` From ``Interpolation``
1309+
----------------------------------------------
13071310

1308-
During the authoring of this PEP, we considered removing the ``conv`` attribute
1309-
from ``Interpolation`` and specifying that the conversion should be performed
1310-
eagerly, before ``Interpolation.value`` is set.
1311+
During the authoring of this PEP, we considered removing the ``conversion``
1312+
attribute from ``Interpolation`` and specifying that the conversion should be
1313+
performed eagerly, before ``Interpolation.value`` is set.
13111314

13121315
This was done to simplify the work of writing template processing code. The
1313-
``conv`` attribute is of limited extensibility (it is typed as
1316+
``conversion`` attribute is of limited extensibility (it is typed as
13141317
``Literal["r", "s", "a"] | None``). It is not clear that it adds significant
13151318
value or flexibility to template strings that couldn't better be achieved with
13161319
custom format specifiers. Unlike with format specifiers, there is no
1317-
equivalent to Python's :func:`python:format` built-in. (Instead, we include an
1320+
equivalent to Python's :func:`python:format` built-in. (Instead, we include a
13181321
sample implementation of ``convert()`` in the `Examples`_ section.)
13191322

1320-
Ultimately we decided to keep the ``conv`` attribute in the ``Interpolation`` type
1321-
to maintain compatibility with f-strings and to allow for future extensibility.
1323+
Ultimately we decided to keep the ``conversion`` attribute in the
1324+
``Interpolation`` type to maintain compatibility with f-strings and to allow
1325+
for future extensibility.
13221326

13231327

13241328
Alternate Interpolation Symbols

0 commit comments

Comments
 (0)