classification
Title: Improve syntax error for parenthesized arguments
Type: Stage: patch review
Components: Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: aroberge, josh.r, pablogsal, terry.reedy
Priority: normal Keywords: patch

Created on 2021-10-12 17:20 by pablogsal, last changed 2021-10-19 16:49 by pablogsal.

Pull Requests
URL Status Linked Edit
PR 28906 open pablogsal, 2021-10-12 17:23
Messages (8)
msg403747 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-10-12 17:20
There is a non-trivial ammount of users that try to parenthesize lambda parameters:

>>> lambda (x,y,z): x+y+z
  File "<stdin>", line 1
    lambda (x,y,z): x+y+z
           ^
SyntaxError: invalid syntax

We should improve the error message in this case
msg403748 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-10-12 17:21
Example:

>>> lambda (x,y,z): x+y+z
  File "<stdin>", line 1
    lambda (x,y,z): x+y+z
           ^^^^^^^
SyntaxError: Function parameters cannot be parenthesized
msg403757 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2021-10-12 21:01
Why not "lambda parameters cannot be parenthesized" (optionally "lambda function")? def-ed function parameters are parenthesized, so just saying "Function parameters cannot be parenthesized" seems very weird.
msg403762 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-10-12 22:14
> so just saying "Function parameters cannot be parenthesized" seems very weird.

I agree that the wording can be improved, but is not weird, it has the same problem in functions:

>>> def foo(x,y,(z,w),k):
  File "<stdin>", line 1
    def foo(x,y,(z,w),k):
                ^
SyntaxError: invalid syntax
msg403763 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-10-12 22:15
With PR 28906 it will show:

>>> def foo(x,y,(z,w),k):
  File "<stdin>", line 1
    def foo(x,y,(z,w),k):
                ^^^^^
SyntaxError: Function parameters cannot be parenthesized

I am happy to take suggestions for the wording
msg403764 - (view) Author: Andre Roberge (aroberge) * Date: 2021-10-12 22:28
+1 on adding better error messages for these cases. I also agree with having different explanations with lambda and def.

Below is what I have with friendly-traceback: perhaps the first line of both of these might be suitable?   (I will likely change the first line in each case to be as similar to what you end up with.)

>>> set_include("why")
>>> lambda (x, y): x + y

    `lambda` does not allow parentheses around its arguments.
    This was allowed in Python 2 but it not allowed in Python 3.

>>> def foo(x, (y, z), w):

    You cannot have explicit tuples as function arguments.
    You can only use identifiers (variable names) as function arguments.
    Assign any tuple to a parameter and unpack it
    within the body of the function.
msg404074 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-10-16 11:59
I suggest "Lambda expression parameters ...".  Keep "Function parameters ..." for the other.

Note that the first error is adding invalid outer parens, whereas the second is adding inner parens for sublist.
msg404318 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-10-19 16:49
> Note that the first error is adding invalid outer parens, whereas the second is adding inner parens for sublist.

Yeah, but you can also write:

lambda x, (y, z), w:  None

So is kind of the same error
History
Date User Action Args
2021-10-19 16:49:58pablogsalsetmessages: + msg404318
2021-10-16 11:59:45terry.reedysetnosy: + terry.reedy
messages: + msg404074
2021-10-12 22:28:28arobergesetmessages: + msg403764
2021-10-12 22:15:25pablogsalsetmessages: + msg403763
2021-10-12 22:14:55pablogsalsetmessages: + msg403762
2021-10-12 21:01:49josh.rsetnosy: + josh.r
messages: + msg403757
2021-10-12 17:30:37arobergesetnosy: + aroberge
2021-10-12 17:23:01pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request27197
2021-10-12 17:21:19pablogsalsetmessages: + msg403748
2021-10-12 17:20:57pablogsalcreate