Skip to content

Commit

Permalink
Clarify that error messages are better with PEP 701 (pythonGH-105150)
Browse files Browse the repository at this point in the history
(cherry picked from commit 60cfc6d)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Co-authored-by: Marta Gómez Macías <mgmacias@google.com>
  • Loading branch information
2 people authored and miss-islington committed May 31, 2023
1 parent e7cb216 commit f623580
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,31 @@ same quote as the containing f-string. Let's cover these in detail:

See :pep:`701` for more details.

As a positive side-effect of how this feature has been implemented (by parsing f-strings
with the PEG parser (see :pep:`617`), now error messages for f-strings are more precise
and include the exact location of the error. For example, in Python 3.11, the following
f-string raises a :exc:`SyntaxError`:

.. code-block:: python
>>> my_string = f"{x z y}" + f"{1 + 1}"
File "<stdin>", line 1
(x z y)
^^^
SyntaxError: f-string: invalid syntax. Perhaps you forgot a comma?
but the error message doesn't include the exact location of the error withing the line and
also has the expression artificially surrounded by parentheses. In Python 3.12, as f-strings
are parsed with the PEG parser, error messages can be more precise and show the entire line:

.. code-block:: python
>>> my_string = f"{x z y}" + f"{1 + 1}"
File "<stdin>", line 1
my_string = f"{x z y}" + f"{1 + 1}"
^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?
(Contributed by Pablo Galindo, Batuhan Taskaya, Lysandros Nikolaou, Cristián
Maureira-Fredes and Marta Gómez in :gh:`102856`. PEP written by Pablo Galindo,
Batuhan Taskaya, Lysandros Nikolaou and Marta Gómez).
Expand Down

0 comments on commit f623580

Please sign in to comment.