Skip to content

Commit d1894dc

Browse files
committed
Add match parameter to pytest.warns()
1 parent 234875b commit d1894dc

28 files changed

+80
-69
lines changed

Tests/helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def _cached_hopper(mode: str) -> Image.Image:
272272
else:
273273
im = hopper()
274274
if mode.startswith("BGR;"):
275-
with pytest.warns(DeprecationWarning):
275+
with pytest.warns(DeprecationWarning, match="BGR;"):
276276
im = im.convert(mode)
277277
else:
278278
try:

Tests/test_core_resources.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,5 @@ def test_units(self) -> None:
188188
),
189189
)
190190
def test_warnings(self, var: dict[str, str]) -> None:
191-
with pytest.warns(UserWarning):
191+
with pytest.warns(UserWarning, match=list(var)[0]):
192192
Image._apply_env_variables(var)

Tests/test_features.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_check() -> None:
1919
assert features.check_codec(codec) == features.check(codec)
2020
for feature in features.features:
2121
if "webp" in feature:
22-
with pytest.warns(DeprecationWarning):
22+
with pytest.warns(DeprecationWarning, match="webp"):
2323
assert features.check_feature(feature) == features.check(feature)
2424
else:
2525
assert features.check_feature(feature) == features.check(feature)
@@ -49,24 +49,24 @@ def test(name: str, function: Callable[[str], str | None]) -> None:
4949
test(codec, features.version_codec)
5050
for feature in features.features:
5151
if "webp" in feature:
52-
with pytest.warns(DeprecationWarning):
52+
with pytest.warns(DeprecationWarning, match="webp"):
5353
test(feature, features.version_feature)
5454
else:
5555
test(feature, features.version_feature)
5656

5757

5858
def test_webp_transparency() -> None:
59-
with pytest.warns(DeprecationWarning):
59+
with pytest.warns(DeprecationWarning, match="transp_webp"):
6060
assert (features.check("transp_webp") or False) == features.check_module("webp")
6161

6262

6363
def test_webp_mux() -> None:
64-
with pytest.warns(DeprecationWarning):
64+
with pytest.warns(DeprecationWarning, match="webp_mux"):
6565
assert (features.check("webp_mux") or False) == features.check_module("webp")
6666

6767

6868
def test_webp_anim() -> None:
69-
with pytest.warns(DeprecationWarning):
69+
with pytest.warns(DeprecationWarning, match="webp_anim"):
7070
assert (features.check("webp_anim") or False) == features.check_module("webp")
7171

7272

@@ -95,10 +95,9 @@ def test_check_codecs(feature: str) -> None:
9595

9696

9797
def test_check_warns_on_nonexistent() -> None:
98-
with pytest.warns(UserWarning) as cm:
98+
with pytest.warns(UserWarning, match="Unknown feature 'typo'."):
9999
has_feature = features.check("typo")
100100
assert has_feature is False
101-
assert str(cm[-1].message) == "Unknown feature 'typo'."
102101

103102

104103
def test_supported_modules() -> None:

Tests/test_file_apng.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def test_apng_chunk_errors() -> None:
303303
assert isinstance(im, PngImagePlugin.PngImageFile)
304304
assert not im.is_animated
305305

306-
with pytest.warns(UserWarning):
306+
with pytest.warns(UserWarning, match="Invalid APNG"): # noqa: PT031
307307
with Image.open("Tests/images/apng/chunk_multi_actl.png") as im:
308308
im.load()
309309
assert isinstance(im, PngImagePlugin.PngImageFile)
@@ -330,14 +330,14 @@ def test_apng_chunk_errors() -> None:
330330

331331

332332
def test_apng_syntax_errors() -> None:
333-
with pytest.warns(UserWarning):
333+
with pytest.warns(UserWarning, match="Invalid APNG"): # noqa: PT031
334334
with Image.open("Tests/images/apng/syntax_num_frames_zero.png") as im:
335335
assert isinstance(im, PngImagePlugin.PngImageFile)
336336
assert not im.is_animated
337337
with pytest.raises(OSError):
338338
im.load()
339339

340-
with pytest.warns(UserWarning):
340+
with pytest.warns(UserWarning, match="Invalid APNG"): # noqa: PT031
341341
with Image.open("Tests/images/apng/syntax_num_frames_zero_default.png") as im:
342342
assert isinstance(im, PngImagePlugin.PngImageFile)
343343
assert not im.is_animated
@@ -354,7 +354,7 @@ def test_apng_syntax_errors() -> None:
354354
im.seek(im.n_frames - 1)
355355
im.load()
356356

357-
with pytest.warns(UserWarning):
357+
with pytest.warns(UserWarning, match="Invalid APNG"): # noqa: PT031
358358
with Image.open("Tests/images/apng/syntax_num_frames_invalid.png") as im:
359359
assert isinstance(im, PngImagePlugin.PngImageFile)
360360
assert not im.is_animated

Tests/test_file_avif.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ class TestUnsupportedAvif:
7777
def test_unsupported(self, monkeypatch: pytest.MonkeyPatch) -> None:
7878
monkeypatch.setattr(AvifImagePlugin, "SUPPORTED", False)
7979

80-
with pytest.warns(UserWarning):
81-
with pytest.raises(UnidentifiedImageError):
80+
with pytest.raises(UnidentifiedImageError):
81+
with pytest.warns(UserWarning, match="AVIF support not installed"):
8282
with Image.open(TEST_AVIF_FILE):
8383
pass
8484

Tests/test_file_gif.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,9 @@ def test_removed_transparency(tmp_path: Path) -> None:
12291229
im.putpixel((x, 0), (x, 0, 0))
12301230

12311231
im.info["transparency"] = (255, 255, 255)
1232-
with pytest.warns(UserWarning):
1232+
with pytest.warns(
1233+
UserWarning, match="Couldn't allocate palette entry for transparency"
1234+
):
12331235
im.save(out)
12341236

12351237
with Image.open(out) as reloaded:
@@ -1251,7 +1253,7 @@ def test_rgb_transparency(tmp_path: Path) -> None:
12511253
im = Image.new("RGB", (1, 1))
12521254
im.info["transparency"] = b""
12531255
ims = [Image.new("RGB", (1, 1))]
1254-
with pytest.warns(UserWarning):
1256+
with pytest.warns(UserWarning, match="should be converted to RGBA images"):
12551257
im.save(out, save_all=True, append_images=ims)
12561258

12571259
with Image.open(out) as reloaded:

Tests/test_file_icns.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ def test_sizes() -> None:
9595
for w, h, r in im.info["sizes"]:
9696
wr = w * r
9797
hr = h * r
98-
with pytest.warns(DeprecationWarning):
98+
with pytest.warns(
99+
DeprecationWarning, match=r"Setting size to \(width, height, scale\)"
100+
):
99101
im.size = (w, h, r)
100102
im.load()
101103
assert im.mode == "RGBA"

Tests/test_file_ico.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def test_save_append_images(tmp_path: Path) -> None:
233233
def test_unexpected_size() -> None:
234234
# This image has been manually hexedited to state that it is 16x32
235235
# while the image within is still 16x16
236-
with pytest.warns(UserWarning):
236+
with pytest.warns(UserWarning, match="Image was not the expected size"):
237237
with Image.open("Tests/images/hopper_unexpected.ico") as im:
238238
assert im.size == (16, 16)
239239

Tests/test_file_iptc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_i() -> None:
9999
c = b"a"
100100

101101
# Act
102-
with pytest.warns(DeprecationWarning):
102+
with pytest.warns(DeprecationWarning, match="IptcImagePlugin.i"):
103103
ret = IptcImagePlugin.i(c)
104104

105105
# Assert
@@ -114,13 +114,13 @@ def test_dump(monkeypatch: pytest.MonkeyPatch) -> None:
114114
monkeypatch.setattr(sys, "stdout", mystdout)
115115

116116
# Act
117-
with pytest.warns(DeprecationWarning):
117+
with pytest.warns(DeprecationWarning, match="IptcImagePlugin.dump"):
118118
IptcImagePlugin.dump(c)
119119

120120
# Assert
121121
assert mystdout.getvalue() == "61 62 63 \n"
122122

123123

124124
def test_pad_deprecation() -> None:
125-
with pytest.warns(DeprecationWarning):
125+
with pytest.warns(DeprecationWarning, match="IptcImagePlugin.PAD"):
126126
assert IptcImagePlugin.PAD == b"\0\0\0\0"

Tests/test_file_jpeg.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -752,10 +752,11 @@ def test_bad_mpo_header(self) -> None:
752752

753753
# Act
754754
# Shouldn't raise error
755-
fn = "Tests/images/sugarshack_bad_mpo_header.jpg"
756-
with pytest.warns(UserWarning, Image.open, fn) as im:
757-
# Assert
758-
assert im.format == "JPEG"
755+
with pytest.warns(UserWarning, match="malformed MPO file"):
756+
im = Image.open("Tests/images/sugarshack_bad_mpo_header.jpg")
757+
758+
# Assert
759+
assert im.format == "JPEG"
759760

760761
@pytest.mark.parametrize("mode", ("1", "L", "RGB", "RGBX", "CMYK", "YCbCr"))
761762
def test_save_correct_modes(self, mode: str) -> None:
@@ -1103,9 +1104,9 @@ def test_repr_jpeg_error_returns_none(self) -> None:
11031104
def test_deprecation(self) -> None:
11041105
with Image.open(TEST_FILE) as im:
11051106
assert isinstance(im, JpegImagePlugin.JpegImageFile)
1106-
with pytest.warns(DeprecationWarning):
1107+
with pytest.warns(DeprecationWarning, match="huffman_ac"):
11071108
assert im.huffman_ac == {}
1108-
with pytest.warns(DeprecationWarning):
1109+
with pytest.warns(DeprecationWarning, match="huffman_dc"):
11091110
assert im.huffman_dc == {}
11101111

11111112

0 commit comments

Comments
 (0)