Skip to content

bpo-40334: Reproduce error message for type comments on bare '*' #20151

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 2 commits into from
May 18, 2020
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
1 change: 1 addition & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ invalid_parameters:
RAISE_SYNTAX_ERROR("non-default argument follows default argument") }
invalid_star_etc:
| '*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
| '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") }
invalid_lambda_star_etc:
| '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named arguments must follow bare *") }
invalid_double_type_comments:
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@
Traceback (most recent call last):
SyntaxError: invalid syntax

>>> import ast; ast.parse('''
... def f(
... *, # type: int
... a, # type: int
... ):
... pass
... ''', type_comments=True)
Traceback (most recent call last):
SyntaxError: bare * has associated type comment


From ast_for_funcdef():

Expand Down
23 changes: 22 additions & 1 deletion Parser/pegen/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -10991,7 +10991,7 @@ invalid_parameters_rule(Parser *p)
return _res;
}

// invalid_star_etc: '*' (')' | ',' (')' | '**'))
// invalid_star_etc: '*' (')' | ',' (')' | '**')) | '*' ',' TYPE_COMMENT
static void *
invalid_star_etc_rule(Parser *p)
{
Expand All @@ -11018,6 +11018,27 @@ invalid_star_etc_rule(Parser *p)
}
p->mark = _mark;
}
{ // '*' ',' TYPE_COMMENT
Token * _literal;
Token * _literal_1;
Token * type_comment_var;
if (
(_literal = _PyPegen_expect_token(p, 16)) // token='*'
&&
(_literal_1 = _PyPegen_expect_token(p, 12)) // token=','
&&
(type_comment_var = _PyPegen_expect_token(p, TYPE_COMMENT)) // token='TYPE_COMMENT'
)
{
_res = RAISE_SYNTAX_ERROR ( "bare * has associated type comment" );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
return NULL;
}
goto done;
}
p->mark = _mark;
}
_res = NULL;
done:
return _res;
Expand Down
40 changes: 20 additions & 20 deletions Parser/pegen/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,25 +431,6 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
return NULL;
}

void *_PyPegen_arguments_parsing_error(Parser *p, expr_ty e) {
int kwarg_unpacking = 0;
for (Py_ssize_t i = 0, l = asdl_seq_LEN(e->v.Call.keywords); i < l; i++) {
keyword_ty keyword = asdl_seq_GET(e->v.Call.keywords, i);
if (!keyword->arg) {
kwarg_unpacking = 1;
}
}

const char *msg = NULL;
if (kwarg_unpacking) {
msg = "positional argument follows keyword argument unpacking";
} else {
msg = "positional argument follows keyword argument";
}

return RAISE_SYNTAX_ERROR(msg);
}

#if 0
static const char *
token_name(int type)
Expand Down Expand Up @@ -2099,4 +2080,23 @@ _PyPegen_get_invalid_target(expr_ty e)
default:
return e;
}
}
}

void *_PyPegen_arguments_parsing_error(Parser *p, expr_ty e) {
int kwarg_unpacking = 0;
for (Py_ssize_t i = 0, l = asdl_seq_LEN(e->v.Call.keywords); i < l; i++) {
keyword_ty keyword = asdl_seq_GET(e->v.Call.keywords, i);
if (!keyword->arg) {
kwarg_unpacking = 1;
}
}

const char *msg = NULL;
if (kwarg_unpacking) {
msg = "positional argument follows keyword argument unpacking";
} else {
msg = "positional argument follows keyword argument";
}

return RAISE_SYNTAX_ERROR(msg);
}
4 changes: 2 additions & 2 deletions Parser/pegen/pegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,13 @@ asdl_seq *_PyPegen_seq_extract_starred_exprs(Parser *, asdl_seq *);
asdl_seq *_PyPegen_seq_delete_starred_exprs(Parser *, asdl_seq *);
expr_ty _PyPegen_concatenate_strings(Parser *p, asdl_seq *);
asdl_seq *_PyPegen_join_sequences(Parser *, asdl_seq *, asdl_seq *);
void *_PyPegen_arguments_parsing_error(Parser *, expr_ty);
int _PyPegen_check_barry_as_flufl(Parser *);
mod_ty _PyPegen_make_module(Parser *, asdl_seq *);

// Error reporting helpers

expr_ty _PyPegen_get_invalid_target(expr_ty e);
void *_PyPegen_arguments_parsing_error(Parser *, expr_ty);


void *_PyPegen_parse(Parser *);

Expand Down