Skip to content

Commit ca28947

Browse files
committed
Exclude trailing invalid import targets
1 parent e0b46ea commit ca28947

File tree

3 files changed

+738
-606
lines changed

3 files changed

+738
-606
lines changed

Grammar/python.gram

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,13 +1432,13 @@ invalid_import:
14321432
| 'import' token=NEWLINE {
14331433
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }
14341434
invalid_dotted_as_name:
1435-
| a=dotted_name b=['as' NAME] c=dotted_name {
1435+
| 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") }
14371437
| dotted_name 'as' !(NAME (',' | ')' | ';' | NEWLINE)) a=expression {
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:
1441-
| [NAME 'as'] a=NAME b=NAME {
1441+
| [NAME 'as'] a=NAME b=NAME ('as' | ',' | ')' | ';' | NEWLINE) {
14421442
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected comma between import clauses") }
14431443
| NAME 'as' !(NAME (',' | ')' | ';' | NEWLINE)) a=expression {
14441444
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,

Lib/test/test_syntax.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,6 +2267,16 @@
22672267
Traceback (most recent call last):
22682268
SyntaxError: expected comma between import clauses
22692269
2270+
# Check that we don't raise the "missing comma" error for invalid import targets.
2271+
2272+
>>> import a b()
2273+
Traceback (most recent call last):
2274+
SyntaxError: invalid syntax
2275+
2276+
>>> from x import a b[c]
2277+
Traceback (most recent call last):
2278+
SyntaxError: invalid syntax
2279+
22702280
>>> (): int
22712281
Traceback (most recent call last):
22722282
SyntaxError: only single target (not tuple) can be annotated

0 commit comments

Comments
 (0)