Skip to content

Commit a6ddd07

Browse files
authored
gh-123539: Improve SyntaxError msg for import as with not a name (#123629)
1 parent 39afd29 commit a6ddd07

File tree

4 files changed

+1058
-731
lines changed

4 files changed

+1058
-731
lines changed

Grammar/python.gram

+16-6
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,17 @@ import_from_targets[asdl_alias_seq*]:
233233
import_from_as_names[asdl_alias_seq*]:
234234
| a[asdl_alias_seq*]=','.import_from_as_name+ { a }
235235
import_from_as_name[alias_ty]:
236-
| a=NAME b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
237-
(b) ? ((expr_ty) b)->v.Name.id : NULL,
238-
EXTRA) }
236+
| invalid_import_from_as_name
237+
| a=NAME b=['as' z=NAME { z }] { _PyAST_alias(
238+
a->v.Name.id, (b) ? ((expr_ty) b)->v.Name.id : NULL, EXTRA) }
239+
239240
dotted_as_names[asdl_alias_seq*]:
240241
| a[asdl_alias_seq*]=','.dotted_as_name+ { a }
241242
dotted_as_name[alias_ty]:
242-
| a=dotted_name b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
243-
(b) ? ((expr_ty) b)->v.Name.id : NULL,
244-
EXTRA) }
243+
| invalid_dotted_as_name
244+
| a=dotted_name b=['as' z=NAME { z }] { _PyAST_alias(
245+
a->v.Name.id, (b) ? ((expr_ty) b)->v.Name.id : NULL, EXTRA) }
246+
245247
dotted_name[expr_ty]:
246248
| a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
247249
| NAME
@@ -1375,6 +1377,14 @@ invalid_import:
13751377
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "Did you mean to use 'from ... import ...' instead?") }
13761378
| 'import' token=NEWLINE {
13771379
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }
1380+
invalid_dotted_as_name:
1381+
| dotted_name 'as' !(NAME (',' | ')' | NEWLINE)) a=expression {
1382+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
1383+
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }
1384+
invalid_import_from_as_name:
1385+
| NAME 'as' !(NAME (',' | ')' | NEWLINE)) a=expression {
1386+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
1387+
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }
13781388

13791389
invalid_import_from_targets:
13801390
| import_from_as_names ',' NEWLINE {

Lib/test/test_syntax.py

+50
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,56 @@
19811981
Traceback (most recent call last):
19821982
SyntaxError: cannot assign to __debug__
19831983
1984+
>>> import a as b.c
1985+
Traceback (most recent call last):
1986+
SyntaxError: cannot use attribute as import target
1987+
1988+
>>> import a.b as (a, b)
1989+
Traceback (most recent call last):
1990+
SyntaxError: cannot use tuple as import target
1991+
1992+
>>> import a, a.b as 1
1993+
Traceback (most recent call last):
1994+
SyntaxError: cannot use literal as import target
1995+
1996+
>>> import a.b as 'a', a
1997+
Traceback (most recent call last):
1998+
SyntaxError: cannot use literal as import target
1999+
2000+
>>> from a import (b as c.d)
2001+
Traceback (most recent call last):
2002+
SyntaxError: cannot use attribute as import target
2003+
2004+
>>> from a import b as 1
2005+
Traceback (most recent call last):
2006+
SyntaxError: cannot use literal as import target
2007+
2008+
>>> from a import (
2009+
... b as f())
2010+
Traceback (most recent call last):
2011+
SyntaxError: cannot use function call as import target
2012+
2013+
>>> from a import (
2014+
... b as [],
2015+
... )
2016+
Traceback (most recent call last):
2017+
SyntaxError: cannot use list as import target
2018+
2019+
>>> from a import (
2020+
... b,
2021+
... c as ()
2022+
... )
2023+
Traceback (most recent call last):
2024+
SyntaxError: cannot use tuple as import target
2025+
2026+
>>> from a import b, с as d[e]
2027+
Traceback (most recent call last):
2028+
SyntaxError: cannot use subscript as import target
2029+
2030+
>>> from a import с as d[e], b
2031+
Traceback (most recent call last):
2032+
SyntaxError: cannot use subscript as import target
2033+
19842034
# Check that we dont raise the "trailing comma" error if there is more
19852035
# input to the left of the valid part that we parsed.
19862036
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve :exc:`SyntaxError` message for using ``import ... as``
2+
and ``from ... import ... as`` with not a name.

0 commit comments

Comments
 (0)