Skip to content

Instantly share code, notes, and snippets.

@mtovmassian
Last active October 17, 2022 09:54
Show Gist options
  • Save mtovmassian/a80a9db564dc798696c91b15c503ff8d to your computer and use it in GitHub Desktop.
Save mtovmassian/a80a9db564dc798696c91b15c503ff8d to your computer and use it in GitHub Desktop.
Python deprecation decorator
def deprecated(arg):
if callable(arg):
return deprecated_auto_message(arg)
else:
return deprecated_user_message(arg)
def deprecated_auto_message(func):
def wrapper(*args, **kwargs):
message = "Function {} is deprecated.".format(func.__name__)
warn_deprecation(message, 3)
return func(*args, **kwargs)
return wrapper
def deprecated_user_message(message):
def decorator(func):
def wrapper(*args, **kwargs):
warn_deprecation(message, 3)
return func(*args, **kwargs)
return wrapper
return decorator
def warn_deprecation(message, stacklevel):
from warnings import warn
warn(message, DeprecationWarning, stacklevel=stacklevel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment