diff options
Diffstat (limited to 'astroid/tree/scoped_nodes.py')
-rw-r--r-- | astroid/tree/scoped_nodes.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 257b13d9..31dfc9cd 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -34,6 +34,7 @@ from astroid.interpreter import objects from astroid.interpreter import objectmodel from astroid.interpreter import runtimeabc from astroid.interpreter.util import infer_stmts +from astroid.interpreter import dunder_lookup from astroid import manager from astroid.tree import base as treebase from astroid.tree import node_classes @@ -1671,6 +1672,30 @@ class ClassDef(QualifiedNameMixin, base.FilterStmtsMixin, pass return False + def getitem(self, index, context=None): + """Return the inference of a subscript. + + This is basically looking up the method in the metaclass and calling it. + """ + try: + methods = dunder_lookup.lookup(self, '__getitem__') + except (exceptions.AttributeInferenceError, + exceptions.NotSupportedError) as error: + util.reraise(exceptions.InferenceError(**vars(error))) + + method = methods[0] + + # Create a new callcontext for providing index as an argument. + if context: + new_context = context.clone() + else: + new_context = contextmod.InferenceContext() + + new_context.callcontext = contextmod.CallContext(args=[index]) + new_context.boundnode = self + + return next(method.infer_call_result(self, new_context)) + def methods(self): """return an iterator on all methods defined in the class and its ancestors |