gh-110938: Fix error messages for indented blocks with functions and classes with generic type parameters (#110973)

This commit is contained in:
Pablo Galindo Salgado 2023-10-17 13:45:13 +01:00 committed by GitHub
parent be5e8a0103
commit 24e4ec7766
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 22 deletions

View File

@ -1369,11 +1369,11 @@ invalid_for_stmt:
| ['async'] a='for' star_targets 'in' star_expressions ':' NEWLINE !INDENT {
RAISE_INDENTATION_ERROR("expected an indented block after 'for' statement on line %d", a->lineno) }
invalid_def_raw:
| ['async'] a='def' NAME '(' [params] ')' ['->' expression] ':' NEWLINE !INDENT {
| ['async'] a='def' NAME [type_params] '(' [params] ')' ['->' expression] ':' NEWLINE !INDENT {
RAISE_INDENTATION_ERROR("expected an indented block after function definition on line %d", a->lineno) }
invalid_class_def_raw:
| 'class' NAME ['(' [arguments] ')'] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
| a='class' NAME ['(' [arguments] ')'] ':' NEWLINE !INDENT {
| 'class' NAME [type_params] ['(' [arguments] ')'] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
| a='class' NAME [type_params] ['(' [arguments] ')'] ':' NEWLINE !INDENT {
RAISE_INDENTATION_ERROR("expected an indented block after class definition on line %d", a->lineno) }
invalid_double_starred_kvpairs:

View File

@ -1446,11 +1446,21 @@ Specialized indentation errors:
Traceback (most recent call last):
IndentationError: expected an indented block after function definition on line 1
>>> def foo[T](x, /, y, *, z=2):
... pass
Traceback (most recent call last):
IndentationError: expected an indented block after function definition on line 1
>>> class Blech(A):
... pass
Traceback (most recent call last):
IndentationError: expected an indented block after class definition on line 1
>>> class Blech[T](A):
... pass
Traceback (most recent call last):
IndentationError: expected an indented block after class definition on line 1
>>> match something:
... pass
Traceback (most recent call last):

View File

@ -0,0 +1,2 @@
Fix error messages for indented blocks with functions and classes with
generic type parameters. Patch by Pablo Galindo

50
Parser/parser.c generated
View File

@ -24071,7 +24071,7 @@ invalid_for_stmt_rule(Parser *p)
}
// invalid_def_raw:
// | 'async'? 'def' NAME '(' params? ')' ['->' expression] ':' NEWLINE !INDENT
// | 'async'? 'def' NAME type_params? '(' params? ')' ['->' expression] ':' NEWLINE !INDENT
static void *
invalid_def_raw_rule(Parser *p)
{
@ -24084,12 +24084,12 @@ invalid_def_raw_rule(Parser *p)
}
void * _res = NULL;
int _mark = p->mark;
{ // 'async'? 'def' NAME '(' params? ')' ['->' expression] ':' NEWLINE !INDENT
{ // 'async'? 'def' NAME type_params? '(' params? ')' ['->' expression] ':' NEWLINE !INDENT
if (p->error_indicator) {
p->level--;
return NULL;
}
D(fprintf(stderr, "%*c> invalid_def_raw[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'async'? 'def' NAME '(' params? ')' ['->' expression] ':' NEWLINE !INDENT"));
D(fprintf(stderr, "%*c> invalid_def_raw[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'async'? 'def' NAME type_params? '(' params? ')' ['->' expression] ':' NEWLINE !INDENT"));
Token * _literal;
Token * _literal_1;
Token * _literal_2;
@ -24099,6 +24099,8 @@ invalid_def_raw_rule(Parser *p)
UNUSED(_opt_var_1); // Silence compiler warnings
void *_opt_var_2;
UNUSED(_opt_var_2); // Silence compiler warnings
void *_opt_var_3;
UNUSED(_opt_var_3); // Silence compiler warnings
Token * a;
expr_ty name_var;
Token * newline_var;
@ -24109,13 +24111,15 @@ invalid_def_raw_rule(Parser *p)
&&
(name_var = _PyPegen_name_token(p)) // NAME
&&
(_opt_var_1 = type_params_rule(p), !p->error_indicator) // type_params?
&&
(_literal = _PyPegen_expect_token(p, 7)) // token='('
&&
(_opt_var_1 = params_rule(p), !p->error_indicator) // params?
(_opt_var_2 = params_rule(p), !p->error_indicator) // params?
&&
(_literal_1 = _PyPegen_expect_token(p, 8)) // token=')'
&&
(_opt_var_2 = _tmp_226_rule(p), !p->error_indicator) // ['->' expression]
(_opt_var_3 = _tmp_226_rule(p), !p->error_indicator) // ['->' expression]
&&
(_literal_2 = _PyPegen_expect_token(p, 11)) // token=':'
&&
@ -24124,7 +24128,7 @@ invalid_def_raw_rule(Parser *p)
_PyPegen_lookahead_with_int(0, _PyPegen_expect_token, p, INDENT) // token=INDENT
)
{
D(fprintf(stderr, "%*c+ invalid_def_raw[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'async'? 'def' NAME '(' params? ')' ['->' expression] ':' NEWLINE !INDENT"));
D(fprintf(stderr, "%*c+ invalid_def_raw[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'async'? 'def' NAME type_params? '(' params? ')' ['->' expression] ':' NEWLINE !INDENT"));
_res = RAISE_INDENTATION_ERROR ( "expected an indented block after function definition on line %d" , a -> lineno );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
@ -24135,7 +24139,7 @@ invalid_def_raw_rule(Parser *p)
}
p->mark = _mark;
D(fprintf(stderr, "%*c%s invalid_def_raw[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'async'? 'def' NAME '(' params? ')' ['->' expression] ':' NEWLINE !INDENT"));
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'async'? 'def' NAME type_params? '(' params? ')' ['->' expression] ':' NEWLINE !INDENT"));
}
_res = NULL;
done:
@ -24144,8 +24148,8 @@ invalid_def_raw_rule(Parser *p)
}
// invalid_class_def_raw:
// | 'class' NAME ['(' arguments? ')'] NEWLINE
// | 'class' NAME ['(' arguments? ')'] ':' NEWLINE !INDENT
// | 'class' NAME type_params? ['(' arguments? ')'] NEWLINE
// | 'class' NAME type_params? ['(' arguments? ')'] ':' NEWLINE !INDENT
static void *
invalid_class_def_raw_rule(Parser *p)
{
@ -24158,15 +24162,17 @@ invalid_class_def_raw_rule(Parser *p)
}
void * _res = NULL;
int _mark = p->mark;
{ // 'class' NAME ['(' arguments? ')'] NEWLINE
{ // 'class' NAME type_params? ['(' arguments? ')'] NEWLINE
if (p->error_indicator) {
p->level--;
return NULL;
}
D(fprintf(stderr, "%*c> invalid_class_def_raw[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'class' NAME ['(' arguments? ')'] NEWLINE"));
D(fprintf(stderr, "%*c> invalid_class_def_raw[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'class' NAME type_params? ['(' arguments? ')'] NEWLINE"));
Token * _keyword;
void *_opt_var;
UNUSED(_opt_var); // Silence compiler warnings
void *_opt_var_1;
UNUSED(_opt_var_1); // Silence compiler warnings
expr_ty name_var;
Token * newline_var;
if (
@ -24174,12 +24180,14 @@ invalid_class_def_raw_rule(Parser *p)
&&
(name_var = _PyPegen_name_token(p)) // NAME
&&
(_opt_var = _tmp_227_rule(p), !p->error_indicator) // ['(' arguments? ')']
(_opt_var = type_params_rule(p), !p->error_indicator) // type_params?
&&
(_opt_var_1 = _tmp_227_rule(p), !p->error_indicator) // ['(' arguments? ')']
&&
(newline_var = _PyPegen_expect_token(p, NEWLINE)) // token='NEWLINE'
)
{
D(fprintf(stderr, "%*c+ invalid_class_def_raw[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'class' NAME ['(' arguments? ')'] NEWLINE"));
D(fprintf(stderr, "%*c+ invalid_class_def_raw[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'class' NAME type_params? ['(' arguments? ')'] NEWLINE"));
_res = RAISE_SYNTAX_ERROR ( "expected ':'" );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
@ -24190,17 +24198,19 @@ invalid_class_def_raw_rule(Parser *p)
}
p->mark = _mark;
D(fprintf(stderr, "%*c%s invalid_class_def_raw[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'class' NAME ['(' arguments? ')'] NEWLINE"));
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'class' NAME type_params? ['(' arguments? ')'] NEWLINE"));
}
{ // 'class' NAME ['(' arguments? ')'] ':' NEWLINE !INDENT
{ // 'class' NAME type_params? ['(' arguments? ')'] ':' NEWLINE !INDENT
if (p->error_indicator) {
p->level--;
return NULL;
}
D(fprintf(stderr, "%*c> invalid_class_def_raw[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'class' NAME ['(' arguments? ')'] ':' NEWLINE !INDENT"));
D(fprintf(stderr, "%*c> invalid_class_def_raw[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'class' NAME type_params? ['(' arguments? ')'] ':' NEWLINE !INDENT"));
Token * _literal;
void *_opt_var;
UNUSED(_opt_var); // Silence compiler warnings
void *_opt_var_1;
UNUSED(_opt_var_1); // Silence compiler warnings
Token * a;
expr_ty name_var;
Token * newline_var;
@ -24209,7 +24219,9 @@ invalid_class_def_raw_rule(Parser *p)
&&
(name_var = _PyPegen_name_token(p)) // NAME
&&
(_opt_var = _tmp_228_rule(p), !p->error_indicator) // ['(' arguments? ')']
(_opt_var = type_params_rule(p), !p->error_indicator) // type_params?
&&
(_opt_var_1 = _tmp_228_rule(p), !p->error_indicator) // ['(' arguments? ')']
&&
(_literal = _PyPegen_expect_token(p, 11)) // token=':'
&&
@ -24218,7 +24230,7 @@ invalid_class_def_raw_rule(Parser *p)
_PyPegen_lookahead_with_int(0, _PyPegen_expect_token, p, INDENT) // token=INDENT
)
{
D(fprintf(stderr, "%*c+ invalid_class_def_raw[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'class' NAME ['(' arguments? ')'] ':' NEWLINE !INDENT"));
D(fprintf(stderr, "%*c+ invalid_class_def_raw[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'class' NAME type_params? ['(' arguments? ')'] ':' NEWLINE !INDENT"));
_res = RAISE_INDENTATION_ERROR ( "expected an indented block after class definition on line %d" , a -> lineno );
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
@ -24229,7 +24241,7 @@ invalid_class_def_raw_rule(Parser *p)
}
p->mark = _mark;
D(fprintf(stderr, "%*c%s invalid_class_def_raw[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'class' NAME ['(' arguments? ')'] ':' NEWLINE !INDENT"));
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'class' NAME type_params? ['(' arguments? ')'] ':' NEWLINE !INDENT"));
}
_res = NULL;
done: