_deprecate.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from __future__ import annotations
  2. __lazy_modules__ = {"warnings"}
  3. import warnings
  4. from . import __version__
  5. def deprecate(
  6. deprecated: str,
  7. when: int | None,
  8. replacement: str | None = None,
  9. *,
  10. action: str | None = None,
  11. plural: bool = False,
  12. stacklevel: int = 3,
  13. ) -> None:
  14. """
  15. Deprecations helper.
  16. :param deprecated: Name of thing to be deprecated.
  17. :param when: Pillow major version to be removed in.
  18. :param replacement: Name of replacement.
  19. :param action: Instead of "replacement", give a custom call to action
  20. e.g. "Upgrade to new thing".
  21. :param plural: if the deprecated thing is plural, needing "are" instead of "is".
  22. Usually of the form:
  23. "[deprecated] is deprecated and will be removed in Pillow [when] (yyyy-mm-dd).
  24. Use [replacement] instead."
  25. You can leave out the replacement sentence:
  26. "[deprecated] is deprecated and will be removed in Pillow [when] (yyyy-mm-dd)"
  27. Or with another call to action:
  28. "[deprecated] is deprecated and will be removed in Pillow [when] (yyyy-mm-dd).
  29. [action]."
  30. """
  31. is_ = "are" if plural else "is"
  32. if when is None:
  33. removed = "a future version"
  34. elif when <= int(__version__.split(".")[0]):
  35. msg = f"{deprecated} {is_} deprecated and should be removed."
  36. raise RuntimeError(msg)
  37. elif when == 14:
  38. removed = "Pillow 14 (2027-10-15)"
  39. elif when == 15:
  40. removed = "Pillow 15 (2028-10-15)"
  41. else:
  42. msg = f"Unknown removal version: {when}. Update {__name__}?"
  43. raise ValueError(msg)
  44. if replacement and action:
  45. msg = "Use only one of 'replacement' and 'action'"
  46. raise ValueError(msg)
  47. if replacement:
  48. action = f". Use {replacement} instead."
  49. elif action:
  50. action = f". {action.rstrip('.')}."
  51. else:
  52. action = ""
  53. warnings.warn(
  54. f"{deprecated} {is_} deprecated and will be removed in {removed}{action}",
  55. DeprecationWarning,
  56. stacklevel=stacklevel,
  57. )