utility for showing a warning when a class is subclassed

This commit is contained in:
Mikhail Korobov 2013-12-28 00:46:58 +06:00
parent 74433a17e7
commit 439a141aae
1 changed files with 14 additions and 0 deletions

View File

@ -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