From a00e964dbd62b525618584d98edcf179840f1b46 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Thu, 21 Jan 2021 01:22:43 +0000 Subject: [PATCH 1/2] Add a What's New entry for the parsing improvements --- Doc/whatsnew/3.10.rst | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 0fca2e8b8c311a..d8451c06636573 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -166,6 +166,48 @@ See :class:`typing.Callable`, :class:`typing.ParamSpec`, (Contributed by Ken Jin in :issue:`41559`.) +Better error messages in the parser +----------------------------------- + +When parsing code that contains unclosed parentheses or brackets the interpreter +now includes the location of the unclosed bracket of parentheses instead of displaying +*SyntaxError: unexpected EOF while parsing* or pointing to some incorrect location. +For instance, consider the following code (notice the unclosed '{'): + +.. code-block:: python + + expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4, + 38: 4, 39: 4, 45: 5, 46: 5, 47: 5, 48: 5, 49: 5, 54: 6, + some_other_code = foo() + +previous versions of the interpreter reported confusing places as the location of +the syntax error: + +.. code-block:: text + + File "example.py", line 3 + some_other_code = foo() + ^ + SyntaxError: invalid syntax + +but in Python3.10 a more informative error is emitted: + +.. code-block:: text + + File "example.py", line 1 + expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4, + ^ + SyntaxError: '{' was never closed + + +In a similar way, errors involving unclosed string literals (single and triple +quoted) now point to the start of the string instead of reporting EOF/EOL. + +These improvements are inspired by previous work in the PyPy interpreter. + +(Contributed by Pablo Galindo in :issue:`42864` and Batuhan Taskaya in +:issue:`40176`.) + Other Language Changes ====================== From 11807f90ff929d76f542cea4f5cda89b930c1a70 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Thu, 21 Jan 2021 16:49:51 +0000 Subject: [PATCH 2/2] Update Doc/whatsnew/3.10.rst Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> --- Doc/whatsnew/3.10.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index d8451c06636573..fce2c44457f06f 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -185,10 +185,10 @@ the syntax error: .. code-block:: text - File "example.py", line 3 - some_other_code = foo() - ^ - SyntaxError: invalid syntax + File "example.py", line 3 + some_other_code = foo() + ^ + SyntaxError: invalid syntax but in Python3.10 a more informative error is emitted: