From 525e9fa19ec2380141e4f98d8d4084ead99a174b Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Thu, 21 Dec 2023 13:02:21 +0200 Subject: [PATCH] Subclass DeprecationWarning --- Tests/test_deprecate.py | 6 +++--- src/PIL/_deprecate.py | 13 ++++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Tests/test_deprecate.py b/Tests/test_deprecate.py index d45a6603c54..9270a0c4d04 100644 --- a/Tests/test_deprecate.py +++ b/Tests/test_deprecate.py @@ -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) @@ -80,7 +80,7 @@ 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) @@ -88,5 +88,5 @@ 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) diff --git a/src/PIL/_deprecate.py b/src/PIL/_deprecate.py index 33a0e07b3f4..a5a513becbd 100644 --- a/src/PIL/_deprecate.py +++ b/src/PIL/_deprecate.py @@ -5,6 +5,14 @@ from . import __version__ +class RemovedInPillow11Warning(DeprecationWarning): + pass + + +class RemovedInPillow12Warning(DeprecationWarning): + pass + + def deprecate( deprecated: str, when: int | None, @@ -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) @@ -66,6 +77,6 @@ def deprecate( warnings.warn( f"{deprecated} {is_} deprecated and will be removed in {removed}{action}", - DeprecationWarning, + category, stacklevel=3, )