From 7cd93f5957c4de12dc3755b37975698cea7f49e3 Mon Sep 17 00:00:00 2001 From: Dave Peck Date: Sun, 19 Jan 2025 10:37:30 -0800 Subject: [PATCH] PEP 750: Small update to 'alternate layouts' section (#4210) --- peps/pep-0750.rst | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/peps/pep-0750.rst b/peps/pep-0750.rst index 8a1634a6315..1ad265c226d 100644 --- a/peps/pep-0750.rst +++ b/peps/pep-0750.rst @@ -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 --------------------------------------------