From 31dd3dc4a1060e5e2ab053270e31529aa60819c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gra=C3=B1a?= Date: Fri, 10 Jan 2014 17:42:52 -0200 Subject: [PATCH] allow deprecation of subclasses of already deprecated classes --- scrapy/tests/test_utils_deprecate.py | 24 ++++++++++++++++++++++++ scrapy/utils/deprecate.py | 3 +-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/scrapy/tests/test_utils_deprecate.py b/scrapy/tests/test_utils_deprecate.py index 8cc3a26f1..75a53b2bf 100644 --- a/scrapy/tests/test_utils_deprecate.py +++ b/scrapy/tests/test_utils_deprecate.py @@ -201,3 +201,27 @@ class WarnWhenSubclassedTest(unittest.TestCase): Meta1 = type('Meta1', (type,), {}) New = Meta1('New', (), {}) Deprecated = create_deprecated_class('Deprecated', New) + + def test_deprecate_subclass_of_deprecated_class(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + Deprecated = create_deprecated_class('Deprecated', NewName, + warn_category=MyWarning) + AlsoDeprecated = create_deprecated_class('AlsoDeprecated', Deprecated, + new_class_path='foo.Bar', + warn_category=MyWarning) + + w = self._mywarnings(w) + self.assertEqual(len(w), 0, str(map(str, w))) + + with warnings.catch_warnings(record=True) as w: + AlsoDeprecated() + class UserClass(AlsoDeprecated): + pass + + w = self._mywarnings(w) + self.assertEqual(len(w), 2) + self.assertIn('AlsoDeprecated', str(w[0].message)) + self.assertIn('foo.Bar', str(w[0].message)) + self.assertIn('AlsoDeprecated', str(w[1].message)) + self.assertIn('foo.Bar', str(w[1].message)) diff --git a/scrapy/utils/deprecate.py b/scrapy/utils/deprecate.py index 47d97e9cb..edaecc3d3 100644 --- a/scrapy/utils/deprecate.py +++ b/scrapy/utils/deprecate.py @@ -87,8 +87,7 @@ def create_deprecated_class(name, new_class, clsdict=None, return any(c in candidates for c in mro) def __call__(cls, *args, **kwargs): - meta = cls.__class__ - old = meta.deprecated_class + old = DeprecatedClass.deprecated_class if cls is old: msg = instance_warn_message.format(cls=_clspath(cls, old_class_path), new=_clspath(new_class, new_class_path))