Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify that error messages are better with PEP 701 #105150

Merged
merged 6 commits into from
May 31, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,27 @@ 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`:

>>> my_string = f"{x $ y}" + f"{1 + 1}"
...
(x $ y)
^
SyntaxError: f-string: invalid syntax

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, so error messages can be more precise and show the entire line:
pablogsal marked this conversation as resolved.
Show resolved Hide resolved

>>> my_string = f"{x $ y}" + f"{1 + 1}"
...
my_string = f"{x $ y}" + f"{1 + 1}"
^
SyntaxError: f-string: invalid syntax

(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