A decorator to deprecate a function and provide a new one as replacement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #!/usr/bin/env python
"""
Deprecated decorator.
Author: Giampaolo Rodola' <g.rodola [AT] gmail [DOT] com>
License: MIT
"""
import warnings
import functools
def deprecated(replacement=None):
"""A decorator which can be used to mark functions as deprecated.
replacement is a callable that will be called with the same args
as the decorated function.
>>> @deprecated()
... def foo(x):
... return x
...
>>> ret = foo(1)
DeprecationWarning: foo is deprecated
>>> ret
1
>>>
>>>
>>> def newfun(x):
... return 0
...
>>> @deprecated(newfun)
... def foo(x):
... return x
...
>>> ret = foo(1)
DeprecationWarning: foo is deprecated; use newfun instead
>>> ret
0
>>>
"""
def outer(fun):
msg = "psutil.%s is deprecated" % fun.__name__
if replacement is not None:
msg += "; use %s instead" % replacement
if fun.__doc__ is None:
fun.__doc__ = msg
@functools.wraps(fun)
def inner(*args, **kwargs):
warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
return fun(*args, **kwargs)
return inner
return outer
if __name__ == '__main__':
# --- new function
def sum_many(*args):
return sum(args)
# --- old / deprecated function
@deprecated(sum_many)
def sum_couple(a, b):
return a + b
print sum_couple(2, 2)
|