Skip to content

Instantly share code, notes, and snippets.

@whitekid
Last active November 17, 2017 07:11
Show Gist options
  • Save whitekid/cfa0a569bc6e0d10f3735b26add346f6 to your computer and use it in GitHub Desktop.
Save whitekid/cfa0a569bc6e0d10f3735b26add346f6 to your computer and use it in GitHub Desktop.
depreciated decorator
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