diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2022-11-01 13:01:20 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-01 13:01:20 (GMT) |
commit | 395d4285bfba2177719efd826fbb89bf3efcf641 (patch) | |
tree | 58cdc74ef24a74170efc3c021e29576ed6d13579 /Grammar | |
parent | 0e15c31c7e9907fdbe38a3f419b669fed5bb3b33 (diff) | |
download | cpython-395d4285bfba2177719efd826fbb89bf3efcf641.zip cpython-395d4285bfba2177719efd826fbb89bf3efcf641.tar.gz cpython-395d4285bfba2177719efd826fbb89bf3efcf641.tar.bz2 |
gh-98931: Improve error message when the user types 'import x from y' instead of 'from y import x' (#98932)
Diffstat (limited to 'Grammar')
-rw-r--r-- | Grammar/python.gram | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/Grammar/python.gram b/Grammar/python.gram index 439f08a..7dfc3df 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -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") } |