Skip to content

Commit 805ede8

Browse files
authored
Add a What's New entry for the new parser error improvements (GH-24280)
1 parent 6cd5b01 commit 805ede8

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Doc/whatsnew/3.10.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,48 @@ See :class:`typing.Callable`, :class:`typing.ParamSpec`,
166166

167167
(Contributed by Ken Jin in :issue:`41559`.)
168168

169+
Better error messages in the parser
170+
-----------------------------------
171+
172+
When parsing code that contains unclosed parentheses or brackets the interpreter
173+
now includes the location of the unclosed bracket of parentheses instead of displaying
174+
*SyntaxError: unexpected EOF while parsing* or pointing to some incorrect location.
175+
For instance, consider the following code (notice the unclosed '{'):
176+
177+
.. code-block:: python
178+
179+
expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,
180+
38: 4, 39: 4, 45: 5, 46: 5, 47: 5, 48: 5, 49: 5, 54: 6,
181+
some_other_code = foo()
182+
183+
previous versions of the interpreter reported confusing places as the location of
184+
the syntax error:
185+
186+
.. code-block:: text
187+
188+
File "example.py", line 3
189+
some_other_code = foo()
190+
^
191+
SyntaxError: invalid syntax
192+
193+
but in Python3.10 a more informative error is emitted:
194+
195+
.. code-block:: text
196+
197+
File "example.py", line 1
198+
expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,
199+
^
200+
SyntaxError: '{' was never closed
201+
202+
203+
In a similar way, errors involving unclosed string literals (single and triple
204+
quoted) now point to the start of the string instead of reporting EOF/EOL.
205+
206+
These improvements are inspired by previous work in the PyPy interpreter.
207+
208+
(Contributed by Pablo Galindo in :issue:`42864` and Batuhan Taskaya in
209+
:issue:`40176`.)
210+
169211
Other Language Changes
170212
======================
171213

0 commit comments

Comments
 (0)