This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Improve syntax error for parenthesized arguments
Type: Stage: resolved
Components: Versions:
process
Status: closed Resolution: fixed
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 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28906 merged pablogsal, 2021-10-12 17:23
Messages (9)
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
msg406681 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-11-20 18:27
New changeset 7a1d9325287a39528b795b1e8037146777abfe3e by Pablo Galindo Salgado in branch 'main':
bpo-45450: Improve syntax error for parenthesized arguments (GH-28906)
https://github.com/python/cpython/commit/7a1d9325287a39528b795b1e8037146777abfe3e
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89613
2021-11-20 18:27:50pablogsalsetmessages: + msg406681
2021-11-20 18:27:49pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
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