@@ -394,38 +394,40 @@ Template String Concatenation
394394-----------------------------
395395
396396Template strings support explicit concatenation using ``+ ``. Concatenation is
397- supported for two ``Template `` instances as well as for a ``Template `` instance
398- and a ``str ``:
397+ supported for two ``Template `` instances via ``Template.__add__() ``:
399398
400399.. code-block :: python
401400
402401 name = " World"
403- template = t" {name} "
404402
405- assert isinstance (t" Hello " + template , Template)
406- assert (t" Hello " + template ).strings == (" Hello " , " " )
407- assert (t" Hello " + template).interpolations [0 ].value == " World"
403+ assert isinstance (t" Hello " + t " {name} " , Template)
404+ assert (t" Hello " + t " {name} " ).strings == (" Hello " , " " )
405+ assert (t" Hello " + t " {name} " ).values [0 ] == " World"
408406
409- assert isinstance (" Hello " + template, Template)
410- assert (" Hello " + template).strings == (" Hello " , " " )
411- assert (" Hello " + template).interpolations[0 ].value == " World"
407+ Concatenation of a ``Template `` and a ``str `` is not supported. This is because
408+ it is ambiguous whether the ``str `` should be treated as a static string part
409+ or as an interpolation. Developers can effectively mark a ``str `` by directly
410+ constructing a ``Template `` instance:
412411
413- Concatenation of templates is "viral": the concatenation of a ``Template `` and
414- a ``str `` always results in a ``Template `` instance.
412+ .. code-block :: python
413+
414+ name = " World"
415+
416+ # Treat `name` as a static string part
417+ template = t" Hello " + Template(name)
415418
416- Python's implicit concatenation syntax is also supported. The following code
417- will work as expected:
419+ # Treat `name` as an interpolation
420+ template = t" Hello " + Template(Interpolation(name, " name" ))
421+
422+ Python's implicit concatenation syntax is also supported, both between two
423+ ``Template `` instances and between a ``Template `` instance and a ``str ``:
418424
419425.. code-block :: python
420426
421427 name = " World"
422428 assert (t" Hello " t" World" ).strings == (" Hello World" ,)
423429 assert (" Hello " t" World" ).strings == (" Hello World" ,)
424430
425- The ``Template `` type supports the ``__add__() `` and ``__radd__() `` methods
426- between two ``Template `` instances and between a ``Template `` instance and a
427- ``str ``.
428-
429431
430432 Template and Interpolation Equality
431433-----------------------------------
@@ -1349,11 +1351,11 @@ Developers who need to obtain the original template string literal can always
13491351use ``inspect.getsource() `` or similar tools.
13501352
13511353
1352- Disallowing String Concatenation
1353- --------------------------------
1354+ Disallowing Template Concatenation
1355+ ----------------------------------
13541356
1355- Earlier versions of this PEP proposed that template strings should not support
1356- concatenation. This was rejected in favor of allowing concatenation.
1357+ Earlier versions of this PEP proposed that `` Template `` instances should not
1358+ support concatenation. This was rejected in favor of allowing concatenation.
13571359
13581360There are reasonable arguments in favor of rejecting one or all forms of
13591361concatenation: 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,
13711373after all, and while we are sure there are cases where this introduces bugs,
13721374it's not clear that those bugs outweigh the benefits of supporting concatenation.)
13731375
1374- While concatenation is supported, we expect that code that uses template strings
1375- will more commonly build up larger templates through nesting and composition
1376- rather than concatenation.
1376+ 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.
13771380
13781381
13791382Arbitrary Conversion Values
0 commit comments