Skip to content

Commit 1fae8ed

Browse files
committed
Fix other "cannot use name as import target" cases
1 parent ca28947 commit 1fae8ed

File tree

3 files changed

+508
-483
lines changed

3 files changed

+508
-483
lines changed

Grammar/python.gram

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,13 +1434,13 @@ invalid_import:
14341434
invalid_dotted_as_name:
14351435
| a=dotted_name b=['as' NAME] c=dotted_name ('as' | ',' | ')' | ';' | NEWLINE) {
14361436
RAISE_SYNTAX_ERROR_KNOWN_RANGE(b ? (expr_ty) b : a, c, "expected comma between import clauses") }
1437-
| dotted_name 'as' !(NAME (',' | ')' | ';' | NEWLINE)) a=expression {
1437+
| dotted_name 'as' !(NAME (',' | ')' | ';' | NEWLINE)) a=expression (',' | ')' | ';' | NEWLINE) {
14381438
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
14391439
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }
14401440
invalid_import_from_as_name:
14411441
| [NAME 'as'] a=NAME b=NAME ('as' | ',' | ')' | ';' | NEWLINE) {
14421442
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected comma between import clauses") }
1443-
| NAME 'as' !(NAME (',' | ')' | ';' | NEWLINE)) a=expression {
1443+
| NAME 'as' !(NAME (',' | ')' | ';' | NEWLINE)) a=expression (',' | ')' | ';' | NEWLINE) {
14441444
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
14451445
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }
14461446

Lib/test/test_syntax.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,25 @@
22062206
Traceback (most recent call last):
22072207
SyntaxError: cannot assign to None
22082208
2209+
# Check that we don't raise a "cannot use name as import target" error
2210+
# when there's a syntax error after the import target.
2211+
2212+
>>> import a as b(-)
2213+
Traceback (most recent call last):
2214+
SyntaxError: invalid syntax
2215+
2216+
>>> import a as b None
2217+
Traceback (most recent call last):
2218+
SyntaxError: invalid syntax
2219+
2220+
>>> import a as b as c
2221+
Traceback (most recent call last):
2222+
SyntaxError: invalid syntax
2223+
2224+
>>> from x import a as b None
2225+
Traceback (most recent call last):
2226+
SyntaxError: invalid syntax
2227+
22092228
# Check that we dont raise the "trailing comma" error if there is more
22102229
# input to the left of the valid part that we parsed.
22112230

0 commit comments

Comments
 (0)