@@ -1288,27 +1288,49 @@ Enable Full Reconstruction of Original Template Literal
12881288
12891289Earlier versions of this PEP attempted to make it possible to fully reconstruct
12901290the text of the original template string from a ``Template `` instance. This was
1291- rejected as being overly complex.
1291+ rejected as being overly complex. The mapping between template literal source
1292+ and the underlying AST is not one-to-one and there are several limitations with
1293+ respect to round-tripping to the original source text.
12921294
1293- There are several limitations with respect to round-tripping to the original
1294- source text:
1295+ First, ``Interpolation.format_spec `` defaults to ``"" `` if not provided:
12951296
1296- - ``Interpolation.format_spec `` defaults to ``"" `` if not provided. It is therefore
1297- impossible to distinguish ``t"{expression}" `` from ``t"{expression:}" ``.
1298- - The debug specifier, ``= ``, is treated as a special case. It is therefore not
1299- possible to distinguish ``t"{expression=}" `` from
1300- ``t"expression={expression}" ``.
1301- - Finally, format specifiers in f-strings allow arbitrary nesting. In this PEP
1302- and in the reference implementation, the specifier is eagerly evaluated
1303- to set the ``format_spec `` in the ``Interpolation ``, thereby losing
1304- the original expressions. For example:
1297+ .. code-block :: python
1298+
1299+ value = 42
1300+ template1 = t" {value} "
1301+ template2 = t" {value: } "
1302+ assert template1.interpolations[0 ].format_spec == " "
1303+ assert template2.interpolations[0 ].format_spec == " "
1304+
1305+ Next, the debug specifier, ``= ``, is treated as a special case and is processed
1306+ before the AST is created. It is therefore not possible to distinguish
1307+ ``t"{expression=}" `` from ``t"expression={expression!r}" ``:
1308+
1309+ .. code-block :: python
1310+
1311+ value = 42
1312+ template1 = t" {value=}"
1313+ template2 = t" value={value!r } "
1314+ assert template1.strings[0 ] == " value="
1315+ assert template1.interpolations[0 ].expression == " value"
1316+ assert template1.interpolations[0 ].conversion == " r"
1317+ assert template2.strings[0 ] == " value="
1318+ assert template2.interpolations[0 ].expression == " value"
1319+ assert template2.interpolations[0 ].conversion == " r"
1320+
1321+ Finally, format specifiers in f-strings allow arbitrary nesting. In this PEP
1322+ and in the reference implementation, the specifier is eagerly evaluated to
1323+ set the ``format_spec `` in the ``Interpolation ``, thereby losing the original
1324+ expressions. For example:
13051325
13061326.. code-block :: python
13071327
13081328 value = 42
13091329 precision = 2
1310- template = t" Value: {value: .{precision}f} "
1311- assert template.interpolations[0 ].format_spec == " .2f"
1330+ template1 = t" {value:.2f } "
1331+ template2 = t" {value: .{precision}f} "
1332+ assert template1.interpolations[0 ].format_spec == " .2f"
1333+ assert template2.interpolations[0 ].format_spec == " .2f"
13121334
13131335 We do not anticipate that these limitations will be a significant issue in practice.
13141336Developers who need to obtain the original template string literal can always
0 commit comments