Skip to content

bpo-45450: Improve syntax error for parenthesized arguments #28906

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 1 commit into from
Nov 20, 2021
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
4 changes: 4 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1140,12 +1140,16 @@ invalid_dict_comprehension:
invalid_parameters:
| param_no_default* invalid_parameters_helper a=param_no_default {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "non-default argument follows default argument") }
| param_no_default* a='(' param_no_default+ ','? b=')' {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "Function parameters cannot be parenthesized") }
invalid_parameters_helper: # This is only there to avoid type errors
| a=slash_with_default { _PyPegen_singleton_seq(p, a) }
| param_with_default+
invalid_lambda_parameters:
| lambda_param_no_default* invalid_lambda_parameters_helper a=lambda_param_no_default {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "non-default argument follows default argument") }
| lambda_param_no_default* a='(' ','.lambda_param+ ','? b=')' {
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "Lambda expression parameters cannot be parenthesized") }
invalid_lambda_parameters_helper:
| a=lambda_slash_with_default { _PyPegen_singleton_seq(p, a) }
| lambda_param_with_default+
Expand Down
38 changes: 38 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,44 @@
Traceback (most recent call last):
SyntaxError: expected '('

Parenthesized arguments in function definitions

>>> def f(x, (y, z), w):
... pass
Traceback (most recent call last):
SyntaxError: Function parameters cannot be parenthesized

>>> def f((x, y, z, w)):
... pass
Traceback (most recent call last):
SyntaxError: Function parameters cannot be parenthesized

>>> def f(x, (y, z, w)):
... pass
Traceback (most recent call last):
SyntaxError: Function parameters cannot be parenthesized

>>> def f((x, y, z), w):
... pass
Traceback (most recent call last):
SyntaxError: Function parameters cannot be parenthesized

>>> lambda x, (y, z), w: None
Traceback (most recent call last):
SyntaxError: Lambda expression parameters cannot be parenthesized

>>> lambda (x, y, z, w): None
Traceback (most recent call last):
SyntaxError: Lambda expression parameters cannot be parenthesized

>>> lambda x, (y, z, w): None
Traceback (most recent call last):
SyntaxError: Lambda expression parameters cannot be parenthesized

>>> lambda (x, y, z), w: None
Traceback (most recent call last):
SyntaxError: Lambda expression parameters cannot be parenthesized

Custom error messages for try blocks that are not followed by except/finally

>>> try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve the syntax error message for parenthesized arguments. Patch by Pablo
Galindo.
Loading