gh-118090: Improve error message for empty type param brackets (GH-118091)

This commit is contained in:
Nikita Sobolev 2024-05-07 15:01:06 +03:00 committed by GitHub
parent 04859228aa
commit b60d4c0d53
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 1430 additions and 1214 deletions

View File

@ -269,11 +269,11 @@ function_def[stmt_ty]:
function_def_raw[stmt_ty]:
| invalid_def_raw
| 'def' n=NAME t=[type_params] &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
| 'def' n=NAME t=[type_params] '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
_PyAST_FunctionDef(n->v.Name.id,
(params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
b, NULL, a, NEW_TYPE_COMMENT(p, tc), t, EXTRA) }
| 'async' 'def' n=NAME t=[type_params] &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
| 'async' 'def' n=NAME t=[type_params] '(' params=[params] ')' a=['->' z=expression { z }] ':' tc=[func_type_comment] b=block {
CHECK_VERSION(
stmt_ty,
5,
@ -641,7 +641,9 @@ type_alias[stmt_ty]:
# Type parameter declaration
# --------------------------
type_params[asdl_type_param_seq*]: '[' t=type_param_seq ']' {
type_params[asdl_type_param_seq*]:
| invalid_type_params
| '[' t=type_param_seq ']' {
CHECK_VERSION(asdl_type_param_seq *, 12, "Type parameter lists are", t) }
type_param_seq[asdl_type_param_seq*]: a[asdl_type_param_seq*]=','.type_param+ [','] { a }
@ -1392,6 +1394,7 @@ invalid_for_stmt:
invalid_def_raw:
| ['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) }
| ['async'] 'def' NAME [type_params] &&'(' [params] ')' ['->' expression] &&':' [func_type_comment] block
invalid_class_def_raw:
| 'class' NAME [type_params] ['(' [arguments] ')'] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
| a='class' NAME [type_params] ['(' [arguments] ')'] ':' NEWLINE !INDENT {
@ -1435,3 +1438,9 @@ invalid_arithmetic:
| sum ('+'|'-'|'*'|'/'|'%'|'//'|'@') a='not' b=inversion { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "'not' after an operator must be parenthesized") }
invalid_factor:
| ('+' | '-' | '~') a='not' b=factor { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "'not' after an operator must be parenthesized") }
invalid_type_params:
| '[' token=']' {
RAISE_SYNTAX_ERROR_STARTING_FROM(
token,
"Type parameter list cannot be empty")}

View File

@ -1213,6 +1213,22 @@ Missing parens after function definition
Traceback (most recent call last):
SyntaxError: expected '('
>>> def f -> int:
Traceback (most recent call last):
SyntaxError: expected '('
>>> async def f -> int: # type: int
Traceback (most recent call last):
SyntaxError: expected '('
>>> async def f[T]:
Traceback (most recent call last):
SyntaxError: expected '('
>>> def f[T] -> str:
Traceback (most recent call last):
SyntaxError: expected '('
Parenthesized arguments in function definitions
>>> def f(x, (y, z), w):
@ -2027,6 +2043,31 @@ Invalid bytes literals:
Invalid expressions in type scopes:
>>> type A[] = int
Traceback (most recent call last):
...
SyntaxError: Type parameter list cannot be empty
>>> class A[]: ...
Traceback (most recent call last):
...
SyntaxError: Type parameter list cannot be empty
>>> def some[](): ...
Traceback (most recent call last):
...
SyntaxError: Type parameter list cannot be empty
>>> def some[]()
Traceback (most recent call last):
...
SyntaxError: Type parameter list cannot be empty
>>> async def some[]: # type: int
Traceback (most recent call last):
...
SyntaxError: Type parameter list cannot be empty
>>> type A[T: (x:=3)] = int
Traceback (most recent call last):
...

View File

@ -0,0 +1 @@
Improve :exc:`SyntaxError` message for empty type param brackets.

2587
Parser/parser.c generated

File diff suppressed because it is too large Load Diff