10000 gh-123539: Improve SyntaxError msg for `import as` with not a name by sobolevn · Pull Request #123629 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-123539: Improve SyntaxError msg for import as with not a name #123629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,17 @@ import_from_targets[asdl_alias_seq*]:
import_from_as_names[asdl_alias_seq*]:
| a[asdl_alias_seq*]=','.import_from_as_name+ { a }
import_from_as_name[alias_ty]:
| a=NAME b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
(b) ? ((expr_ty) b)->v.Name.id : NULL,
EXTRA) }
| invalid_import_from_as_name
| a=NAME b=['as' z=NAME { z }] { _PyAST_alias(
a->v.Name.id, (b) ? ((expr_ty) b)->v.Name.id : NULL, EXTRA) }

dotted_as_names[asdl_alias_seq*]:
| a[asdl_alias_seq*]=','.dotted_as_name+ { a }
dotted_as_name[alias_ty]:
| a=dotted_name b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
(b) ? ((expr_ty) b)->v.Name.id : NULL,
EXTRA) }
| invalid_dotted_as_name
| a=dotted_name b=['as' z=NAME { z }] { _PyAST_alias(
a->v.Name.id, (b) ? ((expr_ty) b)->v.Name.id : NULL, EXTRA) }

dotted_name[expr_ty]:
| a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
| NAME
Expand Down Expand Up @@ -1375,6 +1377,14 @@ invalid_import:
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "Did you mean to use 'from ... import ...' instead?") }
| 'import' token=NEWLINE {
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }
invalid_dotted_as_name:
| dotted_name 'as' !(NAME (',' | ')' | NEWLINE)) a=expression {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }
invalid_import_from_as_name:
| NAME 'as' !(NAME (',' | ')' | NEWLINE)) a=expression {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }

invalid_import_from_targets:
| import_from_as_names ',' NEWLINE {
Expand Down
50 changes: 50 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,56 @@
Traceback (most recent call last):
SyntaxError: cannot assign to __debug__

>>> import a as b.c
Traceback (most recent call last):
SyntaxError: cannot use attribute as import target

>>> import a.b as (a, b)
Traceback (most recent call last):
SyntaxError: cannot use tuple as import target

>>> import a, a.b as 1
Traceback (most recent call last):
SyntaxError: cannot use literal as import target

>>> import a.b as 'a', a
Traceback (most recent call last):
SyntaxError: cannot use literal as import target

>>> from a import (b as c.d)
Traceback (most recent call last):
SyntaxError: cannot use attribute as import target

>>> from a import b as 1
Traceback (most recent call last):
SyntaxError: cannot use literal as import target

>>> from a import (
... b as f())
Traceback (most recent call last):
SyntaxError: cannot use function call as import target

>>> from a import (
... b as [],
... )
Traceback (most recent call last):
SyntaxError: cannot use list as import target

>>> from a import (
... b,
... c as ()
... )
Traceback (most recent call last):
SyntaxError: cannot use tuple as import target

>>> from a import b, с as d[e]
Traceback (most recent call last):
SyntaxError: cannot use subscript as import target

>>> from a import с as d[e], b
Traceback (most recent call last):
SyntaxError: cannot use subscript as import target

# Check that we dont raise the "trailing comma" error if there is more
# input to the left of the valid part that we parsed.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve :exc:`SyntaxError` message for using ``import ... as``
and ``from ... import ... as`` with not a name.
Loading
Loading
0