Rename py__getitem__ to py__simple_getitem

This commit is contained in:
Dave Halter
2018-08-10 19:31:19 +02:00
parent bc5ca4d8ae
commit 84b89f4689
9 changed files with 31 additions and 27 deletions

View File

@@ -77,7 +77,7 @@ class Context(BaseContext):
# The actual getitem call. # The actual getitem call.
try: try:
getitem = self.py__getitem__ getitem = self.py__simple_getitem__
except AttributeError: except AttributeError:
from jedi.evaluate import analysis from jedi.evaluate import analysis
# TODO this context is probably not right. # TODO this context is probably not right.

View File

@@ -226,7 +226,7 @@ class DirectObjectAccess(object):
def py__mro__accesses(self): def py__mro__accesses(self):
return tuple(self._create_access_path(cls) for cls in self._obj.__mro__[1:]) return tuple(self._create_access_path(cls) for cls in self._obj.__mro__[1:])
def py__getitem__(self, index): def py__simple_getitem__(self, index):
if type(self._obj) not in (str, list, tuple, unicode, bytes, bytearray, dict): if type(self._obj) not in (str, list, tuple, unicode, bytes, bytearray, dict):
# Get rid of side effects, we won't call custom `__getitem__`s. # Get rid of side effects, we won't call custom `__getitem__`s.
return None return None

View File

@@ -19,10 +19,15 @@ from . import fake
class CheckAttribute(object): class CheckAttribute(object):
"""Raises an AttributeError if the attribute X isn't available.""" """Raises an AttributeError if the attribute X isn't available."""
def __init__(self, func): def __init__(self, check_name=None):
self.func = func
# Remove the py in front of e.g. py__call__. # Remove the py in front of e.g. py__call__.
self.check_name = force_unicode(func.__name__[2:]) self.check_name = check_name
def __call__(self, func):
self.func = func
if self.check_name is None:
self.check_name = force_unicode(func.__name__[2:])
return self
def __get__(self, instance, owner): def __get__(self, instance, owner):
if instance is None: if instance is None:
@@ -48,7 +53,7 @@ class CompiledObject(Context):
# This attribute will not be set for most classes, except for fakes. # This attribute will not be set for most classes, except for fakes.
self.tree_node = faked_class self.tree_node = faked_class
@CheckAttribute @CheckAttribute()
def py__call__(self, params): def py__call__(self, params):
if self.tree_node is not None and self.tree_node.type == 'funcdef': if self.tree_node is not None and self.tree_node.type == 'funcdef':
from jedi.evaluate.context.function import FunctionContext from jedi.evaluate.context.function import FunctionContext
@@ -63,25 +68,25 @@ class CompiledObject(Context):
else: else:
return ContextSet.from_iterable(self._execute_function(params)) return ContextSet.from_iterable(self._execute_function(params))
@CheckAttribute @CheckAttribute()
def py__class__(self): def py__class__(self):
return create_from_access_path(self.evaluator, self.access_handle.py__class__()) return create_from_access_path(self.evaluator, self.access_handle.py__class__())
@CheckAttribute @CheckAttribute()
def py__mro__(self): def py__mro__(self):
return (self,) + tuple( return (self,) + tuple(
create_from_access_path(self.evaluator, access) create_from_access_path(self.evaluator, access)
for access in self.access_handle.py__mro__accesses() for access in self.access_handle.py__mro__accesses()
) )
@CheckAttribute @CheckAttribute()
def py__bases__(self): def py__bases__(self):
return tuple( return tuple(
create_from_access_path(self.evaluator, access) create_from_access_path(self.evaluator, access)
for access in self.access_handle.py__bases__() for access in self.access_handle.py__bases__()
) )
@CheckAttribute @CheckAttribute()
def py__path__(self): def py__path__(self):
return self.access_handle.py__path__() return self.access_handle.py__path__()
@@ -148,16 +153,16 @@ class CompiledObject(Context):
""" """
return CompiledObjectFilter(self.evaluator, self, is_instance) return CompiledObjectFilter(self.evaluator, self, is_instance)
@CheckAttribute @CheckAttribute('__getitem__')
def py__getitem__(self, index): def py__simple_getitem__(self, index):
with reraise_as_evaluator(IndexError, KeyError, TypeError): with reraise_as_evaluator(IndexError, KeyError, TypeError):
access = self.access_handle.py__getitem__(index) access = self.access_handle.py__simple_getitem__(index)
if access is None: if access is None:
return ContextSet() return ContextSet()
return ContextSet(create_from_access_path(self.evaluator, access)) return ContextSet(create_from_access_path(self.evaluator, access))
@CheckAttribute @CheckAttribute()
def py__iter__(self): def py__iter__(self):
for access in self.access_handle.py__iter__list(): for access in self.access_handle.py__iter__list():
yield LazyKnownContext(create_from_access_path(self.evaluator, access)) yield LazyKnownContext(create_from_access_path(self.evaluator, access))

View File

@@ -126,7 +126,7 @@ class AbstractInstanceContext(Context):
else: else:
yield InstanceClassFilter(self.evaluator, self, cls, origin_scope) yield InstanceClassFilter(self.evaluator, self, cls, origin_scope)
def py__getitem__(self, index): def py__simple_getitem__(self, index):
try: try:
names = self.get_function_slot_names(u'__getitem__') names = self.get_function_slot_names(u'__getitem__')
except KeyError: except KeyError:

View File

@@ -212,7 +212,7 @@ class Sequence(BuiltinOverwrite, IterableMixin):
class ListComprehension(ComprehensionMixin, Sequence): class ListComprehension(ComprehensionMixin, Sequence):
array_type = u'list' array_type = u'list'
def py__getitem__(self, index): def py__simple_getitem__(self, index):
if isinstance(index, slice): if isinstance(index, slice):
return ContextSet(self) return ContextSet(self)
@@ -236,7 +236,7 @@ class DictComprehension(ComprehensionMixin, Sequence):
for keys, values in self._iterate(): for keys, values in self._iterate():
yield LazyKnownContexts(keys) yield LazyKnownContexts(keys)
def py__getitem__(self, index): def py__simple_getitem__(self, index):
for keys, values in self._iterate(): for keys, values in self._iterate():
for k in keys: for k in keys:
if isinstance(k, compiled.CompiledObject): if isinstance(k, compiled.CompiledObject):
@@ -289,7 +289,7 @@ class SequenceLiteralContext(Sequence):
self.array_type = SequenceLiteralContext.mapping[atom.children[0]] self.array_type = SequenceLiteralContext.mapping[atom.children[0]]
"""The builtin name of the array (list, set, tuple or dict).""" """The builtin name of the array (list, set, tuple or dict)."""
def py__getitem__(self, index): def py__simple_getitem__(self, index):
"""Here the index is an int/str. Raises IndexError/KeyError.""" """Here the index is an int/str. Raises IndexError/KeyError."""
if self.array_type == u'dict': if self.array_type == u'dict':
compiled_obj_index = compiled.create_simple_object(self.evaluator, index) compiled_obj_index = compiled.create_simple_object(self.evaluator, index)
@@ -419,7 +419,7 @@ class FakeSequence(_FakeArray):
super(FakeSequence, self).__init__(evaluator, None, array_type) super(FakeSequence, self).__init__(evaluator, None, array_type)
self._lazy_context_list = lazy_context_list self._lazy_context_list = lazy_context_list
def py__getitem__(self, index): def py__simple_getitem__(self, index):
with reraise_as_evaluator(IndexError, TypeError): with reraise_as_evaluator(IndexError, TypeError):
lazy_context = self._lazy_context_list[index] lazy_context = self._lazy_context_list[index]
return lazy_context.infer() return lazy_context.infer()
@@ -443,7 +443,7 @@ class FakeDict(_FakeArray):
for key in self._dct: for key in self._dct:
yield LazyKnownContext(compiled.create_simple_object(self.evaluator, key)) yield LazyKnownContext(compiled.create_simple_object(self.evaluator, key))
def py__getitem__(self, index): def py__simple_getitem__(self, index):
if is_py3 and self.evaluator.environment.version_info.major == 2: if is_py3 and self.evaluator.environment.version_info.major == 2:
# In Python 2 bytes and unicode compare. # In Python 2 bytes and unicode compare.
if isinstance(index, bytes): if isinstance(index, bytes):
@@ -487,7 +487,7 @@ class MergedArray(_FakeArray):
for lazy_context in array.py__iter__(): for lazy_context in array.py__iter__():
yield lazy_context yield lazy_context
def py__getitem__(self, index): def py__simple_getitem__(self, index):
return ContextSet.from_sets(lazy_context.infer() for lazy_context in self.py__iter__()) return ContextSet.from_sets(lazy_context.infer() for lazy_context in self.py__iter__())
def _items(self): def _items(self):

View File

@@ -24,8 +24,9 @@ py__bases__() Returns a list of base classes.
py__mro__() Returns a list of classes (the mro). py__mro__() Returns a list of classes (the mro).
py__iter__() Returns a generator of a set of types. py__iter__() Returns a generator of a set of types.
py__class__() Returns the class of an instance. py__class__() Returns the class of an instance.
py__getitem__(index: int/str) Returns a a set of types of the index. py__simple_getitem__(index: int/str) Returns a a set of types of the index.
Can raise an IndexError/KeyError. Can raise an IndexError/KeyError.
py__getitem__(indexes: ContextSet) Returns a a set of types of the index.
py__file__() Only on modules. Returns None if does py__file__() Only on modules. Returns None if does
not exist. not exist.
py__package__() Only on modules. For the import system. py__package__() Only on modules. For the import system.

View File

@@ -56,7 +56,7 @@ def _evaluate_annotation_string(context, string, index=None):
context_set = context_set.filter( context_set = context_set.filter(
lambda context: context.array_type == u'tuple' # noqa lambda context: context.array_type == u'tuple' # noqa
and len(list(context.py__iter__())) >= index and len(list(context.py__iter__())) >= index
).py__getitem__(index) ).py__simple_getitem__(index)
return context_set.execute_evaluated() return context_set.execute_evaluated()
@@ -243,7 +243,7 @@ def _get_typing_replacement_module(grammar):
return _typing_module, _typing_module_code_lines return _typing_module, _typing_module_code_lines
def py__getitem__(context, typ, node): def py__simple_getitem__(context, typ, node):
if not typ.get_root_context().name.string_name == "typing": if not typ.get_root_context().name.string_name == "typing":
return None return None
# we assume that any class using [] in a module called # we assume that any class using [] in a module called

View File

@@ -161,7 +161,7 @@ def eval_trailer(context, base_contexts, trailer):
result = ContextSet() result = ContextSet()
for typ in list(foo): for typ in list(foo):
if isinstance(typ, (ClassContext, TreeInstance)): if isinstance(typ, (ClassContext, TreeInstance)):
typing_module_types = pep0484.py__getitem__(context, typ, node) typing_module_types = pep0484.py__simple_getitem__(context, typ, node)
if typing_module_types is not None: if typing_module_types is not None:
foo.remove(typ) foo.remove(typ)
result |= typing_module_types result |= typing_module_types

View File

@@ -1,5 +1,3 @@
from parso.python.tree import Name
from jedi.plugins.base import BasePlugin from jedi.plugins.base import BasePlugin
from jedi.evaluate.imports import JediImportError from jedi.evaluate.imports import JediImportError