@@ -213,7 +213,47 @@ This PEP leaves up to the implementation the level of f-string nesting allowed.
213
213
This means that limiting nesting is **not part of the language specification **
214
214
but also the language specification **doesn't mandate arbitrary nesting **.
215
215
216
- Three new tokens are introduced:
216
+ Handling of f-string debug expressions
217
+ --------------------------------------
218
+
219
+ Since Python 3.8, f-strings can be used to debug expressions by using the
220
+ ``= `` operator. For example::
221
+
222
+ >>> a = 1
223
+ >>> f"{1+1=}"
224
+ '1+1=2'
225
+
226
+ This semantics were not introduced formally in a PEP and they were implemented
227
+ in the current string parser as a special case in `bpo-36817
228
+ <https://bugs.python.org/issue?@action=redirect&bpo=36817> `_ and documented in
229
+ `the f-string lexical analysis section
230
+ <https://docs.python.org/3/reference/lexical_analysis.html#f-strings> `_.
231
+
232
+ This feature is not affected by the changes proposed in this PEP but is
233
+ important to specify that the formal handling of this feature requires the lexer
234
+ to be able to "untokenize" the expression part of the f-string. This is not a
235
+ problem for the current string parser as it can operate directly on the string
236
+ token contents. However, incorporating this feature into a given parser
237
+ implementation requires the lexer to keep track of the raw string contents of
238
+ the expression part of the f-string and make them available to the parser when
239
+ the parse tree is constructed for f-string nodes. A pure "untokenization" is not
240
+ enough because as specified currently, f-string debugging preserve whitespace,
241
+ including spaces after the ``{ `` and the ``= `` characters. This means that the
242
+ raw string contents of the expression part of the f-string must be kept intact
243
+ and not just the associated tokens.
244
+
245
+ How parser/lexer implementations deal with this problem is of course up to the
246
+ implementation.
247
+
248
+ New tokens
249
+ ----------
250
+
251
+ Three new tokens are introduced: ``FSTRING_START ``, ``FSTRING_MIDDLE `` and
252
+ ``FSTRING_END ``. This PEP does not mandate the precise definitions of these tokens
253
+ as different lexers may have different implementations that may be more efficient
254
+ than the ones proposed here given the context of the particular implementation. However,
255
+ the following definitions are provided as a reference so that the reader can have a
256
+ better understanding of the proposed grammar changes and how the tokens are used:
217
257
218
258
* ``FSTRING_START ``: This token includes f-string character (``f ``/``F ``) and the open quote(s).
219
259
* ``FSTRING_MIDDLE ``: This token includes the text between the opening quote
@@ -254,6 +294,9 @@ while ``f"""some words"""`` will be tokenized simply as::
254
294
FSTRING_START - 'f"""'
255
295
FSTRING_END - 'some words'
256
296
297
+ Consequences of the new grammar
298
+ -------------------------------
299
+
257
300
All restrictions mentioned in the PEP are lifted from f-literals, as explained below:
258
301
259
302
* Expression portions may now contain strings delimited with the same kind of
@@ -291,7 +334,7 @@ limited to be different from the quotes of the enclosing string, because this is
291
334
now allowed: as an arbitrary Python string can contain any possible choice of
292
335
quotes, so can any f-string expression. Additionally there is no need to clarify
293
336
that certain things are not allowed in the expression part because of
294
- implementation restructions such as comments, new line characters or
337
+ implementation restrictions such as comments, new line characters or
295
338
backslashes.
296
339
297
340
The only "surprising" difference is that as f-strings allow specifying a
0 commit comments