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

Parameters and arguments parser syntax error improvments #90994

Closed
Andykl mannequin opened this issue Feb 23, 2022 · 7 comments
Closed

Parameters and arguments parser syntax error improvments #90994

Andykl mannequin opened this issue Feb 23, 2022 · 7 comments
Labels
3.11 bug and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@Andykl
Copy link
Mannequin

Andykl mannequin commented Feb 23, 2022

BPO 46838
Nosy @terryjreedy, @aroberge, @lysnikolaou, @pablogsal, @Andykl
PRs
  • bpo-46838: Syntax error improvements for function definitions #31590
  • 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 = None
    created_at = <Date 2022-02-23.21:23:21.121>
    labels = ['interpreter-core', 'type-feature', '3.11']
    title = 'Parameters and arguments parser syntax error improvments'
    updated_at = <Date 2022-03-22.11:39:09.698>
    user = 'https://github.com/Andykl'
    

    bugs.python.org fields:

    activity = <Date 2022-03-22.11:39:09.698>
    actor = 'pablogsal'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Parser']
    creation = <Date 2022-02-23.21:23:21.121>
    creator = 'Andy_kl'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46838
    keywords = ['patch']
    message_count = 6.0
    messages = ['413856', '413858', '413860', '414038', '414052', '415760']
    nosy_count = 5.0
    nosy_names = ['terry.reedy', 'aroberge', 'lys.nikolaou', 'pablogsal', 'Andy_kl']
    pr_nums = ['31590']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue46838'
    versions = ['Python 3.11']
    

    Linked PRs

    @Andykl
    Copy link
    Mannequin Author

    Andykl mannequin commented Feb 23, 2022

    I saw that pablogsal welcomed improvments to the parser's suggestions, so here are the messages for parameters and arguments lists I think should be written instead of the current generic "invalid syntax".

    >>> def foo(*arg, *arg): pass
    SyntaxError: * argument may appear only once
    
    >>> def foo(**arg, **arg): pass
    SyntaxError: ** argument may appear only once
    
    >>> def foo(arg1, /, arg2, /, arg3): pass
    SyntaxError: / may appear only once
    
    >>> def foo(*args, /, arg): pass
    SyntaxError: / must be ahead of *
    
    >>> def foo(/, arg): pass
    SyntaxError: at least one argument must precede /
    
    >>> def foo(arg=): pass
    SyntaxError: expected default value expression
    
    >>> def foo(*args=None): pass
    SyntaxError: * argument cannot have default value
    
    >>> def foo(**kwargs=None): pass
    SyntaxError: ** argument cannot have default value
    
    >>> foo(*args=[0])
    SyntaxError: cannot assign to iterable argument unpacking
    
    >>> foo(**args={"a": None})
    SyntaxError: cannot assign to keyword argument unpacking
    
    >>> foo(arg=)
    SyntaxError: expected argument value expression
    

    @Andykl Andykl mannequin added 3.11 bug and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Feb 23, 2022
    @Andykl
    Copy link
    Mannequin Author

    Andykl mannequin commented Feb 23, 2022

    I also sometimes write def foo(pos_only, /*, kwarg): pass. Perhaps this can be special cased with
    SyntaxError: expected comma between / and *

    @pablogsal
    Copy link
    Member

    Thanks a lot for the suggestions! I will try to give these a go to see if we can get them implemented. Parameter parsing is a bit hairy so not sure how lucky we will be but all of them make sense!

    @terryjreedy
    Copy link
    Member

    With two exceptions, nice suggestions if feasible.

    >>> def foo(*args=None): pass
    SyntaxError: * argument cannot have default value
    
    >>> def foo(**kwargs=None): pass
    SyntaxError: ** argument cannot have default value
    

    Good.

    >>> foo(*args=[0])
    SyntaxError: cannot assign to iterable argument unpacking
    
    >>> foo(**args={"a": None})
    SyntaxError: cannot assign to keyword argument unpacking
    

    Incomprehensible. It seems to me that these should have same message as first two; message should not depend on proposed default.

    @Andykl
    Copy link
    Mannequin Author

    Andykl mannequin commented Feb 26, 2022

    @terry.reedy, I based that error message on current

    >>> foo(**{}, *())
    SyntaxError: iterable argument unpacking follows keyword argument unpacking
    

    and

    >>> foo(__debug__=True)
    SyntaxError: cannot assign to __debug__
    

    but the final error message could be anything if it explicitly says "what's wrong".

    @pablogsal
    Copy link
    Member

    New changeset 7d810b6 by Pablo Galindo Salgado in branch 'main':
    bpo-46838: Syntax error improvements for function definitions (GH-31590)
    7d810b6

    @hauntsaninja
    Copy link
    Contributor

    Thanks Andykl for the suggestion and pablogsal and lysnikolaou for implementing. I think all of the suggestions have now been implemented, so this can be closed.

    Also linking Pablo's PR #31590 (since it linked the bpo)

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 bug and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants