1
0
forked from VimPlug/jedi

refactor general array lookup method get_index_types

This commit is contained in:
Dave Halter
2014-05-12 15:23:48 +02:00
parent 70d85d1b3a
commit b6ec589997
6 changed files with 28 additions and 19 deletions

View File

@@ -275,8 +275,11 @@ class Evaluator(object):
# This must be an execution, either () or []. # This must be an execution, either () or [].
if current.type == pr.Array.LIST: if current.type == pr.Array.LIST:
if hasattr(typ, 'get_index_types'): if hasattr(typ, 'get_index_types'):
slc = iterable.create_indexes_or_slices(self, current) if isinstance(typ, compiled.CompiledObject):
result = typ.get_index_types(slc) # CompiledObject doesn't contain an evaluator instance.
result = typ.get_index_types(self, current)
else:
result = typ.get_index_types(current)
elif current.type not in [pr.Array.DICT]: elif current.type not in [pr.Array.DICT]:
# Scope must be a class or func - make an instance or execution. # Scope must be a class or func - make an instance or execution.
result = self.execute(typ, current) result = self.execute(typ, current)

View File

@@ -99,7 +99,7 @@ class CompiledObject(Base):
else: else:
raise KeyError("CompiledObject doesn't have an attribute '%s'." % name) raise KeyError("CompiledObject doesn't have an attribute '%s'." % name)
def get_index_types(self, index_types): def get_index_types(self, evaluator, index_array):
# If the object doesn't have `__getitem__`, just raise the # If the object doesn't have `__getitem__`, just raise the
# AttributeError. # AttributeError.
if not hasattr(self.obj, '__getitem__'): if not hasattr(self.obj, '__getitem__'):
@@ -110,7 +110,8 @@ class CompiledObject(Base):
return [] return []
result = [] result = []
for typ in index_types: from jedi.evaluate.iterable import create_indexes_or_slices
for typ in create_indexes_or_slices(evaluator, index_array):
index = None index = None
try: try:
index = typ.obj index = typ.obj

View File

@@ -356,7 +356,7 @@ def _check_isinstance_type(evaluator, stmt, search_name_part):
result = [] result = []
for c in evaluator.eval_call(classes[0]): for c in evaluator.eval_call(classes[0]):
for typ in (c.get_index_types() if isinstance(c, iterable.Array) else [c]): for typ in (c.values() if isinstance(c, iterable.Array) else [c]):
result += evaluator.execute(typ) result += evaluator.execute(typ)
return result return result

View File

@@ -60,7 +60,7 @@ class Generator(use_metaclass(CachedMetaClass, pr.Base)):
""" returns the content of __iter__ """ """ returns the content of __iter__ """
return self._evaluator.execute(self.func, self.var_args, True) return self._evaluator.execute(self.func, self.var_args, True)
def get_index_types(self, index=None): def get_index_types(self, index_array):
debug.warning('Tried to get array access on a generator: %s', self) debug.warning('Tried to get array access on a generator: %s', self)
return [] return []
@@ -99,13 +99,13 @@ class Array(use_metaclass(CachedMetaClass, pr.Base)):
self._array = array self._array = array
@memoize_default(NO_DEFAULT) @memoize_default(NO_DEFAULT)
def get_index_types(self, indexes=()): def get_index_types(self, index_array=()):
""" """
Get the types of a specific index or all, if not given. Get the types of a specific index or all, if not given.
:param indexes: The index input types. :param indexes: The index input types.
""" """
result = [] indexes = create_indexes_or_slices(self._evaluator, index_array)
if [index for index in indexes if isinstance(index, Slice)]: if [index for index in indexes if isinstance(index, Slice)]:
return [self] return [self]
@@ -118,6 +118,10 @@ class Array(use_metaclass(CachedMetaClass, pr.Base)):
with common.ignored(KeyError, IndexError, TypeError): with common.ignored(KeyError, IndexError, TypeError):
return self.get_exact_index_types(index.obj) return self.get_exact_index_types(index.obj)
return self.values()
@memoize_default(NO_DEFAULT)
def values(self):
result = list(_follow_values(self._evaluator, self._array.values)) result = list(_follow_values(self._evaluator, self._array.values))
result += check_array_additions(self._evaluator, self) result += check_array_additions(self._evaluator, self)
return result return result
@@ -228,22 +232,22 @@ def get_iterator_types(inputs):
result = [] result = []
from jedi.evaluate.representation import Instance from jedi.evaluate.representation import Instance
for gen in iterators: for it in iterators:
if isinstance(gen, Array): if isinstance(it, Array):
# Array is a little bit special, since this is an internal # Array is a little bit special, since this is an internal
# array, but there's also the list builtin, which is # array, but there's also the list builtin, which is
# another thing. # another thing.
result += gen.get_index_types() result += it.values()
elif isinstance(gen, Instance): elif isinstance(it, Instance):
# __iter__ returned an instance. # __iter__ returned an instance.
name = '__next__' if is_py3 else 'next' name = '__next__' if is_py3 else 'next'
try: try:
result += gen.execute_subscope_by_name(name) result += it.execute_subscope_by_name(name)
except KeyError: except KeyError:
debug.warning('Instance has no __next__ function in %s.', gen) debug.warning('Instance has no __next__ function in %s.', it)
else: else:
# is a generator # Is a generator.
result += gen.iter_content() result += it.iter_content()
return result return result

View File

@@ -168,7 +168,9 @@ class Instance(use_metaclass(CachedMetaClass, Executable)):
except KeyError: except KeyError:
return False return False
def get_index_types(self, indexes=[]): def get_index_types(self, index_array):
indexes = iterable.create_indexes_or_slices(self._evaluator, index_array)
if any([isinstance(i, iterable.Slice) for i in indexes]): if any([isinstance(i, iterable.Slice) for i in indexes]):
# Slice support in Jedi is very marginal, at the moment, so just # Slice support in Jedi is very marginal, at the moment, so just
# ignore them in case of __getitem__. # ignore them in case of __getitem__.

View File

@@ -17,8 +17,7 @@ class Cls():
def defined_lookup(self, obj): def defined_lookup(self, obj):
""" """
Uses an arbitrary object and performs an operation on it, shouldn't `obj` is defined by a call into this function.
be a problem.
""" """
obj.upper obj.upper
#! attribute-error #! attribute-error