generated from jCloud/repository-template
style: format source code using Ruff
This commit is contained in:
41 files changed
+6061
-5670
No files matched your search
@@ -31,6 +31,7 @@ __all__ = [
|
||||
'PythonASTArgumentsListParser',
|
||||
]
|
||||
|
||||
|
||||
class PythonArgumentKind(Enum):
|
||||
NORMAL = 0
|
||||
POSITIONAL_ONLY = 1
|
||||
@@ -38,8 +39,9 @@ class PythonArgumentKind(Enum):
|
||||
VARARG = 3
|
||||
KWARGS = 4
|
||||
|
||||
|
||||
class PythonFunctionArgument:
|
||||
'''
|
||||
"""
|
||||
Represents an argument of a Python function.
|
||||
|
||||
:param name: The name of the argument.
|
||||
@@ -50,7 +52,7 @@ class PythonFunctionArgument:
|
||||
:type default: Union[str, None]
|
||||
:param annotation: The type annotation of the argument.
|
||||
:type annotation: Union[str, None]
|
||||
'''
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -68,7 +70,7 @@ class PythonFunctionArgument:
|
||||
if not name or not name.isidentifier() or keyword.iskeyword(name):
|
||||
raise InvalidPythonIdentifierError(
|
||||
'invalid identifier',
|
||||
identifier = name,
|
||||
identifier=name,
|
||||
)
|
||||
|
||||
# check whether the annotation is a valid annotation
|
||||
@@ -77,7 +79,7 @@ class PythonFunctionArgument:
|
||||
if annotation is not None and not annotation:
|
||||
raise InvalidPythonAnnotationError(
|
||||
'invalid annotation',
|
||||
annotation = annotation,
|
||||
annotation=annotation,
|
||||
)
|
||||
|
||||
self.name = name
|
||||
@@ -85,13 +87,17 @@ class PythonFunctionArgument:
|
||||
self.default = default
|
||||
self.annotation = annotation
|
||||
|
||||
if self.kind in (
|
||||
PythonArgumentKind.VARARG,
|
||||
PythonArgumentKind.KWARGS,
|
||||
) and self.default is not None:
|
||||
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(),
|
||||
definition=self.signature_repr(),
|
||||
)
|
||||
|
||||
def __eq__(self, value: PythonFunctionArgument) -> bool:
|
||||
@@ -105,20 +111,22 @@ class PythonFunctionArgument:
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return type(self).__name__ + repr((
|
||||
self.name,
|
||||
self.kind,
|
||||
self.default,
|
||||
self.annotation,
|
||||
))
|
||||
return type(self).__name__ + repr(
|
||||
(
|
||||
self.name,
|
||||
self.kind,
|
||||
self.default,
|
||||
self.annotation,
|
||||
)
|
||||
)
|
||||
|
||||
def signature_repr(self) -> str:
|
||||
'''
|
||||
"""
|
||||
Returns the function signature representation of the argument.
|
||||
|
||||
:return: The string representation of the argument.
|
||||
:rtype: str
|
||||
'''
|
||||
"""
|
||||
|
||||
if self.kind == PythonArgumentKind.KWARGS:
|
||||
str_repr = '**'
|
||||
@@ -137,48 +145,44 @@ class PythonFunctionArgument:
|
||||
|
||||
return str_repr
|
||||
|
||||
|
||||
class PythonASTArgumentsListParser:
|
||||
'''
|
||||
"""
|
||||
A parser for making ``PythonFunctionArgument`` objects from an
|
||||
``ast.arguments`` arguments list.
|
||||
|
||||
:param ast_arguments_list: The ``ast.arguments`` arguments list.
|
||||
:type ast_arguments_list: ast.arguments
|
||||
'''
|
||||
"""
|
||||
|
||||
def __init__(self, ast_arguments_list: ast.arguments) -> None:
|
||||
def __init__(self, ast_arguments_list: ast.arguments) -> None:
|
||||
self.ast_arguments_list = ast_arguments_list
|
||||
|
||||
def to_argument_list(self) -> list[PythonFunctionArgument]:
|
||||
'''
|
||||
"""
|
||||
Converts the AST arguments list to a list of Python function
|
||||
argument objects.
|
||||
|
||||
:return: The list of Python function argument objects.
|
||||
: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
|
||||
))
|
||||
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)
|
||||
for d in self.ast_arguments_list.defaults
|
||||
]
|
||||
+ [
|
||||
ast.unparse(d)
|
||||
if d is not None
|
||||
else None
|
||||
ast.unparse(d) if d is not None else None
|
||||
for d in self.ast_arguments_list.kw_defaults
|
||||
]
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user