mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 06:44:46 +08:00
Fix some API classes issues. Among them call signature generation and Definition.parent() issues.
This commit is contained in:
@@ -156,9 +156,13 @@ class BaseDefinition(object):
|
||||
|
||||
if isinstance(stripped, compiled.CompiledObject):
|
||||
return stripped.type()
|
||||
if isinstance(stripped, iterable.Array):
|
||||
elif isinstance(stripped, iterable.Array):
|
||||
return 'instance'
|
||||
elif isinstance(stripped, pr.Import):
|
||||
return 'import'
|
||||
|
||||
string = type(stripped).__name__.lower().replace('wrapper', '')
|
||||
print(stripped, string)
|
||||
if string == 'exprstmt':
|
||||
return 'statement'
|
||||
else:
|
||||
@@ -379,8 +383,8 @@ class BaseDefinition(object):
|
||||
|
||||
def parent(self):
|
||||
scope = self._definition.get_parent_scope()
|
||||
non_flow = scope.get_parent_until(pr.Flow, reverse=True)
|
||||
return Definition(self._evaluator, non_flow.name)
|
||||
scope = er.wrap(self._evaluator, scope)
|
||||
return Definition(self._evaluator, scope.name)
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s %s>" % (type(self).__name__, self.description)
|
||||
|
||||
@@ -414,6 +414,10 @@ class Class(use_metaclass(CachedMetaClass, Wrapper)):
|
||||
def py__getattribute__(self, name):
|
||||
return self._evaluator.find_types(self, name)
|
||||
|
||||
@property
|
||||
def params(self):
|
||||
return self.get_subscope_by_name('__init__').params
|
||||
|
||||
def scope_names_generator(self, position=None, add_class_vars=True):
|
||||
def in_iterable(name, iterable):
|
||||
""" checks if the name is in the variable 'iterable'. """
|
||||
|
||||
@@ -43,9 +43,10 @@ import re
|
||||
from inspect import cleandoc
|
||||
from collections import defaultdict
|
||||
from itertools import chain
|
||||
import textwrap
|
||||
|
||||
from jedi._compatibility import (next, Python3Method, encoding, is_py3,
|
||||
literal_eval, use_metaclass)
|
||||
literal_eval, use_metaclass, unicode)
|
||||
from jedi import debug
|
||||
from jedi import cache
|
||||
|
||||
@@ -779,9 +780,9 @@ class Class(ClassOrFunc):
|
||||
if self._doc_token is not None:
|
||||
docstr = self.raw_doc
|
||||
for sub in self.subscopes:
|
||||
if unicode(sub.name) == '__init__':
|
||||
if str(sub.name) == '__init__':
|
||||
return '%s\n\n%s' % (
|
||||
sub.get_call_signature(funcname=self.name), docstr)
|
||||
sub.get_call_signature(func_name=self.name), docstr)
|
||||
return docstr
|
||||
|
||||
def scope_names_generator(self, position=None):
|
||||
@@ -862,32 +863,20 @@ class Function(ClassOrFunc):
|
||||
def scope_names_generator(self, position=None):
|
||||
yield self, filter_after_position(self.get_defined_names(), position)
|
||||
|
||||
def get_call_signature(self, width=72, funcname=None):
|
||||
def get_call_signature(self, width=72, func_name=None):
|
||||
"""
|
||||
Generate call signature of this function.
|
||||
|
||||
:param width: Fold lines if a line is longer than this value.
|
||||
:type width: int
|
||||
:arg funcname: Override function name when given.
|
||||
:type funcname: str
|
||||
:arg func_name: Override function name when given.
|
||||
:type func_name: str
|
||||
|
||||
:rtype: str
|
||||
"""
|
||||
l = unicode(funcname or self.name) + '('
|
||||
lines = []
|
||||
for (i, p) in enumerate(self.params):
|
||||
code = p.get_code(False)
|
||||
if i != len(self.params) - 1:
|
||||
code += ', '
|
||||
if len(l + code) > width:
|
||||
lines.append(l[:-1] if l[-1] == ' ' else l)
|
||||
l = code
|
||||
else:
|
||||
l += code
|
||||
if l:
|
||||
lines.append(l)
|
||||
lines[-1] += ')'
|
||||
return '\n'.join(lines)
|
||||
func_name = func_name or self.children[1]
|
||||
code = unicode(func_name) + self.children[2].get_code()
|
||||
return '\n'.join(textwrap.wrap(code, width))
|
||||
|
||||
@property
|
||||
def doc(self):
|
||||
|
||||
@@ -105,7 +105,7 @@ def test_class_call_signature():
|
||||
pass
|
||||
Foo""").goto_definitions()
|
||||
doc = defs[0].doc
|
||||
assert "Foo(self, x, y = 1, z = 'a')" in str(doc)
|
||||
assert "Foo(self, x, y=1, z='a')" in str(doc)
|
||||
|
||||
|
||||
def test_position_none_if_builtin():
|
||||
@@ -212,7 +212,7 @@ class TestParent(TestCase):
|
||||
def bar(): pass
|
||||
Foo().bar''')).completions()[0].parent()
|
||||
assert parent.name == 'Foo'
|
||||
assert parent.type == 'class'
|
||||
assert parent.type == 'instance'
|
||||
|
||||
parent = Script('str.join').completions()[0].parent()
|
||||
assert parent.name == 'str'
|
||||
|
||||
Reference in New Issue
Block a user