Skip to content

Commit 7a1d932

Browse files
authored
bpo-45450: Improve syntax error for parenthesized arguments (pythonGH-28906)
1 parent 9852339 commit 7a1d932

4 files changed

Lines changed: 893 additions & 445 deletions

File tree

Grammar/python.gram

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,12 +1140,16 @@ invalid_dict_comprehension:
11401140
invalid_parameters:
11411141
| param_no_default* invalid_parameters_helper a=param_no_default {
11421142
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "non-default argument follows default argument") }
1143+
| param_no_default* a='(' param_no_default+ ','? b=')' {
1144+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "Function parameters cannot be parenthesized") }
11431145
invalid_parameters_helper: # This is only there to avoid type errors
11441146
| a=slash_with_default { _PyPegen_singleton_seq(p, a) }
11451147
| param_with_default+
11461148
invalid_lambda_parameters:
11471149
| lambda_param_no_default* invalid_lambda_parameters_helper a=lambda_param_no_default {
11481150
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "non-default argument follows default argument") }
1151+
| lambda_param_no_default* a='(' ','.lambda_param+ ','? b=')' {
1152+
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "Lambda expression parameters cannot be parenthesized") }
11491153
invalid_lambda_parameters_helper:
11501154
| a=lambda_slash_with_default { _PyPegen_singleton_seq(p, a) }
11511155
| lambda_param_with_default+

Lib/test/test_syntax.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,44 @@
909909
Traceback (most recent call last):
910910
SyntaxError: expected '('
911911
912+
Parenthesized arguments in function definitions
913+
914+
>>> def f(x, (y, z), w):
915+
... pass
916+
Traceback (most recent call last):
917+
SyntaxError: Function parameters cannot be parenthesized
918+
919+
>>> def f((x, y, z, w)):
920+
... pass
921+
Traceback (most recent call last):
922+
SyntaxError: Function parameters cannot be parenthesized
923+
924+
>>> def f(x, (y, z, w)):
925+
... pass
926+
Traceback (most recent call last):
927+
SyntaxError: Function parameters cannot be parenthesized
928+
929+
>>> def f((x, y, z), w):
930+
... pass
931+
Traceback (most recent call last):
932+
SyntaxError: Function parameters cannot be parenthesized
933+
934+
>>> lambda x, (y, z), w: None
935+
Traceback (most recent call last):
936+
SyntaxError: Lambda expression parameters cannot be parenthesized
937+
938+
>>> lambda (x, y, z, w): None
939+
Traceback (most recent call last):
940+
SyntaxError: Lambda expression parameters cannot be parenthesized
941+
942+
>>> lambda x, (y, z, w): None
943+
Traceback (most recent call last):
944+
SyntaxError: Lambda expression parameters cannot be parenthesized
945+
946+
>>> lambda (x, y, z), w: None
947+
Traceback (most recent call last):
948+
SyntaxError: Lambda expression parameters cannot be parenthesized
949+
912950
Custom error messages for try blocks that are not followed by except/finally
913951
914952
>>> try:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve the syntax error message for parenthesized arguments. Patch by Pablo
2+
Galindo.

0 commit comments

Comments
 (0)