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: Parameters and arguments parser syntax error improvments
Type: enhancement Stage: patch review
Components: Parser Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Andy_kl, aroberge, lys.nikolaou, pablogsal, terry.reedy
Priority: normal Keywords: patch

Created on 2022-02-23 21:23 by Andy_kl, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 31590 merged pablogsal, 2022-02-25 22:42
Messages (6)
msg413856 - (view) Author: Andrej Klychin (Andy_kl) Date: 2022-02-23 21:23
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
msg413858 - (view) Author: Andrej Klychin (Andy_kl) Date: 2022-02-23 21:31
I also sometimes write def foo(pos_only, /*, kwarg): pass. Perhaps this can be special cased with 
SyntaxError: expected comma between / and *
msg413860 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2022-02-23 21:46
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!
msg414038 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2022-02-25 20:45
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.
msg414052 - (view) Author: Andrej Klychin (Andy_kl) Date: 2022-02-26 00:30
@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".
msg415760 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2022-03-22 11:39
New changeset 7d810b6a4eab6eba689acc5bb05f85515478d690 by Pablo Galindo Salgado in branch 'main':
bpo-46838: Syntax error improvements for function definitions (GH-31590)
https://github.com/python/cpython/commit/7d810b6a4eab6eba689acc5bb05f85515478d690
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 90994
2022-03-22 11:39:09pablogsalsetmessages: + msg415760
2022-02-26 00:30:09Andy_klsetmessages: + msg414052
2022-02-26 00:26:19arobergesetnosy: + aroberge
2022-02-25 22:42:54pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request29713
2022-02-25 20:45:07terry.reedysetnosy: + terry.reedy
messages: + msg414038
2022-02-23 21:46:37pablogsalsetmessages: + msg413860
2022-02-23 21:31:02Andy_klsetmessages: + msg413858
2022-02-23 21:23:21Andy_klcreate