Bug fix: add support for default values in core.python.arguments.PythonASTArgumentsListParser.to_argument_list

This commit is contained in:
2026-04-12 19:30:04 +02:00
parent e4fe88b60b
commit ed3b47bd81
2 changed files with 85 additions and 6 deletions
+18 -6
View File
@@ -103,17 +103,28 @@ class PythonASTArgumentsListParser:
: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 = []
for arg in self.ast_arguments_list.posonlyargs:
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:
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:
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:
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.annotation is 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)))
for arg in self.ast_arguments_list.kwonlyargs:
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:
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.annotation is 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('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):
assert PythonASTArgumentsListParser(ast_arguments_list).to_argument_list() == expected