Better signature generation

This commit is contained in:
Dave Halter
2019-05-22 20:18:35 +02:00
parent 9bb88b43ca
commit 3bec1a6938
2 changed files with 22 additions and 14 deletions

View File

@@ -122,10 +122,12 @@ class TreeNameDefinition(AbstractTreeName):
class ParamNameInterface(object): class ParamNameInterface(object):
# annotation default?!
def get_kind(self): def get_kind(self):
raise NotImplementedError raise NotImplementedError
def to_string(self):
raise NotImplementedError
class ParamName(AbstractTreeName, ParamNameInterface): class ParamName(AbstractTreeName, ParamNameInterface):
api_type = u'param' api_type = u'param'
@@ -134,8 +136,11 @@ class ParamName(AbstractTreeName, ParamNameInterface):
self.parent_context = parent_context self.parent_context = parent_context
self.tree_name = tree_name self.tree_name = tree_name
def _get_param_node(self):
return search_ancestor(self.tree_name, 'param')
def get_kind(self): def get_kind(self):
tree_param = search_ancestor(self.tree_name, 'param') tree_param = self._get_param_node()
if tree_param.star_count == 1: # *args if tree_param.star_count == 1: # *args
return Parameter.VAR_POSITIONAL return Parameter.VAR_POSITIONAL
if tree_param.star_count == 2: # **kwargs if tree_param.star_count == 2: # **kwargs
@@ -150,6 +155,15 @@ class ParamName(AbstractTreeName, ParamNameInterface):
break break
return Parameter.POSITIONAL_OR_KEYWORD return Parameter.POSITIONAL_OR_KEYWORD
def to_string(self):
output = self.string_name
param_node = self._get_param_node()
if param_node.annotation is not None:
output += ': ' + param_node.annotation.get_code(include_prefix=False)
if param_node.default is not None:
output += '=' + param_node.default.get_code(include_prefix=False)
return output
def infer(self): def infer(self):
return self.get_param().infer() return self.get_param().infer()

View File

@@ -1,6 +1,3 @@
from jedi.parser_utils import get_call_signature
class AbstractSignature(object): class AbstractSignature(object):
def __init__(self, context, is_bound=False): def __init__(self, context, is_bound=False):
self.context = context self.context = context
@@ -15,7 +12,12 @@ class AbstractSignature(object):
return None return None
def to_string(self): def to_string(self):
raise NotImplementedError param_code = ', '.join(n.to_string() for n in self.get_param_names())
s = self.name.string_name + '(' + param_code + ')'
annotation = self.annotation
if annotation is not None:
s += ' -> ' + annotation.get_code(include_prefix=False)
return s
def bind(self, context): def bind(self, context):
raise NotImplementedError raise NotImplementedError
@@ -39,14 +41,6 @@ class TreeSignature(AbstractSignature):
def annotation(self): def annotation(self):
return self._function_context.tree_node.annotation return self._function_context.tree_node.annotation
def to_string(self, normalize=False):
return get_call_signature(
self._function_context.tree_node,
call_string=self.name.string_name,
omit_first_param=self.is_bound,
omit_return_annotation=self.context.is_class(),
)
class BuiltinSignature(AbstractSignature): class BuiltinSignature(AbstractSignature):
@property @property