python / cpython

The Python programming language
https://www.python.org
Other
63.36k stars 30.34k forks source link

Function definition grammar is incorrect #122845

Open skirpichev opened 2 months ago

skirpichev commented 2 months ago

https://github.com/python/cpython/blob/2f5c3b09e45798a18d60841d04a165fb062be666/Doc/reference/compound_stmts.rst?plain=1#L1211-L1224

In parameter_list_starargs - everything can be optional, except for *, and that permits incorrect function definitions like def foo(*):

>>> def foo(*): pass
  File "<stdin>", line 1
    def foo(*): pass
            ^
SyntaxError: named arguments must follow bare *

See d.p.o thread https://discuss.python.org/t/56998. I'll provide a patch, based on suggested solution.

CC @norpadon

Linked PRs

mashkoor098 commented 2 months ago

def foo(*args): pass

skirpichev commented 2 months ago

@mashkoor098, not sure I get your point.

def foo(*args):
    pass

is a correct syntax, yes. Another example:

def bar(*, spam='spam!'):
    pass