Skip to content
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

Improve syntax error for parenthesized arguments #89613

Closed
pablogsal opened this issue Oct 12, 2021 · 9 comments
Closed

Improve syntax error for parenthesized arguments #89613

pablogsal opened this issue Oct 12, 2021 · 9 comments

Comments

@pablogsal
Copy link
Member

BPO 45450
Nosy @terryjreedy, @aroberge, @MojoVampire, @pablogsal
PRs
  • bpo-45450: Improve syntax error for parenthesized arguments #28906
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-11-20.18:27:49.879>
    created_at = <Date 2021-10-12.17:20:57.605>
    labels = []
    title = 'Improve syntax error for parenthesized arguments'
    updated_at = <Date 2021-11-20.18:27:50.755>
    user = 'https://github.com/pablogsal'

    bugs.python.org fields:

    activity = <Date 2021-11-20.18:27:50.755>
    actor = 'pablogsal'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-11-20.18:27:49.879>
    closer = 'pablogsal'
    components = []
    creation = <Date 2021-10-12.17:20:57.605>
    creator = 'pablogsal'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45450
    keywords = ['patch']
    message_count = 9.0
    messages = ['403747', '403748', '403757', '403762', '403763', '403764', '404074', '404318', '406681']
    nosy_count = 4.0
    nosy_names = ['terry.reedy', 'aroberge', 'josh.r', 'pablogsal']
    pr_nums = ['28906']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue45450'
    versions = []

    @pablogsal
    Copy link
    Member Author

    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

    @pablogsal
    Copy link
    Member Author

    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

    @MojoVampire
    Copy link
    Mannequin

    MojoVampire mannequin commented Oct 12, 2021

    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.

    @pablogsal
    Copy link
    Member Author

    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

    @pablogsal
    Copy link
    Member Author

    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

    @aroberge
    Copy link
    Mannequin

    aroberge mannequin commented Oct 12, 2021

    +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.
    

    @terryjreedy
    Copy link
    Member

    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.

    @pablogsal
    Copy link
    Member Author

    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

    @pablogsal
    Copy link
    Member Author

    New changeset 7a1d932 by Pablo Galindo Salgado in branch 'main':
    bpo-45450: Improve syntax error for parenthesized arguments (GH-28906)
    7a1d932

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants