diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2021-04-16 00:28:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-16 00:28:48 (GMT) |
commit | ff3d9c0f1a960c3d677d7eb0200d59f3aa2a8b1b (patch) | |
tree | 69604a3ab7091365d8f706ef6bebd0031880782f | |
parent | b5b98bd8f8c72a9068cf149dbc162c8c95d30057 (diff) | |
download | cpython-ff3d9c0f1a960c3d677d7eb0200d59f3aa2a8b1b.zip cpython-ff3d9c0f1a960c3d677d7eb0200d59f3aa2a8b1b.tar.gz cpython-ff3d9c0f1a960c3d677d7eb0200d59f3aa2a8b1b.tar.bz2 |
Improve the section with SyntaxError message improvements to the What's New of 3.10 (GH-25428)
-rw-r--r-- | Doc/whatsnew/3.10.rst | 93 |
1 files 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 "<stdin>", 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 "<stdin>", 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 "<stdin>", 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 "<stdin>", 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 "<stdin>", line 4 + z: + ^ + SyntaxError: expression expected after dictionary key and ':' + + >>> values = {x:1, y:2, z w:3} + File "<stdin>", 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 "<stdin>", 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 "<stdin>", line 1 + (*all_black_holes) + ^ + SyntaxError: f-string: cannot use starred expression here + AttributeErrors ~~~~~~~~~~~~~~~ |