From 4ceb60ae198f5c35d74a752384033528d8f76d7a Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Wed, 31 May 2023 16:47:34 +0100 Subject: [PATCH 1/5] Clarify that error messages are better with PEP 701 --- Doc/whatsnew/3.12.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 2423672f75b370..eaa4c26af462fb 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -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: + + >>> 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). From 66f93b121524269761cc1a628dc9c936831ab12c Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Wed, 31 May 2023 16:51:47 +0100 Subject: [PATCH 2/5] fixup! Clarify that error messages are better with PEP 701 --- Doc/whatsnew/3.12.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index eaa4c26af462fb..90fb52ea2f787f 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -221,7 +221,7 @@ are parsed with the PEG parser, so error messages can be more precise and show t >>> 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 From 7ca09c375fb687d2fd63a56556479c4b8e431b38 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Wed, 31 May 2023 17:03:03 +0100 Subject: [PATCH 3/5] Update Doc/whatsnew/3.12.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marta Gómez Macías --- Doc/whatsnew/3.12.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 90fb52ea2f787f..1175e75cf4d3b2 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -216,7 +216,7 @@ f-string raises a :exc:`SyntaxError`: 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: +are parsed with the PEG parser, error messages can be more precise and show the entire line: >>> my_string = f"{x $ y}" + f"{1 + 1}" ... From 06174dab6aecd453177633cd0a8b4e1e1b7f90fe Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Wed, 31 May 2023 21:42:31 +0100 Subject: [PATCH 4/5] fixup! Update Doc/whatsnew/3.12.rst --- Doc/whatsnew/3.12.rst | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 1175e75cf4d3b2..6cd0b182ad7f5b 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -208,21 +208,25 @@ with the PEG parser (see :pep:`617`), now error messages for f-strings are more 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 +.. code-block:: python + + >>> my_string = f"{x z y}" + f"{1 + 1}" + File "", 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: - >>> my_string = f"{x $ y}" + f"{1 + 1}" - ... - my_string = f"{x $ y}" + f"{1 + 1}" - ^ - SyntaxError: f-string: invalid syntax +.. code-block:: python + + >>> my_string = f"{x z y}" + f"{1 + 1}" + File "", 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, From 6fe2f27389113b39903e283a609c2f68710e97dd Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Wed, 31 May 2023 21:42:51 +0100 Subject: [PATCH 5/5] fixup! fixup! Update Doc/whatsnew/3.12.rst --- Doc/whatsnew/3.12.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 6cd0b182ad7f5b..6f725906f1e1ae 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -225,7 +225,7 @@ are parsed with the PEG parser, error messages can be more precise and show the >>> my_string = f"{x z y}" + f"{1 + 1}" File "", 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