1
0
forked from VimPlug/jedi

A work in progress improvement for compiled signatures

This commit is contained in:
Dave Halter
2019-05-21 09:37:17 +02:00
parent 95cd8427f4
commit b17e7d5746
4 changed files with 43 additions and 12 deletions

View File

@@ -377,6 +377,21 @@ class DirectObjectAccess(object):
return inspect.isclass(self._obj) and self._obj != type return inspect.isclass(self._obj) and self._obj != type
def get_signature_params(self): def get_signature_params(self):
return [
SignatureParam(
name=p.name,
has_default=p.default is not p.empty,
default=self._create_access_path(p.default),
has_annotation=p.annotation is not p.empty,
annotation=self._create_access_path(p.annotation),
kind_name=str(p.kind)
) for p in self._get_signature().parameters.values()
]
def get_signature_text(self):
return str(self._get_signature())
def _get_signature(self):
obj = self._obj obj = self._obj
if py_version < 33: if py_version < 33:
raise ValueError("inspect.signature was introduced in 3.3") raise ValueError("inspect.signature was introduced in 3.3")
@@ -397,22 +412,12 @@ class DirectObjectAccess(object):
raise ValueError raise ValueError
try: try:
signature = inspect.signature(obj) return inspect.signature(obj)
except (RuntimeError, TypeError): except (RuntimeError, TypeError):
# Reading the code of the function in Python 3.6 implies there are # Reading the code of the function in Python 3.6 implies there are
# at least these errors that might occur if something is wrong with # at least these errors that might occur if something is wrong with
# the signature. In that case we just want a simple escape for now. # the signature. In that case we just want a simple escape for now.
raise ValueError raise ValueError
return [
SignatureParam(
name=p.name,
has_default=p.default is not p.empty,
default=self._create_access_path(p.default),
has_annotation=p.annotation is not p.empty,
annotation=self._create_access_path(p.annotation),
kind_name=str(p.kind)
) for p in signature.parameters.values()
]
def negate(self): def negate(self):
return self._create_access_path(-self._obj) return self._create_access_path(-self._obj)

View File

@@ -5,6 +5,7 @@ import re
from functools import partial from functools import partial
from jedi import debug from jedi import debug
from jedi.evaluate.utils import to_list
from jedi._compatibility import force_unicode, Parameter, cast_path from jedi._compatibility import force_unicode, Parameter, cast_path
from jedi.cache import underscore_memoization, memoize_method from jedi.cache import underscore_memoization, memoize_method
from jedi.evaluate.filters import AbstractFilter from jedi.evaluate.filters import AbstractFilter
@@ -111,6 +112,7 @@ class CompiledObject(Context):
def py__doc__(self): def py__doc__(self):
return self.access_handle.py__doc__() return self.access_handle.py__doc__()
@to_list
def get_param_names(self): def get_param_names(self):
try: try:
signature_params = self.access_handle.get_signature_params() signature_params = self.access_handle.get_signature_params()
@@ -126,6 +128,13 @@ class CompiledObject(Context):
for signature_param in signature_params: for signature_param in signature_params:
yield SignatureParamName(self, signature_param) yield SignatureParamName(self, signature_param)
def get_signature_text(self):
try:
return self.access_handle.get_signature_text()
except ValueError:
params_str, ret = self._parse_function_doc()
return '(' + params_str + ')' + ret
def get_signatures(self): def get_signatures(self):
return [BuiltinSignature(self)] return [BuiltinSignature(self)]

View File

@@ -10,6 +10,7 @@ class AbstractSignature(object):
def name(self): def name(self):
return self.context.name return self.context.name
@property
def annotation(self): def annotation(self):
return None return None
@@ -34,6 +35,7 @@ class TreeSignature(AbstractSignature):
def bind(self, context): def bind(self, context):
return TreeSignature(context, self._function_context, is_bound=True) return TreeSignature(context, self._function_context, is_bound=True)
@property
def annotation(self): def annotation(self):
return self._function_context.tree_node.annotation return self._function_context.tree_node.annotation
@@ -52,7 +54,7 @@ class BuiltinSignature(AbstractSignature):
return self.context return self.context
def to_string(self): def to_string(self):
return '' return self.name.string_name + self.context.get_signature_text()
def bind(self, context): def bind(self, context):
raise NotImplementedError('pls implement, need test case, %s' % context) raise NotImplementedError('pls implement, need test case, %s' % context)

View File

@@ -0,0 +1,15 @@
import pytest
from jedi.evaluate.gradual.conversion import stub_to_actual_context_set
def test_compiled_signature(Script):
code = 'import math; math.cos'
sig = 'cos(x, /)'
d, = Script(code).goto_definitions()
context, = d._name.infer()
compiled, = stub_to_actual_context_set(context)
signature, = compiled.get_signatures()
assert signature.to_string() == sig
assert signature.get_param_names() == []
assert signature.annotation is None