Skip to content

Commit 0a9a9e0

Browse files
committed
Proposed update: restrict support for Template + str addition.
1 parent 1f7d8fc commit 0a9a9e0

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

peps/pep-0750.rst

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,14 @@ constructing a ``Template`` instance:
419419
# Treat `name` as an interpolation
420420
template = t"Hello " + Template(Interpolation(name, "name"))
421421
422-
Python's implicit concatenation syntax is also supported, both between two
423-
``Template`` instances and between a ``Template`` instance and a ``str``:
422+
Python's implicit concatenation syntax is supported between any combination
423+
of ``Template`` and ``str``:
424424

425425
.. code-block:: python
426426
427427
name = "World"
428428
assert (t"Hello " t"World").strings == ("Hello World",)
429+
assert (t"Hello " "World").stings == ("Hello World",)
429430
assert ("Hello " t"World").strings == ("Hello World",)
430431
431432
@@ -1355,7 +1356,8 @@ Disallowing Template Concatenation
13551356
----------------------------------
13561357

13571358
Earlier versions of this PEP proposed that ``Template`` instances should not
1358-
support concatenation. This was rejected in favor of allowing concatenation.
1359+
support concatenation. This was rejected in favor of allowing concatenating
1360+
multiple ``Template`` instances.
13591361

13601362
There are reasonable arguments in favor of rejecting one or all forms of
13611363
concatenation: namely, that it cuts off a class of potential bugs, particularly
@@ -1369,14 +1371,16 @@ return a type that supported concatenation.
13691371

13701372
In the end, we decided that the surprise to developers of a new string type
13711373
*not* supporting concatenation was likely to be greater than the theoretical
1372-
harm caused by supporting it. (Developers concatenate f-strings all the time,
1373-
after all, and while we are sure there are cases where this introduces bugs,
1374-
it's not clear that those bugs outweigh the benefits of supporting concatenation.)
1374+
harm caused by supporting it.
13751375

13761376
While concatenation of two ``Templates`` is supported by this PEP, concatenation
1377-
via ``+`` of a ``Template`` and a ``str`` is not supported. We expect that
1378-
code that uses template strings will more commonly build up larger templates
1379-
through nesting and composition rather than concatenation.
1377+
via ``+`` of a ``Template`` and a ``str`` is not supported. This is because it
1378+
is ambiguous whether ``str`` should be treated as a static string or an
1379+
interpolation. Developers must wrap the ``str`` in a ``Template`` instance before
1380+
concatenating it with another ``Template``, as described above.
1381+
1382+
We expect that code that uses template strings will more commonly build up
1383+
larger templates through nesting and composition rather than concatenation.
13801384

13811385

13821386
Arbitrary Conversion Values

0 commit comments

Comments
 (0)