Skip to content

Commit

Permalink
Fix an issue with magic methods on classes. Fixes #461.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhalter committed Jul 31, 2016
1 parent 647a4db commit 6440e33
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions jedi/evaluate/compiled/__init__.py
Expand Up @@ -476,6 +476,7 @@ def _a_generator(foo):

_SPECIAL_OBJECTS = {
'FUNCTION_CLASS': type(load_module),
'METHOD_CLASS': type(CompiledObject.is_class),
'MODULE_CLASS': type(os),
'GENERATOR_OBJECT': _a_generator(1.0),
'BUILTINS': _builtins,
Expand Down
10 changes: 8 additions & 2 deletions jedi/evaluate/representation.py
Expand Up @@ -579,7 +579,7 @@ def names_dicts(self, search_global):
if search_global:
yield self.names_dict
else:
scope = compiled.get_special_object(self._evaluator, 'FUNCTION_CLASS')
scope = self.py__class__()
for names_dict in scope.names_dicts(False):
yield names_dict

Expand All @@ -605,7 +605,13 @@ def py__annotations__(self):
return dct

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):
return getattr(self.base_func, name)
Expand Down
17 changes: 17 additions & 0 deletions test/test_evaluate/test_compiled.py
@@ -1,3 +1,5 @@
from textwrap import dedent

from jedi._compatibility import builtins, is_py3
from jedi.parser import load_grammar
from jedi.parser.tree import Function
Expand Down Expand Up @@ -67,3 +69,18 @@ def typ(string):
else:
assert typ('b""') == 'str'
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

0 comments on commit 6440e33

Please sign in to comment.