1
0
forked from VimPlug/jedi

Fix an issue with magic methods on classes. Fixes #461.

This commit is contained in:
Dave Halter
2016-07-31 23:42:00 +02:00
parent 647a4db326
commit 6440e33512
3 changed files with 26 additions and 2 deletions

View File

@@ -476,6 +476,7 @@ def _a_generator(foo):
_SPECIAL_OBJECTS = { _SPECIAL_OBJECTS = {
'FUNCTION_CLASS': type(load_module), 'FUNCTION_CLASS': type(load_module),
'METHOD_CLASS': type(CompiledObject.is_class),
'MODULE_CLASS': type(os), 'MODULE_CLASS': type(os),
'GENERATOR_OBJECT': _a_generator(1.0), 'GENERATOR_OBJECT': _a_generator(1.0),
'BUILTINS': _builtins, 'BUILTINS': _builtins,

View File

@@ -579,7 +579,7 @@ class Function(use_metaclass(CachedMetaClass, Wrapper)):
if search_global: if search_global:
yield self.names_dict yield self.names_dict
else: else:
scope = compiled.get_special_object(self._evaluator, 'FUNCTION_CLASS') scope = self.py__class__()
for names_dict in scope.names_dicts(False): for names_dict in scope.names_dicts(False):
yield names_dict yield names_dict
@@ -605,7 +605,13 @@ class Function(use_metaclass(CachedMetaClass, Wrapper)):
return dct return dct
def py__class__(self): def py__class__(self):
return compiled.get_special_object(self._evaluator, 'FUNCTION_CLASS') # This differentiation is only necessary for Python2. Python3 does not
# use a different method class.
if isinstance(self.base.get_parent_scope(), tree.Class):
name = 'METHOD_CLASS'
else:
name = 'FUNCTION_CLASS'
return compiled.get_special_object(self._evaluator, name)
def __getattr__(self, name): def __getattr__(self, name):
return getattr(self.base_func, name) return getattr(self.base_func, name)

View File

@@ -1,3 +1,5 @@
from textwrap import dedent
from jedi._compatibility import builtins, is_py3 from jedi._compatibility import builtins, is_py3
from jedi.parser import load_grammar from jedi.parser import load_grammar
from jedi.parser.tree import Function from jedi.parser.tree import Function
@@ -67,3 +69,18 @@ def test_string_literals():
else: else:
assert typ('b""') == 'str' assert typ('b""') == 'str'
assert typ('u""') == 'unicode' assert typ('u""') == 'unicode'
def test_method_completion():
code = dedent('''
class Foo:
def bar(self):
pass
foo = Foo()
foo.bar.__func__''')
if is_py3:
result = []
else:
result = ['__func__']
assert [c.name for c in Script(code).completions()] == result