Skip to content

Commit c39efc5

Browse files
committed
gh-123539: Improve SyntaxError msg for import as with not a name
1 parent e9d210b commit c39efc5

File tree

3 files changed

+1034
-709
lines changed

3 files changed

+1034
-709
lines changed

Grammar/python.gram

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,17 @@ import_from_targets[asdl_alias_seq*]:
227227
import_from_as_names[asdl_alias_seq*]:
228228
| a[asdl_alias_seq*]=','.import_from_as_name+ { a }
229229
import_from_as_name[alias_ty]:
230-
| a=NAME b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
231-
(b) ? ((expr_ty) b)->v.Name.id : NULL,
232-
EXTRA) }
230+
| invalid_import_from_as_name
231+
| a=NAME b=['as' z=NAME { z }] { _PyAST_alias(
232+
a->v.Name.id, (b) ? ((expr_ty) b)->v.Name.id : NULL, EXTRA) }
233+
233234
dotted_as_names[asdl_alias_seq*]:
234235
| a[asdl_alias_seq*]=','.dotted_as_name+ { a }
235236
dotted_as_name[alias_ty]:
236-
| a=dotted_name b=['as' z=NAME { z }] { _PyAST_alias(a->v.Name.id,
237-
(b) ? ((expr_ty) b)->v.Name.id : NULL,
238-
EXTRA) }
237+
| invalid_dotted_as_name
238+
| a=dotted_name b=['as' z=NAME { z }] { _PyAST_alias(
239+
a->v.Name.id, (b) ? ((expr_ty) b)->v.Name.id : NULL, EXTRA) }
240+
239241
dotted_name[expr_ty]:
240242
| a=dotted_name '.' b=NAME { _PyPegen_join_names_with_dot(p, a, b) }
241243
| NAME
@@ -1331,6 +1333,14 @@ invalid_import:
13311333
RAISE_SYNTAX_ERROR_STARTING_FROM(a, "Did you mean to use 'from ... import ...' instead?") }
13321334
| 'import' token=NEWLINE {
13331335
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }
1336+
invalid_dotted_as_name:
1337+
| dotted_name 'as' !(NAME (',' | ')' | NEWLINE)) a=expression {
1338+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
1339+
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }
1340+
invalid_import_from_as_name:
1341+
| NAME 'as' !(NAME (',' | ')' | NEWLINE)) a=expression {
1342+
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
1343+
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }
13341344

13351345
invalid_import_from_targets:
13361346
| import_from_as_names ',' NEWLINE {

Lib/test/test_syntax.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,56 @@
18311831
Traceback (most recent call last):
18321832
SyntaxError: cannot assign to __debug__
18331833
1834+
>>> import a as b.c
1835+
Traceback (most recent call last):
1836+
SyntaxError: cannot use attribute as import target
1837+
1838+
>>> import a.b as (a, b)
1839+
Traceback (most recent call last):
1840+
SyntaxError: cannot use tuple as import target
1841+
1842+
>>> import a, a.b as 1
1843+
Traceback (most recent call last):
1844+
SyntaxError: cannot use literal as import target
1845+
1846+
>>> import a.b as 'a', a
1847+
Traceback (most recent call last):
1848+
SyntaxError: cannot use literal as import target
1849+
1850+
>>> from a import (b as c.d)
1851+
Traceback (most recent call last):
1852+
SyntaxError: cannot use attribute as import target
1853+
1854+
>>> from a import b as 1
1855+
Traceback (most recent call last):
1856+
SyntaxError: cannot use literal as import target
1857+
1858+
>>> from a import (
1859+
... b as f())
1860+
Traceback (most recent call last):
1861+
SyntaxError: cannot use function call as import target
1862+
1863+
>>> from a import (
1864+
... b as [],
1865+
... )
1866+
Traceback (most recent call last):
1867+
SyntaxError: cannot use list as import target
1868+
1869+
>>> from a import (
1870+
... b,
1871+
... c as ()
1872+
... )
1873+
Traceback (most recent call last):
1874+
SyntaxError: cannot use tuple as import target
1875+
1876+
>>> from a import b, с as d[e]
1877+
Traceback (most recent call last):
1878+
SyntaxError: cannot use subscript as import target
1879+
1880+
>>> from a import с as d[e], b
1881+
Traceback (most recent call last):
1882+
SyntaxError: cannot use subscript as import target
1883+
18341884
# Check that we dont raise the "trailing comma" error if there is more
18351885
# input to the left of the valid part that we parsed.
18361886

0 commit comments

Comments
 (0)