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