Last active
November 29, 2018 23:55
-
-
Save lucgiffon/79b934fd289f98ef416e3caf17a95062 to your computer and use it in GitHub Desktop.
deprecated 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
import warnings | |
def deprecated(msg=""): | |
def inner(func): | |
""" | |
This is a decorator which can be used to mark functions | |
as deprecated. It will result in a warning being emitted | |
when the function is used. | |
This decorator must take 0 or 1 arguments, e.g. even if you don't want | |
to use a custom message, write the parenthesis: | |
@deprecated("test_depr never worked") | |
def test_depr(): | |
return 1 | |
or | |
@deprecated() | |
def test_depr(): | |
return 1 | |
""" | |
def new_func(*args, **kwargs): | |
s = "Call to deprecated function {}".format(func.__name__) | |
if str(msg).strip != "": | |
s += ": {}.".format(msg) | |
else: | |
s += "." | |
warnings.warn(s) | |
return func(*args, **kwargs) | |
new_func.__name__ = func.__name__ | |
new_func.__doc__ = func.__doc__ | |
new_func.__dict__.update(func.__dict__) | |
return new_func | |
return inner |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment