summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Euresti <david@dropbox.com>2017-01-03 20:54:36 -0800
committerClaudiu Popa <pcmanticore@gmail.com>2017-01-22 14:43:36 +0200
commitbca2422a1001a9459982c3fbc19e39302fd76ef8 (patch)
treee2825cde503b5943bbed64dc0e02a5fbeecf773e
parent60beb83a9e4c544e6d9a0691812a63a31251e191 (diff)
downloadastroid-git-bca2422a1001a9459982c3fbc19e39302fd76ef8.tar.gz
Implement __getitem__ inference for classes (using the metaclass)
Essentially implement the getitem method in ClassDef which returns the correct value. Fixes #348
-rw-r--r--ChangeLog2
-rw-r--r--astroid/scoped_nodes.py24
-rw-r--r--astroid/tests/unittest_inference.py16
3 files changed, 42 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 423be842..dbc9da6c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,8 @@ Change log for the astroid package (used to be astng)
Classes can support keyword arguments, which are passed when
a class is constructed using ``__new__``.
+ * ClassDef now supports __getitem__ inference through the metaclass.
+
* getitem() method accepts nodes now, instead of Python objects.
* Add support for explicit namespace packages, created with pkg_resources.
diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py
index 46df423d..dd7d0b0a 100644
--- a/astroid/scoped_nodes.py
+++ b/astroid/scoped_nodes.py
@@ -25,6 +25,7 @@ from astroid import context as contextmod
from astroid import exceptions
from astroid import decorators as decorators_mod
from astroid.interpreter import objectmodel
+from astroid.interpreter import dunder_lookup
from astroid import manager
from astroid import mixins
from astroid import node_classes
@@ -1511,6 +1512,29 @@ class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG,
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 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
diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py
index b0ad5c1c..d4b0d811 100644
--- a/astroid/tests/unittest_inference.py
+++ b/astroid/tests/unittest_inference.py
@@ -2531,6 +2531,22 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase):
for node in ast_nodes:
self.assertEqual(next(node.infer()), util.Uninferable)
+ def test_metaclass__getitem__(self):
+ ast_node = extract_node('''
+ class Meta(type):
+ def __getitem__(cls, arg):
+ return 24
+ import six
+ @six.add_metaclass(Meta)
+ class A(object):
+ pass
+
+ A['Awesome'] #@
+ ''')
+ inferred = next(ast_node.infer())
+ self.assertIsInstance(inferred, nodes.Const)
+ self.assertEqual(inferred.value, 24)
+
def test_bin_op_classes(self):
ast_node = extract_node('''
class Meta(type):