Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEP 750: Small update to 'alternate layouts' section #4210

Merged
merged 2 commits into from
Jan 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading