gh-98931: Improve error message when the user types 'import x from y' instead of 'from y import x' (#98932)

This commit is contained in:
Pablo Galindo Salgado 2022-11-01 13:01:20 +00:00 committed by GitHub
parent 0e15c31c7e
commit 395d4285bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 503 additions and 392 deletions

View File

@ -70,6 +70,18 @@ Important deprecations, removals or restrictions:
* :pep:`623`, Remove wstr from Unicode * :pep:`623`, Remove wstr from Unicode
Improved Error Messages
=======================
* Improve the :exc:`SyntaxError` error message when the user types ``import x
from y`` instead of ``from y import x``. Contributed by Pablo Galindo in :gh:`98931`.
>>> import a.y.z from b.y.z
Traceback (most recent call last):
File "<stdin>", line 1
import a.y.z from b.y.z
^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Did you mean to use 'from ... import ...' instead?
New Features New Features
============ ============

View File

@ -194,7 +194,10 @@ yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }
assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) } assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
import_stmt[stmt_ty]: import_name | import_from import_stmt[stmt_ty]:
| invalid_import
| import_name
| import_from
# Import statements # Import statements
# ----------------- # -----------------
@ -1230,6 +1233,10 @@ invalid_group:
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use starred expression here") } RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use starred expression here") }
| '(' a='**' expression ')' { | '(' a='**' expression ')' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") } RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") }
invalid_import:
| a='import' dotted_name 'from' dotted_name {
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "Did you mean to use 'from ... import ...' instead?") }
invalid_import_from_targets: invalid_import_from_targets:
| import_from_as_names ',' NEWLINE { | import_from_as_names ',' NEWLINE {
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") } RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }

View File

@ -1584,6 +1584,22 @@ SyntaxError: trailing comma not allowed without surrounding parentheses
Traceback (most recent call last): Traceback (most recent call last):
SyntaxError: trailing comma not allowed without surrounding parentheses SyntaxError: trailing comma not allowed without surrounding parentheses
>>> import a from b
Traceback (most recent call last):
SyntaxError: Did you mean to use 'from ... import ...' instead?
>>> import a.y.z from b.y.z
Traceback (most recent call last):
SyntaxError: Did you mean to use 'from ... import ...' instead?
>>> import a from b as bar
Traceback (most recent call last):
SyntaxError: Did you mean to use 'from ... import ...' instead?
>>> import a.y.z from b.y.z as bar
Traceback (most recent call last):
SyntaxError: Did you mean to use 'from ... import ...' instead?
# Check that we dont raise the "trailing comma" error if there is more # Check that we dont raise the "trailing comma" error if there is more
# input to the left of the valid part that we parsed. # input to the left of the valid part that we parsed.

View File

@ -0,0 +1,2 @@
Improve the :exc:`SyntaxError` error message when the user types ``import x
from y`` instead of ``from y import x``. Patch by Pablo Galindo

856
Parser/parser.c generated

File diff suppressed because it is too large Load Diff