Use CheckAttribute descriptor more in CompiledObject to avoid duplicate code.

This commit is contained in:
Dave Halter
2015-12-10 16:43:42 +01:00
parent 5087584fdc
commit 1189868593
+5 -20
View File
@@ -46,19 +46,14 @@ class CompiledObject(Base):
self.obj = obj self.obj = obj
self.parent = parent self.parent = parent
@property @CheckAttribute
def py__call__(self): def py__call__(self, params):
def actual(params):
if inspect.isclass(self.obj): if inspect.isclass(self.obj):
from jedi.evaluate.representation import Instance from jedi.evaluate.representation import Instance
return set([Instance(self._evaluator, self, params)]) return set([Instance(self._evaluator, self, params)])
else: else:
return set(self._execute_function(params)) return set(self._execute_function(params))
# Might raise an AttributeError, which is intentional.
self.obj.__call__
return actual
@CheckAttribute @CheckAttribute
def py__class__(self): def py__class__(self):
return create(self._evaluator, self.obj.__class__, parent=self.parent) return create(self._evaluator, self.obj.__class__, parent=self.parent)
@@ -168,32 +163,22 @@ 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)
@property @CheckAttribute
def py__getitem__(self): def py__getitem__(self, index):
if not hasattr(self.obj, '__getitem__'):
raise AttributeError('No __getitem__ on %s' % self.obj)
def actual(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 set() return set()
return set([create(self._evaluator, self.obj[index])]) return set([create(self._evaluator, self.obj[index])])
return actual
@property @CheckAttribute
def py__iter__(self): def py__iter__(self):
if not hasattr(self.obj, '__iter__'):
raise AttributeError('No __iter__ on %s' % self.obj)
def actual():
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 return
for part in self.obj: for part in self.obj:
yield set([create(self._evaluator, part)]) yield set([create(self._evaluator, part)])
return actual
@property @property
def name(self): def name(self):