mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 16:34:47 +01:00
Add support for PEP 570 positional-only arguments
This commit is contained in:
parent
1f2b92bd32
commit
af9c5b59bd
2 changed files with 56 additions and 0 deletions
|
@ -1943,6 +1943,14 @@ def test_func_x_star_y_kwargs():
|
|||
check_stmts("def f(x, *, y, **kwargs):\n return 42")
|
||||
|
||||
|
||||
def test_func_x_divide():
|
||||
check_stmts("def f(x, /):\n return 42")
|
||||
|
||||
|
||||
def test_func_x_divide_y_star_z_kwargs():
|
||||
check_stmts("def f(x, /, y, *, z, **kwargs):\n return 42")
|
||||
|
||||
|
||||
def test_func_tx():
|
||||
check_stmts("def f(x:int):\n return x")
|
||||
|
||||
|
|
|
@ -51,6 +51,31 @@ class Parser(ThreeSixParser):
|
|||
outputdir=outputdir,
|
||||
)
|
||||
|
||||
def _set_posonly_args_def(self, argmts, vals):
|
||||
for v in vals:
|
||||
argmts.posonlyargs.append(v["arg"])
|
||||
d = v["default"]
|
||||
if d is not None:
|
||||
argmts.defaults.append(d)
|
||||
|
||||
def _set_posonly_args(self, p0, p1, p2, p3):
|
||||
if p2 is None and p3 is None:
|
||||
# x
|
||||
p0.posonlyargs.append(p1)
|
||||
elif p2 is not None and p3 is None:
|
||||
# x=42
|
||||
p0.posonlyargs.append(p1)
|
||||
p0.defaults.append(p2)
|
||||
elif p2 is None and p3 is not None:
|
||||
# x, y and x, y=42
|
||||
p0.posonlyargs.append(p1)
|
||||
self._set_posonly_args_def(p0, p3)
|
||||
else:
|
||||
# x=42, y=42
|
||||
p0.posonlyargs.append(p1)
|
||||
p0.defaults.append(p2)
|
||||
self._set_posonly_args_def(p0, p3)
|
||||
|
||||
def p_parameters(self, p):
|
||||
"""parameters : LPAREN typedargslist_opt RPAREN"""
|
||||
p2 = p[2]
|
||||
|
@ -214,6 +239,29 @@ class Parser(ThreeSixParser):
|
|||
self._set_var_args(p0, p[6], p[7])
|
||||
p[0] = p0
|
||||
|
||||
def p_typedargslist_t12(self, p):
|
||||
"""typedargslist : posonlyargslist typedargslist_opt"""
|
||||
if p[2] is None:
|
||||
p0 = p[1]
|
||||
else:
|
||||
p0 = p[2]
|
||||
p0.posonlyargs = p[1].posonlyargs
|
||||
p[0] = p0
|
||||
|
||||
def p_posonlyargslist(self, p):
|
||||
"""posonlyargslist : tfpdef equals_test_opt comma_tfpdef_list_opt DIVIDE comma_opt"""
|
||||
p0 = ast.arguments(
|
||||
posonlyargs=[],
|
||||
args=[],
|
||||
vararg=None,
|
||||
kwonlyargs=[],
|
||||
kw_defaults=[],
|
||||
kwarg=None,
|
||||
defaults=[],
|
||||
)
|
||||
self._set_posonly_args(p0, p[1], p[2], p[3])
|
||||
p[0] = p0
|
||||
|
||||
def p_varargslist_kwargs(self, p):
|
||||
"""varargslist : POW vfpdef"""
|
||||
p[0] = ast.arguments(
|
||||
|
|
Loading…
Add table
Reference in a new issue