Skip to content

Instantly share code, notes, and snippets.

@lucgiffon
Last active November 29, 2018 23:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucgiffon/79b934fd289f98ef416e3caf17a95062 to your computer and use it in GitHub Desktop.
Save lucgiffon/79b934fd289f98ef416e3caf17a95062 to your computer and use it in GitHub Desktop.
deprecated decorator
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