From 4a94c14c50ce6150cf9b29395c592d43f79ed569 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 2 Jan 2017 22:22:22 -0500 Subject: [PATCH 1/2] Add test capturing known limitation on add_metaclass. Ref #127. --- test_six.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test_six.py b/test_six.py index b8543a93e..c4a9ebe9c 100644 --- a/test_six.py +++ b/test_six.py @@ -833,6 +833,30 @@ class MySlotsWeakref(object): assert type(MySlotsWeakref) is Meta +def test_add_metaclass_child_meta(): + """ + Capture the known failure in the rare case where the parent + has a metaclass that is a superclass of the metaclass used + with the decorator (#127). + """ + class MetaA(type): pass + class MetaB(type): pass + + class A(object): pass + A = six.add_metaclass(MetaA)(A) + + class B(object): pass + B = six.add_metaclass(MetaB)(B) + + class MetaC(MetaA, MetaB): pass + + with py.test.raises(TypeError) as exc: + class C(A, B): pass + C = six.add_metaclass(MetaC)(C) + + assert 'metaclass conflict' in str(exc.value) + + @py.test.mark.skipif("sys.version_info[:2] < (2, 7) or sys.version_info[:2] in ((3, 0), (3, 1))") def test_assertCountEqual(): class TestAssertCountEqual(unittest.TestCase): From 85b52133a5e77d0fdb142b9fc3605368a1c43387 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 2 Jan 2017 22:27:03 -0500 Subject: [PATCH 2/2] Update documentation to capture the caveat about suitable usage. Ref #127. --- documentation/index.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/documentation/index.rst b/documentation/index.rst index 30374519a..bde2c8731 100644 --- a/documentation/index.rst +++ b/documentation/index.rst @@ -356,6 +356,12 @@ Python 2 and 3. pass MyClass = add_metaclass(Meta)(MyClass) + This technique works by re-building MyClass from the indicated metaclass, + but in the rare case where the parent has a metaclass that is a + superclass of the metaclass used with the decorator, this approach will + fail with a TypeError. See `issue 127 + `_ for details. + Binary and text data >>>>>>>>>>>>>>>>>>>>