File tree 1 file changed +25
-0
lines changed
1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -203,6 +203,31 @@ same quote as the containing f-string. Let's cover these in detail:
203
203
204
204
See :pep: `701 ` for more details.
205
205
206
+ As a positive side-effect of how this feature has been implemented (by parsing f-strings
207
+ with the PEG parser (see :pep: `617 `), now error messages for f-strings are more precise
208
+ and include the exact location of the error. For example, in Python 3.11, the following
209
+ f-string raises a :exc: `SyntaxError `:
210
+
211
+ .. code-block :: python
212
+
213
+ >> > my_string = f " { x z y} " + f " { 1 + 1 } "
214
+ File " <stdin>" , line 1
215
+ (x z y)
216
+ ^^^
217
+ SyntaxError : f- string: invalid syntax. Perhaps you forgot a comma?
218
+
219
+ but the error message doesn't include the exact location of the error withing the line and
220
+ also has the expression artificially surrounded by parentheses. In Python 3.12, as f-strings
221
+ are parsed with the PEG parser, error messages can be more precise and show the entire line:
222
+
223
+ .. code-block :: python
224
+
225
+ >> > my_string = f " { x z y} " + f " { 1 + 1 } "
226
+ File " <stdin>" , line 1
227
+ my_string = f " { x z y} " + f " { 1 + 1 } "
228
+ ^^^
229
+ SyntaxError : invalid syntax. Perhaps you forgot a comma?
230
+
206
231
(Contributed by Pablo Galindo, Batuhan Taskaya, Lysandros Nikolaou, Cristián
207
232
Maureira-Fredes and Marta Gómez in :gh: `102856 `. PEP written by Pablo Galindo,
208
233
Batuhan Taskaya, Lysandros Nikolaou and Marta Gómez).
You can’t perform that action at this time.
0 commit comments