deprecation.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python
  2. """Decorators to mark function deprecated."""
  3. import functools
  4. import warnings
  5. def Deprecated(message):
  6. """Returns a decorator with a given warning message."""
  7. def Decorator(func):
  8. """Emits a deprecation warning when the decorated function is called.
  9. Also adds the deprecation message to the function's docstring.
  10. Args:
  11. func: The function to deprecate.
  12. Returns:
  13. func: The wrapped function.
  14. """
  15. @functools.wraps(func)
  16. def Wrapper(*args, **kwargs):
  17. warnings.warn_explicit(
  18. '%s() is deprecated: %s' % (func.__name__, message),
  19. category=DeprecationWarning,
  20. filename=func.__code__.co_filename,
  21. lineno=func.__code__.co_firstlineno + 1)
  22. return func(*args, **kwargs)
  23. deprecation_message = '\nDEPRECATED: ' + message
  24. try:
  25. Wrapper.__doc__ += deprecation_message
  26. # If there are non-ASCII characters in the docs, and we're in
  27. # Python 2, use a hammer to force them into a str.
  28. except UnicodeDecodeError:
  29. Wrapper.__doc__ += deprecation_message.encode('utf8')
  30. return Wrapper
  31. return Decorator
  32. def CanUseDeprecated(func):
  33. """Ignores deprecation warnings emitted while the decorated function runs."""
  34. @functools.wraps(func)
  35. def Wrapper(*args, **kwargs):
  36. with warnings.catch_warnings():
  37. warnings.filterwarnings('ignore', category=DeprecationWarning)
  38. return func(*args, **kwargs)
  39. return Wrapper