From ff3d9c0f1a960c3d677d7eb0200d59f3aa2a8b1b Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Fri, 16 Apr 2021 01:28:48 +0100 Subject: Improve the section with SyntaxError message improvements to the What's New of 3.10 (GH-25428) --- Doc/whatsnew/3.10.rst | 93 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 0962f04..6623adf 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -145,7 +145,7 @@ For instance, consider the following code (notice the unclosed '{'): previous versions of the interpreter reported confusing places as the location of the syntax error: -.. code-block:: text +.. code-block:: python File "example.py", line 3 some_other_code = foo() @@ -154,7 +154,7 @@ the syntax error: but in Python3.10 a more informative error is emitted: -.. code-block:: text +.. code-block:: python File "example.py", line 1 expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4, @@ -170,6 +170,95 @@ These improvements are inspired by previous work in the PyPy interpreter. (Contributed by Pablo Galindo in :issue:`42864` and Batuhan Taskaya in :issue:`40176`.) +A considerable ammount of new specialized messages for :exc:`SyntaxError` exceptions +have been incorporated. Some of the most notable ones: + +* Missing ``:`` before blocks: + +.. code-block:: python + + >>> if rocket.position > event_horizon + File "", line 1 + if rocket.position > event_horizon + ^ + SyntaxError: expected ':' + + +* Unparenthesised tuples in comprehensions targets: + +.. code-block:: python + + >>> {x,y for x,y in range(100)} + File "", line 1 + {x,y for x,y in range(100)} + ^ + SyntaxError: did you forget parentheses around the comprehension target? + +* Missing commas in collection literals: + +.. code-block:: python + + >>> items = { + ... x: 1, + ... y: 2 + ... z: 3, + File "", line 3 + y: 2 + ^ + SyntaxError: invalid syntax. Perhaps you forgot a comma? + +* Exception groups without parentheses: + +.. code-block:: python + + >>> try: + ... build_dyson_sphere() + ... except NotEnoughScienceError, NotEnoughResourcesError: + File "", line 3 + except NotEnoughScienceError, NotEnoughResourcesError: + ^ + SyntaxError: exception group must be parenthesized + +* Missing ``:`` and values in dictionary literals: + +.. code-block:: python + + >>> values = { + ... x: 1, + ... y: 2, + ... z: + ... } + File "", line 4 + z: + ^ + SyntaxError: expression expected after dictionary key and ':' + + >>> values = {x:1, y:2, z w:3} + File "", line 1 + values = {x:1, y:2, z w:3} + ^ + SyntaxError: ':' expected after dictionary key + +* Usage of ``=`` instead of ``==`` in comparisons: + +.. code-block:: python + + >>> if rocket.position = event_horizon: + File "", line 1 + if rocket.position = event_horizon: + ^ + SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='? + +* Usage of ``*`` in f-strings: + +.. code-block:: python + + >>> f"Black holes {*all_black_holes} and revelations" + File "", line 1 + (*all_black_holes) + ^ + SyntaxError: f-string: cannot use starred expression here + AttributeErrors ~~~~~~~~~~~~~~~ -- cgit v0.12