@@ -1344,31 +1344,28 @@ Alternate Layouts for ``Template``
1344
1344
-----------------------------------
1345
1345
1346
1346
During the development of this PEP, we considered several alternate layouts for
1347
- the contents of ``Templates ``. Many focused on a single ``args `` tuple that
1348
- contained both strings and interpolations. Variants included:
1349
-
1350
- - Instead of ``args ``, ``Template `` contains a ``strings `` attribute of type
1351
- ``tuple[str, ...] `` and an ``interpolations `` attribute of type
1352
- ``tuple[Interpolation, ...] ``. There are zero or more interpolations and
1353
- there is always one more string than there are interpolations. Utility code
1354
- could build an interleaved tuple of strings and interpolations from these
1355
- separate attributes. This was rejected as being overly complex.
1356
-
1357
- - ``args `` is typed as a ``Sequence[tuple[str, Interpolation | None]] ``. Each
1358
- static string is paired with is neighboring interpolation. The final
1359
- string part has no corresponding interpolation. This was rejected as being
1347
+ the ``Template `` type. Many focused on a single ``args `` tuple that contained
1348
+ both strings and interpolations. Variants included:
1349
+
1350
+ - ``args `` was a ``tuple[str | Interpolation, ...]` `` with the promise that
1351
+ its first and last items were strings and that strings and interpolations
1352
+ always alternated. This implied that ``args `` was always non-empty and that
1353
+ empty strings would be inserted between neighboring interpolations. This was
1354
+ rejected because alternation could not be captured by the type system and was
1355
+ not a guarantee we wished to make.
1356
+
1357
+ - ``args `` remained a ``tuple[str | Interpolation, ...] `` but did not support
1358
+ interleaving. As a result, empty strings were not added to the sequence. It
1359
+ was no longer possible to obtain static strings with ``args[::2] ``; instead,
1360
+ instance checks or structural pattern matching had to be used to distinguish
1361
+ between strings and interpolations. This approach was rejected as offering
1362
+ less future opportunity for performance optimization.
1363
+
1364
+ - ``args `` was typed as a ``Sequence[tuple[str, Interpolation | None]] ``. Each
1365
+ static string was paired with is neighboring interpolation. The final
1366
+ string part had no corresponding interpolation. This was rejected as being
1360
1367
overly complex.
1361
1368
1362
- - ``args `` remains a ``tuple[str | Interpolation, ...] `` but does not support
1363
- interleaving. As a result, empty strings are not added to the sequence. It is
1364
- no longer possible to obtain static strings with ``args[::2] ``; instead,
1365
- instance checks or structural pattern matching must be used to distinguish
1366
- between strings and interpolations. We believe this approach is easier to
1367
- explain and, at first glance, more intuitive. However, it was rejected as
1368
- offering less future opportunty for performance optimization. We also believe
1369
- that ``args[::2] `` may prove to be a useful shortcut in template processing
1370
- code.
1371
-
1372
1369
1373
1370
Mechanism to Describe the "Kind" of Template
1374
1371
--------------------------------------------
0 commit comments