Last active
November 17, 2017 07:11
-
-
Save whitekid/cfa0a569bc6e0d10f3735b26add346f6 to your computer and use it in GitHub Desktop.
depreciated decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect | |
import warnings | |
from functools import wraps | |
def decorator(func_or_class): | |
warnings.filterwarnings('default', category=DeprecationWarning) | |
is_class = inspect.isclass(func_or_class) | |
if is_class: | |
type_name = 'class' | |
else: | |
type_name = type(func_or_class).__name__ | |
if is_class: | |
old_init = func_or_class.__init__ | |
def init(self, *args, **kwargs): | |
warnings.warn( | |
"{} {}: {}".format(type_name, func_or_class.__name__, | |
message), | |
DeprecationWarning, | |
stacklevel=2) | |
old_init(self, *args, **kwargs) | |
func_or_class.__init__ = init | |
return func_or_class | |
else: | |
@wraps(func_or_class) | |
def wrapper(*args, **kwargs): | |
warnings.warn( | |
"{} {}: {}".format(type_name, func_or_class.__name__, | |
message), | |
DeprecationWarning, | |
stacklevel=2) | |
return func_or_class(*args, **kwargs) | |
return wrapper | |
return func_or_class | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment