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
ast.arguments has confusing args/posonlyargs ordering #81774
Comments
Positional-only arguments come before position-or-keyword arguments. def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2): However, the posonlyargs are defined to come after args in the AST: arguments = (arg* args, arg* posonlyargs, arg? vararg, arg* kwonlyargs,
expr* kw_defaults, arg? kwarg, expr* defaults) which results in confusing ast.dump output because they share defaults: >>> r = ast.parse('lambda a=1,/,b=2:a+b', mode='eval')
>>> ast.dump(r.body.args)
"arguments(
args=[arg(arg='b', annotation=None, type_comment=None)],
posonlyargs=[arg(arg='a', annotation=None, type_comment=None)],
vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None,
defaults=[Constant(value=1, kind=None), Constant(value=2, kind=None)])"
[manually prettified] Note how the ordering is 'args b', then 'posonlyargs a', but the defaults are still 1 then 2. This can be confusing to someone building an ast.arguments using keywords because the elements in 'defaults' have to be supplied in a specific order, but the keyword args 'args' and 'posonlyargs' do not, or to someone building an ast.arguments using positional arguments (because, maybe ironically, they're not keyword-only arguments) because 'posonlyargs' and 'args' must be supplied in a different order than the ordering of elements in 'defaults' would imply. Potential solutions:
|
Thanks for the report, Benjamin! |
The patch that was merged to fix this may have caused a regresion in xonsh. Am working on a reproducer. |
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:
The text was updated successfully, but these errors were encountered: