Skip to content

Commit 098aa8d

Browse files
committed
Fix SyntaxError msg for erroneous stmt on same line as import-as stmt
1 parent 537133d commit 098aa8d

3 files changed

Lines changed: 57 additions & 19 deletions

File tree

Grammar/python.gram

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,11 +1415,11 @@ invalid_import:
14151415
| 'import' token=NEWLINE {
14161416
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }
14171417
invalid_dotted_as_name:
1418-
| dotted_name 'as' !(NAME (',' | ')' | NEWLINE)) a=expression {
1418+
| dotted_name 'as' !(NAME (',' | ')' | ';' | NEWLINE)) a=expression {
14191419
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
14201420
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }
14211421
invalid_import_from_as_name:
1422-
| NAME 'as' !(NAME (',' | ')' | NEWLINE)) a=expression {
1422+
| NAME 'as' !(NAME (',' | ')' | ';' | NEWLINE)) a=expression {
14231423
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
14241424
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }
14251425

Lib/test/test_syntax.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,25 @@
21332133
Traceback (most recent call last):
21342134
SyntaxError: cannot use subscript as import target
21352135
2136+
# Check that we don't raise a "cannot use name as import target" error
2137+
# if there is an error in an unrelated statement after ';'
2138+
2139+
>>> import a as b; None = 1
2140+
Traceback (most recent call last):
2141+
SyntaxError: cannot assign to None
2142+
2143+
>>> import a, b as c; d = 1; None = 1
2144+
Traceback (most recent call last):
2145+
SyntaxError: cannot assign to None
2146+
2147+
>>> from a import b as c; None = 1
2148+
Traceback (most recent call last):
2149+
SyntaxError: cannot assign to None
2150+
2151+
>>> from a import b, c as d; e = 1; None = 1
2152+
Traceback (most recent call last):
2153+
SyntaxError: cannot assign to None
2154+
21362155
# Check that we dont raise the "trailing comma" error if there is more
21372156
# input to the left of the valid part that we parsed.
21382157

Parser/parser.c

Lines changed: 36 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)