Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
15 changes: 12 additions & 3 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,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 @@ -638,7 +638,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 @@ -1385,6 +1387,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 @@ -1428,3 +1431,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