From e9ddb83591ba90c1c9ce049067ee5eb48ead1981 Mon Sep 17 00:00:00 2001 From: Julien Cristau Date: Tue, 10 Jun 2014 18:48:00 +0200 Subject: Handle __metaclass__ defined at the module level According to the doc (https://docs.python.org/2/reference/datamodel.html#customizing-class-creation): The appropriate metaclass is determined by the following precedence rules: - If dict['__metaclass__'] exists, it is used. - Otherwise, if there is at least one base class, its metaclass is used (this looks for a __class__ attribute first and if not found, uses its type). - Otherwise, if a global variable named __metaclass__ exists, it is used. - Otherwise, the old-style, classic metaclass (types.ClassType) is used. The third case was not handled by node.metaclass(). Remove metaclass lookup from the rebuilder, handle it all in Class._explicit_metaclass() instead, and use that in _newstyle_impl if possible. Remove test_newstyle_and_metaclass_bad, as I think the returned value in this test is irrelevant (it's a TypeError anyway, so you can't actually build that class), and replace it with a test using nested classes. Closes issue#33 --- test/unittest_scoped_nodes.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/unittest_scoped_nodes.py b/test/unittest_scoped_nodes.py index 4d0bc594..62674f0f 100644 --- a/test/unittest_scoped_nodes.py +++ b/test/unittest_scoped_nodes.py @@ -767,17 +767,38 @@ def g2(): """)) klass = astroid['Test'] self.assertTrue(klass.newstyle) - - def test_newstyle_and_metaclass_bad(self): + self.assertEqual(klass.metaclass().name, 'ABCMeta') astroid = abuilder.string_build(dedent(""" + from abc import ABCMeta + __metaclass__ = ABCMeta class Test: - __metaclass__ = int + pass """)) klass = astroid['Test'] - if PY3K: - self.assertTrue(klass.newstyle) - else: - self.assertFalse(klass.newstyle) + self.assertTrue(klass.newstyle) + self.assertEqual(klass.metaclass().name, 'ABCMeta') + + def test_nested_metaclass(self): + astroid = abuilder.string_build(dedent(""" + from abc import ABCMeta + class A(object): + __metaclass__ = ABCMeta + class B: pass + + __metaclass__ = ABCMeta + class C: + __metaclass__ = type + class D: pass + """)) + a = astroid['A'] + b = a.locals['B'][0] + c = astroid['C'] + d = c.locals['D'][0] + self.assertEqual(a.metaclass().name, 'ABCMeta') + self.assertFalse(b.newstyle) + self.assertIsNone(b.metaclass()) + self.assertEqual(c.metaclass().name, 'type') + self.assertEqual(d.metaclass().name, 'ABCMeta') @require_version('2.7') def test_parent_metaclass(self): -- cgit v1.2.1 From ecd1e616e7ba45fc986063d5de3d2bc774f9d206 Mon Sep 17 00:00:00 2001 From: Julien Cristau Date: Mon, 30 Jun 2014 10:38:46 +0200 Subject: Stop looking at the __metaclass__ class attribute for python3 Python 3 only considers the `metaclass` keyword argument. --- test/unittest_python3.py | 3 +-- test/unittest_scoped_nodes.py | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/unittest_python3.py b/test/unittest_python3.py index 114e3d37..d93deacf 100644 --- a/test/unittest_python3.py +++ b/test/unittest_python3.py @@ -127,8 +127,7 @@ class Python3TC(TestCase): """)) klass = astroid['SubTest'] metaclass = klass.metaclass() - self.assertIsInstance(metaclass, Class) - self.assertEqual(metaclass.name, 'type') + self.assertIsNone(metaclass) @require_version('3.0') def test_metaclass_yes_leak(self): diff --git a/test/unittest_scoped_nodes.py b/test/unittest_scoped_nodes.py index 62674f0f..b6f3434b 100644 --- a/test/unittest_scoped_nodes.py +++ b/test/unittest_scoped_nodes.py @@ -716,6 +716,8 @@ def g2(): self.assertEqual(astroid['g2'].tolineno, 10) def test_simple_metaclass(self): + if PY3K: + self.skipTest('__metaclass__ syntax is python2-specific') astroid = abuilder.string_build(dedent(""" class Test(object): __metaclass__ = type @@ -734,8 +736,9 @@ def g2(): klass = astroid['Test'] self.assertFalse(klass.metaclass()) - @require_version('2.7') def test_metaclass_imported(self): + if PY3K: + self.skipTest('__metaclass__ syntax is python2-specific') astroid = abuilder.string_build(dedent(""" from abc import ABCMeta class Test(object): @@ -758,8 +761,9 @@ def g2(): klass = astroid['Meta'] self.assertIsNone(klass.metaclass()) - @require_version('2.7') def test_newstyle_and_metaclass_good(self): + if PY3K: + self.skipTest('__metaclass__ syntax is python2-specific') astroid = abuilder.string_build(dedent(""" from abc import ABCMeta class Test: @@ -779,6 +783,8 @@ def g2(): self.assertEqual(klass.metaclass().name, 'ABCMeta') def test_nested_metaclass(self): + if PY3K: + self.skipTest('__metaclass__ syntax is python2-specific') astroid = abuilder.string_build(dedent(""" from abc import ABCMeta class A(object): @@ -800,8 +806,9 @@ def g2(): self.assertEqual(c.metaclass().name, 'type') self.assertEqual(d.metaclass().name, 'ABCMeta') - @require_version('2.7') def test_parent_metaclass(self): + if PY3K: + self.skipTest('__metaclass__ syntax is python2-specific') astroid = abuilder.string_build(dedent(""" from abc import ABCMeta class Test: @@ -815,6 +822,8 @@ def g2(): self.assertEqual(metaclass.name, 'ABCMeta') def test_metaclass_ancestors(self): + if PY3K: + self.skipTest('__metaclass__ syntax is python2-specific') astroid = abuilder.string_build(dedent(""" from abc import ABCMeta -- cgit v1.2.1 From bae8340f044ed41e9b7cfde8d5a8880860862f67 Mon Sep 17 00:00:00 2001 From: Julien Cristau Date: Mon, 30 Jun 2014 11:26:59 +0200 Subject: test: Add test_parent_metaclass for python3 --- test/unittest_python3.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'test') diff --git a/test/unittest_python3.py b/test/unittest_python3.py index d93deacf..295984a8 100644 --- a/test/unittest_python3.py +++ b/test/unittest_python3.py @@ -140,6 +140,19 @@ class Python3TC(TestCase): klass = astroid['Meta'] self.assertIsNone(klass.metaclass()) + @require_version('3.0') + def test_parent_metaclass(self): + astroid = self.builder.string_build(dedent(""" + from abc import ABCMeta + class Test(metaclass=ABCMeta): pass + class SubTest(Test): pass + """)) + klass = astroid['SubTest'] + self.assertTrue(klass.newstyle) + metaclass = klass.metaclass() + self.assertIsInstance(metaclass, Class) + self.assertEqual(metaclass.name, 'ABCMeta') + @require_version('3.0') def test_metaclass_ancestors(self): astroid = self.builder.string_build(dedent(""" -- cgit v1.2.1