Skip to content

bpo-41064: Improve syntax error for invalid usage of '**' in f-strings #25006

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
Mar 24, 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: 2 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@ invalid_for_target:
invalid_group:
| '(' a=starred_expression ')' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use starred expression here") }
| '(' a='**' expression ')' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "can't use double starred expression here") }
invalid_import_from_targets:
| import_from_as_names ',' {
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1275,5 +1275,14 @@ def test_with_an_underscore_and_a_comma_in_format_specifier(self):
with self.assertRaisesRegex(ValueError, error_msg):
f'{1:_,}'

def test_syntax_error_for_starred_expressions(self):
error_msg = re.escape("can't use starred expression here")
with self.assertRaisesRegex(SyntaxError, error_msg):
compile("f'{*a}'", "?", "exec")

error_msg = re.escape("can't use double starred expression here")
with self.assertRaisesRegex(SyntaxError, error_msg):
compile("f'{**a}'", "?", "exec")

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve the syntax error for invalid usage of double starred elements ('**')
in f-strings. Patch by Pablo Galindo.
35 changes: 34 additions & 1 deletion Parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -18285,7 +18285,7 @@ invalid_for_target_rule(Parser *p)
return _res;
}

// invalid_group: '(' starred_expression ')'
// invalid_group: '(' starred_expression ')' | '(' '**' expression ')'
static void *
invalid_group_rule(Parser *p)
{
Expand Down Expand Up @@ -18326,6 +18326,39 @@ invalid_group_rule(Parser *p)
D(fprintf(stderr, "%*c%s invalid_group[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'(' starred_expression ')'"));
}
{ // '(' '**' expression ')'
if (p->error_indicator) {
D(p->level--);
return NULL;
}
D(fprintf(stderr, "%*c> invalid_group[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'(' '**' expression ')'"));
Token * _literal;
Token * _literal_1;
Token * a;
expr_ty expression_var;
if (
(_literal = _PyPegen_expect_token(p, 7)) // token='('
&&
(a = _PyPegen_expect_token(p, 35)) // token='**'
&&
(expression_var = expression_rule(p)) // expression
&&
(_literal_1 = _PyPegen_expect_token(p, 8)) // token=')'
)
{
D(fprintf(stderr, "%*c+ invalid_group[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'(' '**' expression ')'"));
_res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "can't use double starred expression here" );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
D(p->level--);
return NULL;
}
goto done;
}
p->mark = _mark;
D(fprintf(stderr, "%*c%s invalid_group[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'(' '**' expression ')'"));
}
_res = NULL;
done:
D(p->level--);
Expand Down