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

This commit is contained in:
sobolevn 2025-05-02 11:34:13 +03:00 committed by GitHub
parent 39afd290ae
commit a6ddd078d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 1309 additions and 982 deletions

View file

@ -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
@ -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 {

View file

@ -1981,6 +1981,56 @@ SyntaxError: cannot assign to __debug__
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.

View file

@ -0,0 +1,2 @@
Improve :exc:`SyntaxError` message for using ``import ... as``
and ``from ... import ... as`` with not a name.

2217
Parser/parser.c generated

File diff suppressed because it is too large Load diff