Skip to content
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