@@ -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}' ``.
@@ -549,7 +560,7 @@ Examples
549
560
========
550
561
551
562
All examples in this section of the PEP have fully tested reference implementations
552
- available in the public `pep750-examples <https://github.com/davepeck /pep750-examples >`_
563
+ available in the public `pep750-examples <https://github.com/t-strings /pep750-examples >`_
553
564
git repository.
554
565
555
566
@@ -602,8 +613,8 @@ specifiers like ``:.2f``. The full code is fairly simple:
602
613
603
614
See `fstring.py `__ and `test_fstring.py `__.
604
615
605
- __ https://github.com/davepeck /pep750-examples/blob/main/pep/fstring.py
606
- __ https://github.com/davepeck /pep750-examples/blob/main/pep/test_fstring.py
616
+ __ https://github.com/t-strings /pep750-examples/blob/main/pep/fstring.py
617
+ __ https://github.com/t-strings /pep750-examples/blob/main/pep/test_fstring.py
607
618
608
619
609
620
Example: Structured Logging
@@ -775,8 +786,8 @@ logging:
775
786
776
787
See `logging.py `__ and `test_logging.py `__.
777
788
778
- __ https://github.com/davepeck /pep750-examples/blob/main/pep/logging.py
779
- __ https://github.com/davepeck /pep750-examples/blob/main/pep/test_logging.py
789
+ __ https://github.com/t-strings /pep750-examples/blob/main/pep/logging.py
790
+ __ https://github.com/t-strings /pep750-examples/blob/main/pep/test_logging.py
780
791
781
792
782
793
Example: HTML Templating
@@ -785,7 +796,7 @@ Example: HTML Templating
785
796
This PEP contains several short HTML templating examples. It turns out that the
786
797
"hypothetical" ``html() `` function mentioned in the `Motivation `_ section
787
798
(and a few other places in this PEP) exists and is available in the
788
- `pep750-examples repository <https://github.com/davepeck /pep750-examples/ >`_.
799
+ `pep750-examples repository <https://github.com/t-strings /pep750-examples/ >`_.
789
800
If you're thinking about parsing a complex grammar with template strings, we
790
801
hope you'll find it useful.
791
802
@@ -1081,8 +1092,8 @@ and is able to ``await`` an interpolation's value.
1081
1092
1082
1093
See `afstring.py `__ and `test_afstring.py `__.
1083
1094
1084
- __ https://github.com/davepeck /pep750-examples/blob/main/pep/afstring.py
1085
- __ https://github.com/davepeck /pep750-examples/blob/main/pep/test_afstring.py
1095
+ __ https://github.com/t-strings /pep750-examples/blob/main/pep/afstring.py
1096
+ __ https://github.com/t-strings /pep750-examples/blob/main/pep/test_afstring.py
1086
1097
1087
1098
1088
1099
Approaches to Template Reuse
@@ -1157,16 +1168,16 @@ which supports the full grammar of format strings.
1157
1168
1158
1169
See `format.py `__ and `test_format.py `__.
1159
1170
1160
- __ https://github.com/davepeck /pep750-examples/blob/main/pep/format.py
1161
- __ https://github.com/davepeck /pep750-examples/blob/main/pep/test_format.py
1171
+ __ https://github.com/t-strings /pep750-examples/blob/main/pep/format.py
1172
+ __ https://github.com/t-strings /pep750-examples/blob/main/pep/test_format.py
1162
1173
1163
1174
1164
1175
Reference Implementation
1165
1176
========================
1166
1177
1167
1178
A CPython implementation of PEP 750 is `available <https://github.com/lysnikolaou/cpython/tree/tstrings >`_.
1168
1179
1169
- There is also a public repository of `examples and tests <https://github.com/davepeck /pep750-examples >`_
1180
+ There is also a public repository of `examples and tests <https://github.com/t-strings /pep750-examples >`_
1170
1181
built around the reference implementation. If you're interested in playing with
1171
1182
template strings, this repository is a great place to start.
1172
1183
0 commit comments