summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Peuch <cortex@worlddomination.be>2020-05-07 22:22:42 +0200
committerLaurent Peuch <cortex@worlddomination.be>2020-05-07 22:22:42 +0200
commit4a94989c06e8693be7bc3859418a0049d3ef4ae6 (patch)
tree6174858583c545e1f2da946132fc10e4fae25aa9
parent95a99fdd10aae7ff0d30bd029d633dfe36689ad0 (diff)
downloadlogilab-common-4a94989c06e8693be7bc3859418a0049d3ef4ae6.tar.gz
[fix] metaclass conflict for class_deprecated
Actually the old compatibility code actually prevented this bug so back it up.
-rw-r--r--logilab/common/deprecation.py20
-rw-r--r--test/unittest_deprecation.py11
2 files changed, 30 insertions, 1 deletions
diff --git a/logilab/common/deprecation.py b/logilab/common/deprecation.py
index b147b43..6edc71c 100644
--- a/logilab/common/deprecation.py
+++ b/logilab/common/deprecation.py
@@ -163,7 +163,25 @@ def callable_deprecated(reason=None, version=None, stacklevel=2):
deprecated = callable_renamed(old_name="deprecated", new_function=callable_deprecated)
-class class_deprecated(type):
+def class_deprecated(old_name, parents, class_dict):
+ try:
+ return _class_deprecated(old_name, parents, clsdict)
+ except (NameError, TypeError):
+ # in case of conflicting metaclass situation
+ class DeprecatedClass(*parents):
+ def __init__(self, *args, **kwargs):
+ msg = class_dict.get("__deprecation_warning__", f"{old_name} is deprecated")
+ send_warning(
+ msg,
+ stacklevel=class_dict.get("__deprecation_warning_stacklevel__", 3),
+ version=class_dict.get("__deprecation_warning_version__", None),
+ )
+ super(DeprecatedClass, self).__init__(*args, **kwargs)
+
+ return DeprecatedClass
+
+
+class _class_deprecated(type):
"""metaclass to print a warning on instantiation of a deprecated class"""
def __call__(cls, *args, **kwargs):
diff --git a/test/unittest_deprecation.py b/test/unittest_deprecation.py
index 096978e..172b9fb 100644
--- a/test/unittest_deprecation.py
+++ b/test/unittest_deprecation.py
@@ -61,6 +61,17 @@ class RawInputTC(TestCase):
self.assertEqual(self.messages,
['[logilab.common] OldClass is deprecated, use AnyClass instead'])
+ def test_class_renamed_conflict_metaclass(self):
+ class SomeMetaClass(type):
+ pass
+
+ class AnyClass(metaclass=SomeMetaClass):
+ pass
+
+ # make sure the "metaclass conflict: the metaclass of a derived class # must be a
+ # (non-strict) subclass of the metaclasses of all its bases" exception won't be raised
+ deprecation.class_renamed("OldClass", AnyClass)
+
def test_class_moved(self):
class AnyClass(object):
pass