]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Clarify that error messages are better with PEP 701 (#105150)
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Wed, 31 May 2023 21:01:29 +0000 (22:01 +0100)
committerGitHub <noreply@github.com>
Wed, 31 May 2023 21:01:29 +0000 (22:01 +0100)
Co-authored-by: Marta Gómez Macías <mgmacias@google.com>
Doc/whatsnew/3.12.rst

index 2423672f75b3707228fecb30e1d0a1a71390d8ea..6f725906f1e1aed943d285528f552c2679665da9 100644 (file)
@@ -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).