-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Comments
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 |
I also sometimes write def foo(pos_only, /*, kwarg): pass. Perhaps this can be special cased with |
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! |
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. |
@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". |
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) |
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:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: