Last active
July 12, 2022 16:27
-
-
Save Mukhopadhyay/620b8e5f95f371793bb614fe6860b8ec to your computer and use it in GitHub Desktop.
Deprecation warning decorator in python!
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
import warnings | |
import functools | |
def deprecated(function): | |
""" | |
This decorator which can be used to mark functions | |
as deprecated. It will result in a warning being emitted | |
when the function is used. | |
Args: | |
function: function: Function to be overridden! | |
Returns: | |
[object]: Whatever the "function" was supposed to return! | |
""" | |
@functools.wraps(function) | |
def new_function(*args, **kwargs): | |
warnings.simplefilter('always', DeprecationWarning) | |
warnings.warn( | |
message='Call to deprecated function {}.'.format(function.__name__), | |
category=DeprecationWarning, | |
stacklevel=2 | |
) | |
warnings.simplefilter('default', DeprecationWarning) | |
return function(*args, **kwargs) | |
return new_function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage