Skip to content

Commit

Permalink
pythongh-118090: Improve error message for empty type param brackets (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored May 7, 2024
1 parent 0485922 commit b60d4c0
Show file tree
Hide file tree
Showing 4 changed files with 1,016 additions and 800 deletions.
15 changes: 12 additions & 3 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")}
41 changes: 41 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,22 @@
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):
Expand Down Expand Up @@ -2027,6 +2043,31 @@ def f(x: *b)
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):
...
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve :exc:`SyntaxError` message for empty type param brackets.
Loading

0 comments on commit b60d4c0

Please sign in to comment.