Skip to content

Instantly share code, notes, and snippets.

@Mukhopadhyay
Last active July 12, 2022 16:27
Show Gist options
  • Save Mukhopadhyay/620b8e5f95f371793bb614fe6860b8ec to your computer and use it in GitHub Desktop.
Save Mukhopadhyay/620b8e5f95f371793bb614fe6860b8ec to your computer and use it in GitHub Desktop.
Deprecation warning decorator in python!
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
@Mukhopadhyay
Copy link
Author

Usage

@deprecated
def add(a: int, b: int) -> int:
    return a + b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment