summaryrefslogtreecommitdiff
path: root/astroid/bases.py
diff options
context:
space:
mode:
Diffstat (limited to 'astroid/bases.py')
-rw-r--r--astroid/bases.py21
1 files changed, 17 insertions, 4 deletions
diff --git a/astroid/bases.py b/astroid/bases.py
index 05c0ae1c..0888f063 100644
--- a/astroid/bases.py
+++ b/astroid/bases.py
@@ -283,6 +283,23 @@ class Instance(BaseInstance):
except exceptions.AttributeInferenceError:
return False
+ def getitem(self, index, context=None):
+ # TODO: Rewrap index to Const for this case
+ new_context = contextmod.bind_context_to_node(context, self)
+ if not context:
+ context = new_context
+
+ # Create a new callcontext for providing index as an argument.
+ new_context.callcontext = contextmod.CallContext(args=[index])
+
+ method = next(self.igetattr("__getitem__", context=context), None)
+ if not isinstance(method, BoundMethod):
+ raise exceptions.InferenceError(
+ "Could not find __getitem__ for {node!r}.", node=self, context=context
+ )
+
+ return next(method.infer_call_result(self, new_context))
+
def pytype(self):
return self._proxied.qname()
@@ -316,10 +333,6 @@ class Instance(BaseInstance):
return True
return result
- # This is set in inference.py.
- def getitem(self, index, context=None):
- pass
-
class UnboundMethod(Proxy):
"""a special node representing a method not bound to an instance"""