Skip to content

Commit 3b4d41c

Browse files
committed
Improve SyntaxError for missing comma between import clauses
1 parent 17070f4 commit 3b4d41c

3 files changed

Lines changed: 711 additions & 583 deletions

File tree

Grammar/python.gram

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,10 +1432,14 @@ 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 {
1436+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(b ? (expr_ty) b : a, c, "expected comma between import clauses") }
14351437
| dotted_name 'as' !(NAME (',' | ')' | ';' | NEWLINE)) a=expression {
14361438
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
14371439
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }
14381440
invalid_import_from_as_name:
1441+
| [NAME 'as'] a=NAME b=NAME {
1442+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected comma between import clauses") }
14391443
| NAME 'as' !(NAME (',' | ')' | ';' | NEWLINE)) a=expression {
14401444
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a,
14411445
"cannot use %s as import target", _PyPegen_get_expr_name(a)) }

Lib/test/test_syntax.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,6 +2225,22 @@
22252225
Traceback (most recent call last):
22262226
SyntaxError: Expected one or more names after 'import'
22272227
2228+
>>> import a b
2229+
Traceback (most recent call last):
2230+
SyntaxError: expected comma between import clauses
2231+
2232+
>>> import a.a as a b.b
2233+
Traceback (most recent call last):
2234+
SyntaxError: expected comma between import clauses
2235+
2236+
>>> from x import a b
2237+
Traceback (most recent call last):
2238+
SyntaxError: expected comma between import clauses
2239+
2240+
>>> from x import a as a b
2241+
Traceback (most recent call last):
2242+
SyntaxError: expected comma between import clauses
2243+
22282244
>>> (): int
22292245
Traceback (most recent call last):
22302246
SyntaxError: only single target (not tuple) can be annotated

0 commit comments

Comments
 (0)