Skip to content

Commit

Permalink
Subclass DeprecationWarning
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jan 4, 2024
1 parent 865a23a commit 525e9fa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Tests/test_deprecate.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_plural():
r"Old things are deprecated and will be removed in Pillow 11 \(2024-10-15\)\. "
r"Use new thing instead\."
)
with pytest.warns(DeprecationWarning, match=expected):
with pytest.warns(_deprecate.RemovedInPillow11Warning, match=expected):
_deprecate.deprecate("Old things", 11, "new thing", plural=True)


Expand All @@ -80,13 +80,13 @@ def test_action(action):
r"Old thing is deprecated and will be removed in Pillow 11 \(2024-10-15\)\. "
r"Upgrade to new thing\."
)
with pytest.warns(DeprecationWarning, match=expected):
with pytest.warns(_deprecate.RemovedInPillow11Warning, match=expected):
_deprecate.deprecate("Old thing", 11, action=action)


def test_no_replacement_or_action():
expected = (
r"Old thing is deprecated and will be removed in Pillow 11 \(2024-10-15\)"
)
with pytest.warns(DeprecationWarning, match=expected):
with pytest.warns(_deprecate.RemovedInPillow11Warning, match=expected):
_deprecate.deprecate("Old thing", 11)
13 changes: 12 additions & 1 deletion src/PIL/_deprecate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
from . import __version__


class RemovedInPillow11Warning(DeprecationWarning):
pass


class RemovedInPillow12Warning(DeprecationWarning):
pass


def deprecate(
deprecated: str,
when: int | None,
Expand Down Expand Up @@ -42,13 +50,16 @@ def deprecate(

if when is None:
removed = "a future version"
category = DeprecationWarning
elif when <= int(__version__.split(".")[0]):
msg = f"{deprecated} {is_} deprecated and should be removed."
raise RuntimeError(msg)
elif when == 11:
removed = "Pillow 11 (2024-10-15)"
category = RemovedInPillow11Warning
elif when == 12:
removed = "Pillow 12 (2025-10-15)"
category = RemovedInPillow12Warning
else:
msg = f"Unknown removal version: {when}. Update {__name__}?"
raise ValueError(msg)
Expand All @@ -66,6 +77,6 @@ def deprecate(

warnings.warn(
f"{deprecated} {is_} deprecated and will be removed in {removed}{action}",
DeprecationWarning,
category,
stacklevel=3,
)

0 comments on commit 525e9fa

Please sign in to comment.