generated from jCloud/repository-template
Add feature to get the signature representation of a Python function argument
This commit is contained in:
4 files changed
+86
-8
No files matched your search
@@ -17,7 +17,7 @@ from enum import Enum
|
||||
from typing import Union
|
||||
import ast
|
||||
from ...utils import assert_that_is_instance
|
||||
from ...exceptions import InvalidPythonIdentifierError, InvalidPythonAnnotationError
|
||||
from ...exceptions import InvalidPythonIdentifierError, InvalidPythonAnnotationError, PythonArgumentStructureError
|
||||
import keyword
|
||||
import types
|
||||
|
||||
@@ -68,6 +68,9 @@ class PythonFunctionArgument:
|
||||
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())
|
||||
|
||||
def __eq__(self, value: PythonFunctionArgument) -> bool:
|
||||
if not isinstance(value, PythonFunctionArgument):
|
||||
@@ -82,6 +85,31 @@ class PythonFunctionArgument:
|
||||
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 = '**'
|
||||
elif self.kind == PythonArgumentKind.VARARG:
|
||||
str_repr = '*'
|
||||
else:
|
||||
str_repr = ''
|
||||
|
||||
str_repr += self.name
|
||||
|
||||
if self.annotation is not None:
|
||||
str_repr += ': ' + self.annotation
|
||||
|
||||
if self.default is not None:
|
||||
str_repr += ' = ' + self.default
|
||||
|
||||
return str_repr
|
||||
|
||||
class PythonASTArgumentsListParser:
|
||||
'''
|
||||
A parser for making ``PythonFunctionArgument`` objects from an
|
||||
|
||||
Reference in new issue
Block a user