From 3bec1a69389b70fe09bca12ff14453f5b26e1be0 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Wed, 22 May 2019 20:18:35 +0200 Subject: [PATCH] Better signature generation --- jedi/evaluate/names.py | 18 ++++++++++++++++-- jedi/evaluate/signature.py | 18 ++++++------------ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/jedi/evaluate/names.py b/jedi/evaluate/names.py index 23d7d79f..085dbefe 100644 --- a/jedi/evaluate/names.py +++ b/jedi/evaluate/names.py @@ -122,10 +122,12 @@ class TreeNameDefinition(AbstractTreeName): class ParamNameInterface(object): - # annotation default?! def get_kind(self): raise NotImplementedError + def to_string(self): + raise NotImplementedError + class ParamName(AbstractTreeName, ParamNameInterface): api_type = u'param' @@ -134,8 +136,11 @@ class ParamName(AbstractTreeName, ParamNameInterface): self.parent_context = parent_context self.tree_name = tree_name + def _get_param_node(self): + return search_ancestor(self.tree_name, 'param') + 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 return Parameter.VAR_POSITIONAL if tree_param.star_count == 2: # **kwargs @@ -150,6 +155,15 @@ class ParamName(AbstractTreeName, ParamNameInterface): break 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): return self.get_param().infer() diff --git a/jedi/evaluate/signature.py b/jedi/evaluate/signature.py index d5e1d9db..321fc7a5 100644 --- a/jedi/evaluate/signature.py +++ b/jedi/evaluate/signature.py @@ -1,6 +1,3 @@ -from jedi.parser_utils import get_call_signature - - class AbstractSignature(object): def __init__(self, context, is_bound=False): self.context = context @@ -15,7 +12,12 @@ class AbstractSignature(object): return None 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): raise NotImplementedError @@ -39,14 +41,6 @@ class TreeSignature(AbstractSignature): def annotation(self): 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): @property