Skip to content

Commit f2e5ca4

Browse files
committed
Document the new AST nodes (TemplateStr and Interpolation)
1 parent 9a0a301 commit f2e5ca4

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Doc/library/ast.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,49 @@ Literals
324324
values=[
325325
Constant(value='.3')]))]))
326326

327+
.. class:: TemplateStr(values)
328+
329+
A t-string, comprising a series of :class:`Interpolation` and :class:`Constant`
330+
nodes.
331+
332+
.. doctest::
333+
334+
>>> print(ast.dump(ast.parse('t"{name} finished {place:ordinal}"', mode='eval'), indent=4))
335+
Expression(
336+
body=TemplateStr(
337+
values=[
338+
Interpolation(
339+
value=Name(id='name'),
340+
str='name',
341+
conversion=-1),
342+
Constant(value=' finished '),
343+
Interpolation(
344+
value=Name(id='place'),
345+
str='place',
346+
conversion=-1,
347+
format_spec=JoinedStr(
348+
values=[
349+
Constant(value='ordinal')]))]))
350+
351+
352+
.. class:: Interpolation(value, str, int, format_spec)
353+
354+
Node representing a single interpolation field in a t-string.
355+
356+
* ``value`` is any expression node (such as a literal, a variable, or a
357+
function call).
358+
* ``str`` is a constant containing the text of the interpolation expression.
359+
* ``conversion`` is an integer:
360+
361+
* -1: no conversion
362+
* 115: ``!s`` string conversion
363+
* 114: ``!r`` repr conversion
364+
* 97: ``!a`` ascii conversion
365+
366+
* ``format_spec`` is a :class:`JoinedStr` node representing the formatting
367+
of the value, or ``None`` if no format was specified. Both
368+
``conversion`` and ``format_spec`` can be set at the same time.
369+
327370

328371
.. class:: List(elts, ctx)
329372
Tuple(elts, ctx)

0 commit comments

Comments
 (0)