_deprecate.py 1.9 KB

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