Skip to content

Commit dbc179e

Browse files
miss-islingtonpablogsalmgmacias95
authored
[3.12] Clarify that error messages are better with PEP 701 (GH-105150) (#105169)
Co-authored-by: Pablo Galindo Salgado <[email protected]> Co-authored-by: Marta Gómez Macías <[email protected]>
1 parent 83fc562 commit dbc179e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Doc/whatsnew/3.12.rst

+25
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,31 @@ same quote as the containing f-string. Let's cover these in detail:
203203

204204
See :pep:`701` for more details.
205205

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+
206231
(Contributed by Pablo Galindo, Batuhan Taskaya, Lysandros Nikolaou, Cristián
207232
Maureira-Fredes and Marta Gómez in :gh:`102856`. PEP written by Pablo Galindo,
208233
Batuhan Taskaya, Lysandros Nikolaou and Marta Gómez).

0 commit comments

Comments
 (0)