Skip to content

Commit 5cb89cd

Browse files
committed
Review feedback
1 parent a35f0ee commit 5cb89cd

File tree

3 files changed

+70
-63
lines changed

3 files changed

+70
-63
lines changed

Doc/library/ast.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,17 +361,20 @@ Literals
361361

362362
* ``value`` is any expression node (such as a literal, a variable, or a
363363
function call).
364+
This has the same meaning as ``FormattedValue.value``.
364365
* ``str`` is a constant containing the text of the interpolation expression.
365366
* ``conversion`` is an integer:
366367

367368
* -1: no conversion
368-
* 97: ``!a`` :func:`ASCII <ascii>` conversion (``ord('a')``)
369-
* 114: ``!r`` :func:`repr` conversion (``ord('r')``)
370-
* 115: ``!s`` :func:`string <str>` conversion (``ord('s')``)
369+
* 97 (``ord('a')``): ``!a`` :func:`ASCII <ascii>` conversion
370+
* 114 (``ord('r')``): ``!r`` :func:`repr` conversion
371+
* 115 (``ord('s')``): ``!s`` :func:`string <str>` conversion
371372

373+
This has the same meaning as ``FormattedValue.conversion``.
372374
* ``format_spec`` is a :class:`JoinedStr` node representing the formatting
373375
of the value, or ``None`` if no format was specified. Both
374376
``conversion`` and ``format_spec`` can be set at the same time.
377+
This has the same meaning as ``FormattedValue.format_spec``.
375378

376379

377380
.. class:: List(elts, ctx)

Doc/library/string.templatelib.rst

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ To write a t-string, use a ``'t'`` prefix instead of an ``'f'``, like so:
3434
>>> pi = 3.14
3535
>>> t't-strings are new in Python {pi!s}!'
3636
Template(
37-
strings=('t-strings are new in Python ', '!'),
37+
strings=('t-strings are new in Python ', '.'),
3838
interpolations=(Interpolation(3.14, 'pi', 's', ''),)
3939
)
4040
@@ -51,32 +51,32 @@ Types
5151
This syntax is identical to that of :ref:`f-strings <f-strings>`,
5252
except that it uses a ``t`` prefix in place of an ``f``:
5353

54-
>>> name = 'World'
55-
>>> template = t'Hello, {name}!'
54+
>>> cheese = 'Red Leicester'
55+
>>> template = t"We're fresh out of {cheese}, sir."
5656
>>> type(template)
5757
<class 'string.templatelib.Template'>
5858

5959
Templates are stored as sequences of literal :attr:`~Template.strings`
6060
and dynamic :attr:`~Template.interpolations`.
6161
A :attr:`~Template.values` attribute holds the values of the interpolations:
6262

63-
>>> name = 'World'
64-
>>> template = t'Hello, {name}!'
63+
>>> cheese = 'Camembert'
64+
>>> template = t'Ah! We do have {cheese}.'
6565
>>> template.strings
66-
('Hello, ', '!')
66+
('Ah! We do have ', '.')
6767
>>> template.interpolations
68-
(Interpolation('World', ...),)
68+
(Interpolation('Camembert', ...),)
6969
>>> template.values
70-
('World',)
70+
('Camembert',)
7171

7272
The :attr:`!strings` tuple has one more element than :attr:`!interpolations`
7373
and :attr:`!values`; the interpolations “belong” between the strings.
7474
This may be easier to understand when tuples are aligned
7575

7676
.. code-block:: python
7777
78-
template.strings: ('Hello, ', '!')
79-
template.values: ( 'World', )
78+
template.strings: ('Ah! We do have ', '.')
79+
template.values: ( 'Camembert', )
8080
8181
.. rubric:: Attributes
8282

@@ -85,17 +85,18 @@ Types
8585

8686
A :class:`tuple` of the static strings in the template.
8787

88-
>>> name = 'World'
89-
>>> template = t'Hello, {name}!'
88+
>>> cheese = 'Camembert'
89+
>>> template = t'Ah! We do have {cheese}.'
9090
>>> template.strings
91-
('Hello, ', '!')
91+
('Ah! We do have ', '.')
9292

9393
Empty strings *are* included in the tuple:
9494

95-
>>> name = 'World'
96-
>>> template = t'Hello, {name}{name}!'
95+
>>> response = 'We do have '
96+
>>> cheese = 'Camembert'
97+
>>> template = t'Ah! {response}{cheese}.'
9798
>>> template.strings
98-
('Hello, ', '', '!')
99+
('Ah! ', '', '.')
99100

100101
The ``strings`` tuple is never empty, and always contains one more
101102
string than the ``interpolations`` and ``values`` tuples:
@@ -114,26 +115,26 @@ Types
114115

115116
A :class:`tuple` of the interpolations in the template.
116117

117-
>>> name = 'World'
118-
>>> template = t'Hello, {name}!'
118+
>>> cheese = 'Camembert'
119+
>>> template = t'Ah! We do have {cheese}.'
119120
>>> template.interpolations
120-
(Interpolation('World', 'name', None, ''),)
121+
(Interpolation('Camembert', 'cheese', None, ''),)
121122

122123
The ``interpolations`` tuple may be empty and always contains one fewer
123124
values than the ``strings`` tuple:
124125

125-
>>> t'Hello!'.interpolations
126+
>>> t'Red Leicester'.interpolations
126127
()
127128

128129
.. attribute:: values
129130
:type: tuple[object, ...]
130131

131132
A tuple of all interpolated values in the template.
132133

133-
>>> name = 'World'
134-
>>> template = t'Hello, {name}!'
134+
>>> cheese = 'Camembert'
135+
>>> template = t'Ah! We do have {cheese}.'
135136
>>> template.values
136-
('World',)
137+
('Camembert',)
137138

138139
The ``values`` tuple always has the same length as the
139140
``interpolations`` tuple. It is always equivalent to
@@ -147,19 +148,21 @@ Types
147148
it is also possible to create them directly using the constructor:
148149

149150
>>> from string.templatelib import Interpolation, Template
150-
>>> name = 'World'
151-
>>> template = Template('Hello, ', Interpolation(name, 'name'), '!')
151+
>>> cheese = 'Camembert'
152+
>>> template = Template(
153+
... 'Ah! We do have ', Interpolation(cheese, 'cheese'), '.'
154+
... )
152155
>>> list(template)
153-
['Hello, ', Interpolation('World', 'name', None, ''), '!']
156+
['Ah! We do have ', Interpolation('Camembert', 'cheese', None, ''), '.']
154157

155158
If multiple strings are passed consecutively, they will be concatenated
156159
into a single value in the :attr:`~Template.strings` attribute. For example,
157160
the following code creates a :class:`Template` with a single final string:
158161

159162
>>> from string.templatelib import Template
160-
>>> template = Template('Hello, ', 'World', '!')
163+
>>> template = Template('Ah! We do have ', 'Camembert', '.')
161164
>>> template.strings
162-
('Hello, World!',)
165+
('Ah! We do have Camembert.',)
163166

164167
If multiple interpolations are passed consecutively, they will be treated
165168
as separate interpolations and an empty string will be inserted between them.
@@ -168,8 +171,8 @@ Types
168171

169172
>>> from string.templatelib import Interpolation, Template
170173
>>> template = Template(
171-
... Interpolation('World', 'name'),
172-
... Interpolation('!', 'punctuation'),
174+
... Interpolation('Camembert', 'cheese'),
175+
... Interpolation('.', 'punctuation'),
173176
... )
174177
>>> template.strings
175178
('', '', '')
@@ -179,30 +182,31 @@ Types
179182
Iterate over the template, yielding each non-empty string and
180183
:class:`Interpolation` in the correct order:
181184

182-
>>> name = 'World'
183-
>>> list(t'Hello, {name}!')
184-
['Hello, ', Interpolation('World', 'name', None, ''), '!']
185+
>>> cheese = 'Camembert'
186+
>>> list(t'Ah! We do have {cheese}.')
187+
['Ah! We do have ', Interpolation('Camembert', 'cheese', None, ''), '.']
185188

186189
.. caution::
187190

188191
Empty strings are **not** included in the iteration:
189192

190-
>>> name = 'World'
191-
>>> list(t'Hello, {name}{name}!') # doctest: +NORMALIZE_WHITESPACE
192-
['Hello, ',
193-
Interpolation('World', 'name', None, ''),
194-
Interpolation('World', 'name', None, ''),
195-
'!']
193+
>>> response = 'We do have '
194+
>>> cheese = 'Camembert'
195+
>>> list(t'Ah! {response}{cheese}.') # doctest: +NORMALIZE_WHITESPACE
196+
['Ah! ',
197+
Interpolation('We do have ', 'response', None, ''),
198+
Interpolation('Camembert', 'cheese', None, ''),
199+
'.']
196200

197201
.. describe:: template + other
198202
template += other
199203

200204
Concatenate this template with another, returning a new
201205
:class:`!Template` instance:
202206

203-
>>> name = 'World'
204-
>>> list(t'Hello ' + t'there {name}!')
205-
['Hello there ', Interpolation('World', 'name', None, ''), '!']
207+
>>> cheese = 'Camembert'
208+
>>> list(t'Ah! ' + t'We do have {cheese}.')
209+
['Ah! We do have ', Interpolation('Camembert', 'cheese', None, ''), '.']
206210

207211
Concatenating a :class:`!Template` and a ``str`` is **not** supported.
208212
This is because it is unclear whether the string should be treated as
@@ -212,15 +216,15 @@ Types
212216
(to treat it as a static string)
213217
or use an :class:`!Interpolation` (to treat it as dynamic):
214218

215-
>>> from string.templatelib import Template, Interpolation
216-
>>> template = t'Hello '
217-
>>> # Treat 'there ' as a static string
218-
>>> template += Template('there ')
219-
>>> # Treat name as an interpolation
220-
>>> name = 'World'
221-
>>> template += Template(Interpolation(name, 'name'))
219+
>>> from string.templatelib import Interpolation, Template
220+
>>> template = t'Ah! '
221+
>>> # Treat 'We do have ' as a static string
222+
>>> template += Template('We do have ')
223+
>>> # Treat cheese as an interpolation
224+
>>> cheese = 'Camembert'
225+
>>> template += Template(Interpolation(cheese, 'cheese'))
222226
>>> list(template)
223-
['Hello there ', Interpolation('World', 'name', None, '')]
227+
['Ah! We do have ', Interpolation('Camembert', 'cheese', None, '')]
224228

225229

226230
.. class:: Interpolation

Doc/reference/lexical_analysis.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,9 @@ The allowed prefixes are:
599599

600600
See the linked sections for details on each type.
601601

602-
Prefixes are case-insensitive (for example, ``B`` works the same as ``b``).
603-
The ``r`` prefix can be combined with ``f``, ``t`` or ``b``, so ``fr``,
604-
``rf``, ``tr``, ``rt``, ``br`` and ``rb`` are also valid prefixes.
602+
Prefixes are case-insensitive (for example, '``B``' works the same as '``b``').
603+
The '``r``' prefix can be combined with '``f``', '``t``' or '``b``', so '``fr``',
604+
'``rf``', '``tr``', '``rt``', '``br``', and '``rb``' are also valid prefixes.
605605

606606
.. versionadded:: 3.3
607607
The ``'rb'`` prefix of raw bytes literals has been added as a synonym
@@ -661,7 +661,7 @@ quote.
661661
Escape sequences
662662
----------------
663663

664-
Unless an ``'r'`` or ``'R'`` prefix is present, escape sequences in string and
664+
Unless an '``r``' or '``R``' prefix is present, escape sequences in string and
665665
bytes literals are interpreted according to rules similar to those used by
666666
Standard C. The recognized escape sequences are:
667667

@@ -852,7 +852,7 @@ unrecognized escapes.
852852
Bytes literals
853853
--------------
854854

855-
:dfn:`Bytes literals` are always prefixed with ``'b'`` or ``'B'``; they produce an
855+
:dfn:`Bytes literals` are always prefixed with '``b``' or '``B``'; they produce an
856856
instance of the :class:`bytes` type instead of the :class:`str` type.
857857
They may only contain ASCII characters; bytes with a numeric value of 128
858858
or greater must be expressed with escape sequences (typically
@@ -878,8 +878,8 @@ Similarly, a zero byte must be expressed using an escape sequence (typically
878878
Raw string literals
879879
-------------------
880880

881-
Both string and bytes literals may optionally be prefixed with a letter ``'r'``
882-
or ``'R'``; such constructs are called :dfn:`raw string literals`
881+
Both string and bytes literals may optionally be prefixed with a letter '``r``'
882+
or '``R``'; such constructs are called :dfn:`raw string literals`
883883
and :dfn:`raw bytes literals` respectively and treat backslashes as
884884
literal characters.
885885
As a result, in raw string literals, :ref:`escape sequences <escape-sequences>`
@@ -923,7 +923,7 @@ f-strings
923923
.. versionadded:: 3.6
924924

925925
A :dfn:`formatted string literal` or :dfn:`f-string` is a string literal
926-
that is prefixed with ``'f'`` or ``'F'``. These strings may contain
926+
that is prefixed with '``f``' or '``F``'. These strings may contain
927927
replacement fields, which are expressions delimited by curly braces ``{}``.
928928
While other string literals always have a constant value, formatted strings
929929
are really expressions evaluated at run time.
@@ -1089,7 +1089,7 @@ t-strings
10891089
.. versionadded:: 3.14
10901090

10911091
A :dfn:`template string literal` or :dfn:`t-string` is a string literal
1092-
that is prefixed with ``'t'`` or ``'T'``.
1092+
that is prefixed with '``t``' or '``T``'.
10931093
These strings follow the same syntax and evaluation rules as
10941094
:ref:`formatted string literals <f-strings>`, with the following differences:
10951095

@@ -1117,7 +1117,7 @@ These strings follow the same syntax and evaluation rules as
11171117
This includes the equals sign and any surrounding whitespace.
11181118
The :class:`!Interpolation` instance for the expression will be created as
11191119
normal, except that :attr:`~string.templatelib.Interpolation.conversion` will
1120-
be set to ``'r'`` (:func:`repr`) by default.
1120+
be set to '``r``' (:func:`repr`) by default.
11211121
If an explicit conversion or format specifier are provided,
11221122
this will override the default behaviour.
11231123

0 commit comments

Comments
 (0)