From 1f7d8fc518d9a0337f95222085586755fe6261c5 Mon Sep 17 00:00:00 2001 From: Dave Date: Thu, 1 May 2025 06:07:32 -0700 Subject: [PATCH 1/3] Remove support for Template/str addition. --- peps/pep-0750.rst | 51 +++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/peps/pep-0750.rst b/peps/pep-0750.rst index 92ec3c34cea..2b2dfc836c5 100644 --- a/peps/pep-0750.rst +++ b/peps/pep-0750.rst @@ -394,27 +394,33 @@ Template String Concatenation ----------------------------- Template strings support explicit concatenation using ``+``. Concatenation is -supported for two ``Template`` instances as well as for a ``Template`` instance -and a ``str``: +supported for two ``Template`` instances via ``Template.__add__()``: .. code-block:: python name = "World" - template = t"{name}" - assert isinstance(t"Hello " + template, Template) - assert (t"Hello " + template).strings == ("Hello ", "") - assert (t"Hello " + template).interpolations[0].value == "World" + assert isinstance(t"Hello " + t"{name}", Template) + assert (t"Hello " + t"{name}").strings == ("Hello ", "") + assert (t"Hello " + t"{name}").values[0] == "World" - assert isinstance("Hello " + template, Template) - assert ("Hello " + template).strings == ("Hello ", "") - assert ("Hello " + template).interpolations[0].value == "World" +Concatenation of a ``Template`` and a ``str`` is not supported. This is because +it is ambiguous whether the ``str`` should be treated as a static string part +or as an interpolation. Developers can effectively mark a ``str`` by directly +constructing a ``Template`` instance: -Concatenation of templates is "viral": the concatenation of a ``Template`` and -a ``str`` always results in a ``Template`` instance. +.. code-block:: python + + name = "World" + + # Treat `name` as a static string part + template = t"Hello " + Template(name) -Python's implicit concatenation syntax is also supported. The following code -will work as expected: + # Treat `name` as an interpolation + template = t"Hello " + Template(Interpolation(name, "name")) + +Python's implicit concatenation syntax is also supported, both between two +``Template`` instances and between a ``Template`` instance and a ``str``: .. code-block:: python @@ -422,10 +428,6 @@ will work as expected: assert (t"Hello " t"World").strings == ("Hello World",) assert ("Hello " t"World").strings == ("Hello World",) -The ``Template`` type supports the ``__add__()`` and ``__radd__()`` methods -between two ``Template`` instances and between a ``Template`` instance and a -``str``. - Template and Interpolation Equality ----------------------------------- @@ -1349,11 +1351,11 @@ Developers who need to obtain the original template string literal can always use ``inspect.getsource()`` or similar tools. -Disallowing String Concatenation --------------------------------- +Disallowing Template Concatenation +---------------------------------- -Earlier versions of this PEP proposed that template strings should not support -concatenation. This was rejected in favor of allowing concatenation. +Earlier versions of this PEP proposed that ``Template`` instances should not +support concatenation. This was rejected in favor of allowing concatenation. There are reasonable arguments in favor of rejecting one or all forms of concatenation: namely, that it cuts off a class of potential bugs, particularly @@ -1371,9 +1373,10 @@ harm caused by supporting it. (Developers concatenate f-strings all the time, after all, and while we are sure there are cases where this introduces bugs, it's not clear that those bugs outweigh the benefits of supporting concatenation.) -While concatenation is supported, we expect that code that uses template strings -will more commonly build up larger templates through nesting and composition -rather than concatenation. +While concatenation of two ``Templates`` is supported by this PEP, concatenation +via ``+`` of a ``Template`` and a ``str`` is not supported. We expect that +code that uses template strings will more commonly build up larger templates +through nesting and composition rather than concatenation. Arbitrary Conversion Values From 0a9a9e0944fa8df6e3ca1e25c76ae018def6310a Mon Sep 17 00:00:00 2001 From: Dave Date: Thu, 1 May 2025 09:07:33 -0700 Subject: [PATCH 2/3] Proposed update: restrict support for Template + str addition. --- peps/pep-0750.rst | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/peps/pep-0750.rst b/peps/pep-0750.rst index 2b2dfc836c5..8f638b1d1d0 100644 --- a/peps/pep-0750.rst +++ b/peps/pep-0750.rst @@ -419,13 +419,14 @@ constructing a ``Template`` instance: # Treat `name` as an interpolation template = t"Hello " + Template(Interpolation(name, "name")) -Python's implicit concatenation syntax is also supported, both between two -``Template`` instances and between a ``Template`` instance and a ``str``: +Python's implicit concatenation syntax is supported between any combination +of ``Template`` and ``str``: .. code-block:: python name = "World" assert (t"Hello " t"World").strings == ("Hello World",) + assert (t"Hello " "World").stings == ("Hello World",) assert ("Hello " t"World").strings == ("Hello World",) @@ -1355,7 +1356,8 @@ Disallowing Template Concatenation ---------------------------------- Earlier versions of this PEP proposed that ``Template`` instances should not -support concatenation. This was rejected in favor of allowing concatenation. +support concatenation. This was rejected in favor of allowing concatenating +multiple ``Template`` instances. There are reasonable arguments in favor of rejecting one or all forms of concatenation: namely, that it cuts off a class of potential bugs, particularly @@ -1369,14 +1371,16 @@ return a type that supported concatenation. In the end, we decided that the surprise to developers of a new string type *not* supporting concatenation was likely to be greater than the theoretical -harm caused by supporting it. (Developers concatenate f-strings all the time, -after all, and while we are sure there are cases where this introduces bugs, -it's not clear that those bugs outweigh the benefits of supporting concatenation.) +harm caused by supporting it. While concatenation of two ``Templates`` is supported by this PEP, concatenation -via ``+`` of a ``Template`` and a ``str`` is not supported. We expect that -code that uses template strings will more commonly build up larger templates -through nesting and composition rather than concatenation. +via ``+`` of a ``Template`` and a ``str`` is not supported. This is because it +is ambiguous whether ``str`` should be treated as a static string or an +interpolation. Developers must wrap the ``str`` in a ``Template`` instance before +concatenating it with another ``Template``, as described above. + +We expect that code that uses template strings will more commonly build up +larger templates through nesting and composition rather than concatenation. Arbitrary Conversion Values From 74cbb0ebea81abe716fb32494ccc514d86c3b74a Mon Sep 17 00:00:00 2001 From: Dave Date: Thu, 1 May 2025 13:01:06 -0700 Subject: [PATCH 3/3] stings -> strings (the typo, it stings!) --- peps/pep-0750.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/peps/pep-0750.rst b/peps/pep-0750.rst index 8f638b1d1d0..ea9f6a7bd2b 100644 --- a/peps/pep-0750.rst +++ b/peps/pep-0750.rst @@ -426,7 +426,7 @@ of ``Template`` and ``str``: name = "World" assert (t"Hello " t"World").strings == ("Hello World",) - assert (t"Hello " "World").stings == ("Hello World",) + assert (t"Hello " "World").strings == ("Hello World",) assert ("Hello " t"World").strings == ("Hello World",)