generated from jCloud/repository-template
style: limit line length to 79 characters
Limited the lengths of all lines to 79 characters (if possible). Additionally, some other lines were breaked for consistence.
This commit is contained in:
25 files changed
+6726
-1071
No files matched your search
@@ -17,7 +17,11 @@ from enum import Enum
|
||||
from typing import Union
|
||||
import ast
|
||||
from ...utils import assert_that_is_instance
|
||||
from ...exceptions import InvalidPythonIdentifierError, InvalidPythonAnnotationError, PythonArgumentStructureError
|
||||
from ...exceptions import (
|
||||
InvalidPythonAnnotationError,
|
||||
InvalidPythonIdentifierError,
|
||||
PythonArgumentStructureError
|
||||
)
|
||||
import keyword
|
||||
import types
|
||||
|
||||
@@ -48,7 +52,13 @@ class PythonFunctionArgument:
|
||||
:type annotation: Union[str, None]
|
||||
'''
|
||||
|
||||
def __init__(self, name: str, kind: PythonArgumentKind, default: Union[str, None], annotation: Union[str, None]) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
kind: PythonArgumentKind,
|
||||
default: Union[str, None],
|
||||
annotation: Union[str, None]
|
||||
) -> None:
|
||||
assert_that_is_instance(name, str)
|
||||
assert_that_is_instance(kind, PythonArgumentKind)
|
||||
assert_that_is_instance(default, (str, types.NoneType))
|
||||
@@ -56,26 +66,43 @@ class PythonFunctionArgument:
|
||||
|
||||
# check whether name is a valid identifier
|
||||
if not name or not name.isidentifier() or keyword.iskeyword(name):
|
||||
raise InvalidPythonIdentifierError('invalid identifier', identifier = name)
|
||||
raise InvalidPythonIdentifierError(
|
||||
'invalid identifier',
|
||||
identifier = name
|
||||
)
|
||||
|
||||
# check whether the annotation is a valid annotation
|
||||
# Currently, it is only checked whether the parameter is not
|
||||
# empty.
|
||||
if annotation is not None and not annotation:
|
||||
raise InvalidPythonAnnotationError('invalid annotation', annotation = annotation)
|
||||
raise InvalidPythonAnnotationError(
|
||||
'invalid annotation',
|
||||
annotation = annotation
|
||||
)
|
||||
|
||||
self.name = name
|
||||
self.kind = kind
|
||||
self.default = default
|
||||
self.annotation = annotation
|
||||
|
||||
if self.kind in (PythonArgumentKind.VARARG, PythonArgumentKind.KWARGS) and self.default is not None:
|
||||
raise PythonArgumentStructureError('* or ** arguments cannot have default values', definition = self.signature_repr())
|
||||
if self.kind in (
|
||||
PythonArgumentKind.VARARG,
|
||||
PythonArgumentKind.KWARGS
|
||||
) and self.default is not None:
|
||||
raise PythonArgumentStructureError(
|
||||
'* or ** arguments cannot have default values',
|
||||
definition = self.signature_repr()
|
||||
)
|
||||
|
||||
def __eq__(self, value: PythonFunctionArgument) -> bool:
|
||||
if not isinstance(value, PythonFunctionArgument):
|
||||
return False
|
||||
return self.name == value.name and self.kind == value.kind and self.default == value.default and self.annotation == value.annotation
|
||||
return (
|
||||
self.name == value.name
|
||||
and self.kind == value.kind
|
||||
and self.default == value.default
|
||||
and self.annotation == value.annotation
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return type(self).__name__ + repr((
|
||||
@@ -131,43 +158,128 @@ 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]
|
||||
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, defaults[i], None))
|
||||
arguments.append(
|
||||
PythonFunctionArgument(
|
||||
arg.arg,
|
||||
PythonArgumentKind.POSITIONAL_ONLY,
|
||||
defaults[i],
|
||||
None
|
||||
)
|
||||
)
|
||||
else:
|
||||
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.POSITIONAL_ONLY, defaults[i], 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, defaults[i], None))
|
||||
arguments.append(
|
||||
PythonFunctionArgument(
|
||||
arg.arg,
|
||||
PythonArgumentKind.NORMAL,
|
||||
defaults[i],
|
||||
None
|
||||
)
|
||||
)
|
||||
else:
|
||||
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.NORMAL, defaults[i], 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))
|
||||
arguments.append(
|
||||
PythonFunctionArgument(
|
||||
self.ast_arguments_list.vararg.arg,
|
||||
PythonArgumentKind.VARARG,
|
||||
None,
|
||||
None
|
||||
)
|
||||
)
|
||||
else:
|
||||
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:
|
||||
if arg.annotation is None:
|
||||
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.KEYWORD_ONLY, defaults[i], None))
|
||||
arguments.append(
|
||||
PythonFunctionArgument(
|
||||
arg.arg,
|
||||
PythonArgumentKind.KEYWORD_ONLY,
|
||||
defaults[i],
|
||||
None
|
||||
)
|
||||
)
|
||||
else:
|
||||
arguments.append(PythonFunctionArgument(arg.arg, PythonArgumentKind.KEYWORD_ONLY, defaults[i], 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))
|
||||
arguments.append(
|
||||
PythonFunctionArgument(
|
||||
self.ast_arguments_list.kwarg.arg,
|
||||
PythonArgumentKind.KWARGS,
|
||||
None,
|
||||
None
|
||||
)
|
||||
)
|
||||
else:
|
||||
arguments.append(PythonFunctionArgument(self.ast_arguments_list.kwarg.arg, PythonArgumentKind.KWARGS, None, ast.unparse(self.ast_arguments_list.kwarg.annotation)))
|
||||
arguments.append(
|
||||
PythonFunctionArgument(
|
||||
self.ast_arguments_list.kwarg.arg,
|
||||
PythonArgumentKind.KWARGS,
|
||||
None,
|
||||
ast.unparse(self.ast_arguments_list.kwarg.annotation)
|
||||
)
|
||||
)
|
||||
|
||||
return arguments
|
||||
Reference in new issue
Block a user