diff options
author | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-04-25 00:29:31 +0300 |
---|---|---|
committer | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-04-25 00:29:31 +0300 |
commit | 98ebf98748c0cc4beaf40e779b0c863cb5cad7a5 (patch) | |
tree | a23e3b307019d2b0cff8141e0c2ce5ca7f8e8863 | |
parent | d5075734df02cf1aa60da97a2f68b73b97d41558 (diff) | |
download | astroid-98ebf98748c0cc4beaf40e779b0c863cb5cad7a5.tar.gz |
Expose a implicit_metaclass() method in Class.
This will return a builtins.type instance for newstyle classes, otherwise it will
return None.
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | astroid/scoped_nodes.py | 11 | ||||
-rw-r--r-- | astroid/tests/unittest_scoped_nodes.py | 15 |
3 files changed, 30 insertions, 1 deletions
@@ -68,7 +68,10 @@ Change log for the astroid package (used to be astng) change (the previous behaviour was incorrect though). Also, having bases which can return multiple values when inferred will not work with the new approach, because .mro() only - retrieves the first value inferred from a base. + retrieves the first value inferred from a base. + + * Expose a implicit_metaclass() method in Class. This will return + a builtins.type instance for newstyle classes. 2015-03-14 -- 1.3.6 diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 57818be..510961e 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -1318,6 +1318,17 @@ class Class(Statement, LocalsDictNodeNG, FilterStmtsMixin): if missing: raise InferenceError() + def implicit_metaclass(self): + """Get the implicit metaclass of the current class + + For newstyle classes, this will return an instance of builtins.type. + For oldstyle classes, it will simply return None, since there's + no implicit metaclass there. + """ + + if self.newstyle: + return builtin_lookup('type')[1][0] + _metaclass = None def _explicit_metaclass(self): """ Return the explicit defined metaclass diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index ab2ff3f..13af40c 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -1304,6 +1304,21 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): with self.assertRaises(NotFoundError): first.getattr("missing") + def test_implicit_metaclass(self): + cls = test_utils.extract_node(""" + class A(object): + pass + """) + type_cls = scoped_nodes.builtin_lookup("type")[1][0] + self.assertEqual(cls.implicit_metaclass(), type_cls) + + @test_utils.require_version(maxver='3.0') + def test_implicit_metaclass_is_none(self): + cls = test_utils.extract_node(""" + class A: pass + """) + self.assertIsNone(cls.implicit_metaclass()) + if __name__ == '__main__': unittest.main() |