Skip to content

Commit 72e0aa2

Browse files
authored
bpo-40176: Improve error messages for trailing comma on from import (GH-20294)
1 parent d10fef3 commit 72e0aa2

File tree

3 files changed

+220
-151
lines changed

3 files changed

+220
-151
lines changed

Grammar/python.gram

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ import_from[stmt_ty]:
134134
_Py_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
135135
import_from_targets[asdl_seq*]:
136136
| '(' a=import_from_as_names [','] ')' { a }
137-
| import_from_as_names
137+
| import_from_as_names !','
138138
| '*' { _PyPegen_singleton_seq(p, CHECK(_PyPegen_alias_for_star(p))) }
139+
| invalid_import_from_targets
139140
import_from_as_names[asdl_seq*]:
140141
| a=','.import_from_as_name+ { a }
141142
import_from_as_name[alias_ty]:
@@ -670,3 +671,6 @@ invalid_double_type_comments:
670671
invalid_del_target:
671672
| a=star_expression &del_target_end {
672673
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot delete %s", _PyPegen_get_expr_name(a)) }
674+
invalid_import_from_targets:
675+
| import_from_as_names ',' {
676+
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }

Lib/test/test_syntax.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,14 @@
641641
Traceback (most recent call last):
642642
SyntaxError: cannot assign to f-string expression
643643
644+
>>> from t import x,
645+
Traceback (most recent call last):
646+
SyntaxError: trailing comma not allowed without surrounding parentheses
647+
648+
>>> from t import x,y,
649+
Traceback (most recent call last):
650+
SyntaxError: trailing comma not allowed without surrounding parentheses
651+
644652
Corner-cases that used to fail to raise the correct error:
645653
646654
>>> def f(*, x=lambda __debug__:0): pass

0 commit comments

Comments
 (0)