generated from jCloud/repository-template
Add feature to get the signature representation of a Python function argument
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -60,4 +60,20 @@ class PythonAnnotationError(ValueError):
|
||||
else:
|
||||
return f'{self.args[0]}{": " if self.annotation and self.args[0] else ""}{self.annotation if self.args[0] else ""}'
|
||||
|
||||
class InvalidPythonAnnotationError(PythonAnnotationError): ...
|
||||
class InvalidPythonAnnotationError(PythonAnnotationError): ...
|
||||
|
||||
class PythonDefinitionError(ValueError):
|
||||
'''
|
||||
Base class for Python definition errors.
|
||||
'''
|
||||
def __init__(self, *args: object, definition: str = '') -> None:
|
||||
super().__init__(*args)
|
||||
self.definition = definition
|
||||
|
||||
def __str__(self):
|
||||
if not self.args:
|
||||
return ''
|
||||
else:
|
||||
return f'{self.args[0]}{": " if self.definition and self.args[0] else ""}{self.definition if self.args[0] else ""}'
|
||||
|
||||
class PythonArgumentStructureError(PythonDefinitionError): ...
|
||||
Reference in New Issue
Block a user