Skip to content

Commit

Permalink
PEP 750: Small update to 'alternate layouts' section (#4210)
Browse files Browse the repository at this point in the history
  • Loading branch information
davepeck authored Jan 19, 2025
1 parent ec5833e commit 7cd93f5
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions peps/pep-0750.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1344,31 +1344,28 @@ Alternate Layouts for ``Template``
-----------------------------------

During the development of this PEP, we considered several alternate layouts for
the contents of ``Templates``. Many focused on a single ``args`` tuple that
contained both strings and interpolations. Variants included:

- Instead of ``args``, ``Template`` contains a ``strings`` attribute of type
``tuple[str, ...]`` and an ``interpolations`` attribute of type
``tuple[Interpolation, ...]``. There are zero or more interpolations and
there is always one more string than there are interpolations. Utility code
could build an interleaved tuple of strings and interpolations from these
separate attributes. This was rejected as being overly complex.

- ``args`` is typed as a ``Sequence[tuple[str, Interpolation | None]]``. Each
static string is paired with is neighboring interpolation. The final
string part has no corresponding interpolation. This was rejected as being
the ``Template`` type. Many focused on a single ``args`` tuple that contained
both strings and interpolations. Variants included:

- ``args`` was a ``tuple[str | Interpolation, ...]``` with the promise that
its first and last items were strings and that strings and interpolations
always alternated. This implied that ``args`` was always non-empty and that
empty strings would be inserted between neighboring interpolations. This was
rejected because alternation could not be captured by the type system and was
not a guarantee we wished to make.

- ``args`` remained a ``tuple[str | Interpolation, ...]`` but did not support
interleaving. As a result, empty strings were not added to the sequence. It
was no longer possible to obtain static strings with ``args[::2]``; instead,
instance checks or structural pattern matching had to be used to distinguish
between strings and interpolations. This approach was rejected as offering
less future opportunity for performance optimization.

- ``args`` was typed as a ``Sequence[tuple[str, Interpolation | None]]``. Each
static string was paired with is neighboring interpolation. The final
string part had no corresponding interpolation. This was rejected as being
overly complex.

- ``args`` remains a ``tuple[str | Interpolation, ...]`` but does not support
interleaving. As a result, empty strings are not added to the sequence. It is
no longer possible to obtain static strings with ``args[::2]``; instead,
instance checks or structural pattern matching must be used to distinguish
between strings and interpolations. We believe this approach is easier to
explain and, at first glance, more intuitive. However, it was rejected as
offering less future opportunty for performance optimization. We also believe
that ``args[::2]`` may prove to be a useful shortcut in template processing
code.


Mechanism to Describe the "Kind" of Template
--------------------------------------------
Expand Down

0 comments on commit 7cd93f5

Please sign in to comment.