2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2022-04-05 16:33:20 +03:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from PIL import _deprecate
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"version, expected",
|
|
|
|
[
|
2023-01-20 15:35:11 +03:00
|
|
|
(
|
2024-07-01 13:25:43 +03:00
|
|
|
12,
|
|
|
|
"Old thing is deprecated and will be removed in Pillow 12 "
|
|
|
|
r"\(2025-10-15\)\. Use new thing instead\.",
|
2023-01-20 15:35:11 +03:00
|
|
|
),
|
2022-04-05 16:33:20 +03:00
|
|
|
(
|
|
|
|
None,
|
2022-04-06 13:37:17 +03:00
|
|
|
r"Old thing is deprecated and will be removed in a future version\. "
|
2022-04-05 16:33:20 +03:00
|
|
|
r"Use new thing instead\.",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2024-02-20 07:41:20 +03:00
|
|
|
def test_version(version: int | None, expected: str) -> None:
|
2022-04-05 16:33:20 +03:00
|
|
|
with pytest.warns(DeprecationWarning, match=expected):
|
|
|
|
_deprecate.deprecate("Old thing", version, "new thing")
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_unknown_version() -> None:
|
2023-02-07 03:00:31 +03:00
|
|
|
expected = r"Unknown removal version: 12345. Update PIL\._deprecate\?"
|
2022-04-05 16:33:20 +03:00
|
|
|
with pytest.raises(ValueError, match=expected):
|
|
|
|
_deprecate.deprecate("Old thing", 12345, "new thing")
|
|
|
|
|
|
|
|
|
2022-04-08 14:22:31 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"deprecated, plural, expected",
|
|
|
|
[
|
|
|
|
(
|
|
|
|
"Old thing",
|
|
|
|
False,
|
|
|
|
r"Old thing is deprecated and should be removed\.",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Old things",
|
|
|
|
True,
|
|
|
|
r"Old things are deprecated and should be removed\.",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2024-02-20 07:41:20 +03:00
|
|
|
def test_old_version(deprecated: str, plural: bool, expected: str) -> None:
|
2022-04-08 14:22:31 +03:00
|
|
|
expected = r""
|
|
|
|
with pytest.raises(RuntimeError, match=expected):
|
|
|
|
_deprecate.deprecate(deprecated, 1, plural=plural)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_plural() -> None:
|
2022-04-05 16:33:20 +03:00
|
|
|
expected = (
|
2024-07-01 13:25:43 +03:00
|
|
|
r"Old things are deprecated and will be removed in Pillow 12 \(2025-10-15\)\. "
|
2022-04-05 16:33:20 +03:00
|
|
|
r"Use new thing instead\."
|
|
|
|
)
|
|
|
|
with pytest.warns(DeprecationWarning, match=expected):
|
2024-07-01 13:25:43 +03:00
|
|
|
_deprecate.deprecate("Old things", 12, "new thing", plural=True)
|
2022-04-05 16:33:20 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_replacement_and_action() -> None:
|
2022-04-05 16:33:20 +03:00
|
|
|
expected = "Use only one of 'replacement' and 'action'"
|
|
|
|
with pytest.raises(ValueError, match=expected):
|
|
|
|
_deprecate.deprecate(
|
2024-07-01 13:25:43 +03:00
|
|
|
"Old thing", 12, replacement="new thing", action="Upgrade to new thing"
|
2022-04-05 16:33:20 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"action",
|
|
|
|
[
|
|
|
|
"Upgrade to new thing",
|
|
|
|
"Upgrade to new thing.",
|
|
|
|
],
|
|
|
|
)
|
2024-02-20 07:41:20 +03:00
|
|
|
def test_action(action: str) -> None:
|
2022-04-05 16:33:20 +03:00
|
|
|
expected = (
|
2024-07-01 13:25:43 +03:00
|
|
|
r"Old thing is deprecated and will be removed in Pillow 12 \(2025-10-15\)\. "
|
2022-04-05 16:33:20 +03:00
|
|
|
r"Upgrade to new thing\."
|
|
|
|
)
|
|
|
|
with pytest.warns(DeprecationWarning, match=expected):
|
2024-07-01 13:25:43 +03:00
|
|
|
_deprecate.deprecate("Old thing", 12, action=action)
|
2022-04-05 16:33:20 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_no_replacement_or_action() -> None:
|
2022-04-05 16:33:20 +03:00
|
|
|
expected = (
|
2024-07-01 13:25:43 +03:00
|
|
|
r"Old thing is deprecated and will be removed in Pillow 12 \(2025-10-15\)"
|
2022-04-05 16:33:20 +03:00
|
|
|
)
|
|
|
|
with pytest.warns(DeprecationWarning, match=expected):
|
2024-07-01 13:25:43 +03:00
|
|
|
_deprecate.deprecate("Old thing", 12)
|