diff --git a/scrapy/utils/deprecate.py b/scrapy/utils/deprecate.py index a5850b6e8..3b67a3d7d 100644 --- a/scrapy/utils/deprecate.py +++ b/scrapy/utils/deprecate.py @@ -9,3 +9,17 @@ def attribute(obj, oldattr, newattr, version='0.12'): warnings.warn("%s.%s attribute is deprecated and will be no longer supported " "in Scrapy %s, use %s.%s attribute instead" % \ (cname, oldattr, version, cname, newattr), ScrapyDeprecationWarning, stacklevel=3) + + +def warn_when_subclassed(mro_len, message, category=ScrapyDeprecationWarning): + """ + Return a metaclass that causes classes to + issue a warning when they are subclassed. + """ + class Metaclass(type): + def __init__(cls, name, bases, clsdict): + if len(cls.mro()) > mro_len: + warnings.warn(message, category, stacklevel=2) + super(Metaclass, cls).__init__(name, bases, clsdict) + return Metaclass +