Last active
October 17, 2022 09:54
-
-
Save mtovmassian/a80a9db564dc798696c91b15c503ff8d to your computer and use it in GitHub Desktop.
Python deprecation decorator
This file contains 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
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