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

@ -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) }
import_stmt[stmt_ty]: import_name | import_from
import_stmt[stmt_ty]:
| invalid_import
| import_name
| import_from
# Import statements
# -----------------
@ -1230,6 +1233,10 @@ invalid_group:
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use starred expression here") }
| '(' a='**' expression ')' {
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:
| import_from_as_names ',' NEWLINE {
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }