CompiledObject.type resembles now the Node.type values.

This commit is contained in:
Dave Halter
2015-01-09 01:33:59 +01:00
parent b75ba1e16c
commit 26ecb16e5f
5 changed files with 19 additions and 9 deletions

View File

@@ -149,7 +149,7 @@ class BaseDefinition(object):
stripped = stripped.var
if isinstance(stripped, compiled.CompiledObject):
return stripped.type()
return stripped.api_type()
elif isinstance(stripped, iterable.Array):
return 'instance'
elif isinstance(stripped, pr.Import):
@@ -530,7 +530,7 @@ class Definition(use_metaclass(CachedMetaClass, BaseDefinition)):
d = d.var
if isinstance(d, compiled.CompiledObject):
typ = d.type()
typ = d.api_type()
if typ == 'instance':
typ = 'class' # The description should be similar to Py objects.
d = typ + ' ' + d.name.get_code()

View File

@@ -110,7 +110,7 @@ class CompiledObject(Base):
return _parse_function_doc(self.doc)
def type(self):
def api_type(self):
if fake.is_class_instance(self.obj):
return 'instance'
@@ -123,6 +123,18 @@ class CompiledObject(Base):
or inspect.ismethoddescriptor(cls):
return 'function'
@property
def type(self):
"""Imitate the tree.Node.type values."""
cls = self._cls().obj
if inspect.isclass(cls):
return 'classdef'
elif inspect.ismodule(cls):
return 'file_input'
elif inspect.isbuiltin(cls) or inspect.ismethod(cls) \
or inspect.ismethoddescriptor(cls):
return 'funcdef'
@underscore_memoization
def _cls(self):
# Ensures that a CompiledObject is returned that is not an instance (like list)
@@ -195,7 +207,7 @@ class CompiledObject(Base):
return FakeName(self._cls().obj.__name__, self)
def _execute_function(self, evaluator, params):
if self.type() != 'function':
if self.type != 'funcdef':
return
for name in self._parse_function_doc()[1].split():

View File

@@ -467,8 +467,7 @@ def global_names_dict_generator(evaluator, scope, position):
"""
in_func = False
while scope is not None:
if not((scope.type == 'classdef' or isinstance(scope,
compiled.CompiledObject) and scope.type() == 'class') and in_func):
if not (scope.type == 'classdef' and in_func):
# Names in methods cannot be resolved within the class.
for names_dict in scope.names_dicts(True):

View File

@@ -432,8 +432,7 @@ class _Importer(object):
for scope in self.follow(evaluator):
# Non-modules are not completable.
if not isinstance(scope, er.ModuleWrapper) and not (isinstance(scope,
compiled.CompiledObject) and scope.type() == 'module'):
if not scope.type == 'file_input': # not a module
continue
# namespace packages

View File

@@ -169,4 +169,4 @@ def test_loading_unicode_files_with_bad_global_charset(monkeypatch, tmpdir):
f.write(data)
s = Script("from test1 import foo\nfoo.",
line=2, column=4, path=filename2)
s.complete()
s.completions()