@@ -404,30 +404,34 @@ supported for two ``Template`` instances via ``Template.__add__()``:
404404 assert (t" Hello " + t" {name} " ).strings == (" Hello " , " " )
405405 assert (t" Hello " + t" {name} " ).values[0 ] == " World"
406406
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:
407+ Implicit concatenation of two template string literals is also supported:
411408
412409.. code-block :: python
413410
414411 name = " World"
412+ assert isinstance (t" Hello " t" {name} " , Template)
413+ assert (t" Hello " t" {name} " ).strings == (" Hello " , " " )
414+ assert (t" Hello " t" {name} " ).values[0 ] == " World"
415415
416- # Treat `name` as a static string part
417- template = t" Hello " + Template(name)
418-
419- # Treat `name` as an interpolation
420- template = t" Hello " + Template(Interpolation(name, " name" ))
416+ Both implicit and explicit concatenation between ``Template `` and ``str `` is
417+ prohibited. This is because it is ambiguous whether the ``str `` should be
418+ treated as a static string part or as an interpolation.
421419
422- Python's implicit concatenation syntax is supported between any combination
423- of ``Template `` and ``str ``:
420+ To combine a ``Template `` and a ``str ``, developers must explicitly decide how
421+ to treat the ``str ``. If the ``str `` is intended to be a static string part,
422+ it should be wrapped in a ``Template ``. If the ``str `` is intended to be an
423+ interpolation value, it should be wrapped in an ``Interpolation `` and
424+ passed to the ``Template `` constructor. For example:
424425
425426.. code-block :: python
426427
427428 name = " World"
428- assert (t" Hello " t" World" ).strings == (" Hello World" ,)
429- assert (t" Hello " " World" ).strings == (" Hello World" ,)
430- assert (" Hello " t" World" ).strings == (" Hello World" ,)
429+
430+ # Treat `name` as a static string part
431+ template = t" Hello " + Template(name)
432+
433+ # Treat `name` as an interpolation
434+ template = t" Hello " + Template(Interpolation(name, " name" ))
431435
432436
433437 Template and Interpolation Equality
@@ -1374,10 +1378,10 @@ In the end, we decided that the surprise to developers of a new string type
13741378harm caused by supporting it.
13751379
13761380While concatenation of two ``Templates `` is supported by this PEP, 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+ of a ``Template `` and a ``str `` is not supported. This is because it is
1382+ ambiguous whether ``str `` should be treated as a static string or an
1383+ interpolation. Developers must wrap the ``str `` in a ``Template `` instance
1384+ before concatenating it with another ``Template ``, as described above.
13811385
13821386We expect that code that uses template strings will more commonly build up
13831387larger templates through nesting and composition rather than concatenation.
0 commit comments