Skip to content

bpo-44947: Refine the syntax error for trailing commas in import statements #27814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ invalid_group:
| '(' a='**' expression ')' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use double starred expression here") }
invalid_import_from_targets:
| import_from_as_names ',' {
| import_from_as_names ',' NEWLINE {
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }

invalid_with_stmt:
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,13 @@
Traceback (most recent call last):
SyntaxError: trailing comma not allowed without surrounding parentheses

# Check that we dont raise the "trailing comma" error if there is more
# input to the left of the valid part that we parsed.

>>> from t import x,y, and 3
Traceback (most recent call last):
SyntaxError: invalid syntax

>>> (): int
Traceback (most recent call last):
SyntaxError: only single target (not tuple) can be annotated
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Refine the syntax error for trailing commas in import statements. Patch by
Pablo Galindo.
13 changes: 8 additions & 5 deletions Parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -19540,7 +19540,7 @@ invalid_group_rule(Parser *p)
return _res;
}

// invalid_import_from_targets: import_from_as_names ','
// invalid_import_from_targets: import_from_as_names ',' NEWLINE
static void *
invalid_import_from_targets_rule(Parser *p)
{
Expand All @@ -19551,21 +19551,24 @@ invalid_import_from_targets_rule(Parser *p)
}
void * _res = NULL;
int _mark = p->mark;
{ // import_from_as_names ','
{ // import_from_as_names ',' NEWLINE
if (p->error_indicator) {
D(p->level--);
return NULL;
}
D(fprintf(stderr, "%*c> invalid_import_from_targets[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "import_from_as_names ','"));
D(fprintf(stderr, "%*c> invalid_import_from_targets[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "import_from_as_names ',' NEWLINE"));
Token * _literal;
asdl_alias_seq* import_from_as_names_var;
Token * newline_var;
if (
(import_from_as_names_var = import_from_as_names_rule(p)) // import_from_as_names
&&
(_literal = _PyPegen_expect_token(p, 12)) // token=','
&&
(newline_var = _PyPegen_expect_token(p, NEWLINE)) // token='NEWLINE'
)
{
D(fprintf(stderr, "%*c+ invalid_import_from_targets[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "import_from_as_names ','"));
D(fprintf(stderr, "%*c+ invalid_import_from_targets[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "import_from_as_names ',' NEWLINE"));
_res = RAISE_SYNTAX_ERROR ( "trailing comma not allowed without surrounding parentheses" );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
Expand All @@ -19576,7 +19579,7 @@ invalid_import_from_targets_rule(Parser *p)
}
p->mark = _mark;
D(fprintf(stderr, "%*c%s invalid_import_from_targets[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "import_from_as_names ','"));
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "import_from_as_names ',' NEWLINE"));
}
_res = NULL;
done:
Expand Down