__init__.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright (c) 2010-2024 openpyxl
  2. from .numbers import NUMERIC_TYPES
  3. from .strings import safe_string
  4. import warnings
  5. from functools import wraps
  6. import inspect
  7. class DummyCode:
  8. pass
  9. # from https://github.com/tantale/deprecated/blob/master/deprecated/__init__.py
  10. # with an enhancement to update docstrings of deprecated functions
  11. string_types = (type(b''), type(u''))
  12. def deprecated(reason):
  13. if isinstance(reason, string_types):
  14. def decorator(func1):
  15. if inspect.isclass(func1):
  16. fmt1 = "Call to deprecated class {name} ({reason})."
  17. else:
  18. fmt1 = "Call to deprecated function {name} ({reason})."
  19. @wraps(func1)
  20. def new_func1(*args, **kwargs):
  21. #warnings.simplefilter('default', DeprecationWarning)
  22. warnings.warn(
  23. fmt1.format(name=func1.__name__, reason=reason),
  24. category=DeprecationWarning,
  25. stacklevel=2
  26. )
  27. return func1(*args, **kwargs)
  28. # Enhance docstring with a deprecation note
  29. deprecationNote = "\n\n.. note::\n Deprecated: " + reason
  30. if new_func1.__doc__:
  31. new_func1.__doc__ += deprecationNote
  32. else:
  33. new_func1.__doc__ = deprecationNote
  34. return new_func1
  35. return decorator
  36. elif inspect.isclass(reason) or inspect.isfunction(reason):
  37. raise TypeError("Reason for deprecation must be supplied")
  38. else:
  39. raise TypeError(repr(type(reason)))