generated from jCloud/repository-template
Bug fix: add support for default values in core.python.arguments.PythonASTArgumentsListParser.to_argument_list
This commit is contained in:
@@ -103,17 +103,28 @@ class PythonASTArgumentsListParser:
|
|||||||
:rtype: list[PythonFunctionArgument]
|
:rtype: list[PythonFunctionArgument]
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
defaults = [None for _ in range(len(
|
||||||
|
self.ast_arguments_list.posonlyargs +
|
||||||
|
self.ast_arguments_list.args +
|
||||||
|
self.ast_arguments_list.kwonlyargs
|
||||||
|
) - len(self.ast_arguments_list.defaults) - len(self.ast_arguments_list.kw_defaults))] + \
|
||||||
|
[ast.unparse(d) for d in self.ast_arguments_list.defaults] + \
|
||||||
|
[ast.unparse(d) if d is not None else None for d in self.ast_arguments_list.kw_defaults]
|
||||||
|
|
||||||
|
i = 0
|
||||||
arguments = []
|
arguments = []
|
||||||
for arg in self.ast_arguments_list.posonlyargs:
|
for arg in self.ast_arguments_list.posonlyargs:
|
||||||
if arg.annotation is None:
|
if arg.annotation is None:
|
||||||
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.POSITIONAL_ONLY, None, None))
|
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.POSITIONAL_ONLY, defaults[i], None))
|
||||||
else:
|
else:
|
||||||
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.POSITIONAL_ONLY, None, ast.unparse(arg.annotation)))
|
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.POSITIONAL_ONLY, defaults[i], ast.unparse(arg.annotation)))
|
||||||
|
i += 1
|
||||||
for arg in self.ast_arguments_list.args:
|
for arg in self.ast_arguments_list.args:
|
||||||
if arg.annotation is None:
|
if arg.annotation is None:
|
||||||
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.NORMAL, None, None))
|
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.NORMAL, defaults[i], None))
|
||||||
else:
|
else:
|
||||||
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.NORMAL, None, ast.unparse(arg.annotation)))
|
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.NORMAL, defaults[i], ast.unparse(arg.annotation)))
|
||||||
|
i += 1
|
||||||
if self.ast_arguments_list.vararg is not None:
|
if self.ast_arguments_list.vararg is not None:
|
||||||
if self.ast_arguments_list.vararg.annotation is None:
|
if self.ast_arguments_list.vararg.annotation is None:
|
||||||
arguments.append(PythonFunctionArgument(self.ast_arguments_list.vararg.arg, PythonArgumentKind.VARARG, None, None))
|
arguments.append(PythonFunctionArgument(self.ast_arguments_list.vararg.arg, PythonArgumentKind.VARARG, None, None))
|
||||||
@@ -121,9 +132,10 @@ class PythonASTArgumentsListParser:
|
|||||||
arguments.append(PythonFunctionArgument(self.ast_arguments_list.vararg.arg, PythonArgumentKind.VARARG, None, ast.unparse(self.ast_arguments_list.vararg.annotation)))
|
arguments.append(PythonFunctionArgument(self.ast_arguments_list.vararg.arg, PythonArgumentKind.VARARG, None, ast.unparse(self.ast_arguments_list.vararg.annotation)))
|
||||||
for arg in self.ast_arguments_list.kwonlyargs:
|
for arg in self.ast_arguments_list.kwonlyargs:
|
||||||
if arg.annotation is None:
|
if arg.annotation is None:
|
||||||
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.KEYWORD_ONLY, None, None))
|
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.KEYWORD_ONLY, defaults[i], None))
|
||||||
else:
|
else:
|
||||||
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.KEYWORD_ONLY, None, ast.unparse(arg.annotation)))
|
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.KEYWORD_ONLY, defaults[i], ast.unparse(arg.annotation)))
|
||||||
|
i += 1
|
||||||
if self.ast_arguments_list.kwarg is not None:
|
if self.ast_arguments_list.kwarg is not None:
|
||||||
if self.ast_arguments_list.kwarg.annotation is None:
|
if self.ast_arguments_list.kwarg.annotation is None:
|
||||||
arguments.append(PythonFunctionArgument(self.ast_arguments_list.kwarg.arg, PythonArgumentKind.KWARGS, None, None))
|
arguments.append(PythonFunctionArgument(self.ast_arguments_list.kwarg.arg, PythonArgumentKind.KWARGS, None, None))
|
||||||
|
|||||||
@@ -159,6 +159,73 @@ def argument_list(argument_list: str) -> ast.arguments:
|
|||||||
PythonFunctionArgument('d', PythonArgumentKind.KEYWORD_ONLY, None, 'dict'),
|
PythonFunctionArgument('d', PythonArgumentKind.KEYWORD_ONLY, None, 'dict'),
|
||||||
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, 'set'),
|
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, 'set'),
|
||||||
]),
|
]),
|
||||||
|
(argument_list('a = 1, b = \'a\', /, *args, c = None, d = True, **kwargs'), [
|
||||||
|
PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, '1', None),
|
||||||
|
PythonFunctionArgument('b', PythonArgumentKind.POSITIONAL_ONLY, '\'a\'', None),
|
||||||
|
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||||
|
PythonFunctionArgument('c', PythonArgumentKind.KEYWORD_ONLY, 'None', None),
|
||||||
|
PythonFunctionArgument('d', PythonArgumentKind.KEYWORD_ONLY, 'True', None),
|
||||||
|
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||||
|
]),
|
||||||
|
(argument_list('a, b = \'a\', /, *args, c = None, d = True, **kwargs'), [
|
||||||
|
PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('b', PythonArgumentKind.POSITIONAL_ONLY, '\'a\'', None),
|
||||||
|
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||||
|
PythonFunctionArgument('c', PythonArgumentKind.KEYWORD_ONLY, 'None', None),
|
||||||
|
PythonFunctionArgument('d', PythonArgumentKind.KEYWORD_ONLY, 'True', None),
|
||||||
|
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||||
|
]),
|
||||||
|
(argument_list('a, b , /, *args, c = None, d = True, **kwargs'), [
|
||||||
|
PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('b', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||||
|
PythonFunctionArgument('c', PythonArgumentKind.KEYWORD_ONLY, 'None', None),
|
||||||
|
PythonFunctionArgument('d', PythonArgumentKind.KEYWORD_ONLY, 'True', None),
|
||||||
|
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||||
|
]),
|
||||||
|
(argument_list('a, b , /, *args, c, d = True, **kwargs'), [
|
||||||
|
PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('b', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||||
|
PythonFunctionArgument('c', PythonArgumentKind.KEYWORD_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('d', PythonArgumentKind.KEYWORD_ONLY, 'True', None),
|
||||||
|
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||||
|
]),
|
||||||
|
(argument_list('a, b , /, *args, c, d, **kwargs'), [
|
||||||
|
PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('b', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||||
|
PythonFunctionArgument('c', PythonArgumentKind.KEYWORD_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('d', PythonArgumentKind.KEYWORD_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||||
|
]),
|
||||||
|
(argument_list('a, b , /, *args, c = None, d, **kwargs'), [
|
||||||
|
PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('b', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||||
|
PythonFunctionArgument('c', PythonArgumentKind.KEYWORD_ONLY, 'None', None),
|
||||||
|
PythonFunctionArgument('d', PythonArgumentKind.KEYWORD_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||||
|
]),
|
||||||
|
(argument_list('a = 1, b , /, *args, c = None, d, **kwargs'), [
|
||||||
|
PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, '1', None),
|
||||||
|
PythonFunctionArgument('b', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||||
|
PythonFunctionArgument('c', PythonArgumentKind.KEYWORD_ONLY, 'None', None),
|
||||||
|
PythonFunctionArgument('d', PythonArgumentKind.KEYWORD_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||||
|
]),
|
||||||
|
(argument_list('a = 1, b , /, *args, c = None, d = False, **kwargs'), [
|
||||||
|
PythonFunctionArgument('a', PythonArgumentKind.POSITIONAL_ONLY, '1', None),
|
||||||
|
PythonFunctionArgument('b', PythonArgumentKind.POSITIONAL_ONLY, None, None),
|
||||||
|
PythonFunctionArgument('args', PythonArgumentKind.VARARG, None, None),
|
||||||
|
PythonFunctionArgument('c', PythonArgumentKind.KEYWORD_ONLY, 'None', None),
|
||||||
|
PythonFunctionArgument('d', PythonArgumentKind.KEYWORD_ONLY, 'False', None),
|
||||||
|
PythonFunctionArgument('kwargs', PythonArgumentKind.KWARGS, None, None),
|
||||||
|
]),
|
||||||
|
(argument_list('a = 1'), [
|
||||||
|
PythonFunctionArgument('a', PythonArgumentKind.NORMAL, '1', None)
|
||||||
|
]),
|
||||||
])
|
])
|
||||||
def test_PythonASTArgumentsListParser_to_argument_list(ast_arguments_list, expected):
|
def test_PythonASTArgumentsListParser_to_argument_list(ast_arguments_list, expected):
|
||||||
assert PythonASTArgumentsListParser(ast_arguments_list).to_argument_list() == expected
|
assert PythonASTArgumentsListParser(ast_arguments_list).to_argument_list() == expected
|
||||||
Reference in New Issue
Block a user