[issue16545] ast.FunctionDef sets a bad value for kw_defaults when keyword-only arguments present

Daniel Urban report at bugs.python.org
Sat Nov 24 12:51:17 CET 2012


Daniel Urban added the comment:

If I understand correctly, the invariant is that len(kw_defaults) == len(kwonlyargs). I think the reason is that the following is valid syntax (an argument without a default after one with a default):

>>> def f(*, a=0, b): pass
... 
>>>

And None is used as a placeholder in the kw_defaults list:
>>> ast.dump(ast.parse('def f(*, a=0, b): pass'))
"Module(body=[FunctionDef(name='f', args=arguments(args=[], vararg=None, varargannotation=None, kwonlyargs=[arg(arg='a', annotation=None), arg(arg='b', annotation=None)], kwarg=None, kwargannotation=None, defaults=[], kw_defaults=[Num(n=0), None]), body=[Pass()], decorator_list=[], returns=None)])"
>>>

So it seems to me, that this behavior is intentional. (Also, it works no differently in 3.2.)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16545>
_______________________________________


More information about the Python-bugs-list mailing list