Skip to content

Commit a370209

Browse files
authored
Add match parameter to pytest.warns() (#9038)
2 parents 69c0c42 + 958c449 commit a370209

29 files changed

+106
-90
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: v0.11.12
3+
rev: v0.12.0
44
hooks:
55
- id: ruff-check
66
args: [--exit-non-zero-on-fix]
@@ -11,7 +11,7 @@ repos:
1111
- id: black
1212

1313
- repo: https://github.com/PyCQA/bandit
14-
rev: 1.8.3
14+
rev: 1.8.5
1515
hooks:
1616
- id: bandit
1717
args: [--severity-level=high]
@@ -24,7 +24,7 @@ repos:
2424
exclude: (Makefile$|\.bat$|\.cmake$|\.eps$|\.fits$|\.gd$|\.opt$)
2525

2626
- repo: https://github.com/pre-commit/mirrors-clang-format
27-
rev: v20.1.5
27+
rev: v20.1.6
2828
hooks:
2929
- id: clang-format
3030
types: [c]
@@ -51,7 +51,7 @@ repos:
5151
exclude: ^.github/.*TEMPLATE|^Tests/(fonts|images)/
5252

5353
- repo: https://github.com/python-jsonschema/check-jsonschema
54-
rev: 0.33.0
54+
rev: 0.33.1
5555
hooks:
5656
- id: check-github-workflows
5757
- id: check-readthedocs

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: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,11 @@ def test_apng_chunk_errors() -> None:
303303
assert isinstance(im, PngImagePlugin.PngImageFile)
304304
assert not im.is_animated
305305

306-
with pytest.warns(UserWarning):
307-
with Image.open("Tests/images/apng/chunk_multi_actl.png") as im:
308-
im.load()
309-
assert isinstance(im, PngImagePlugin.PngImageFile)
310-
assert not im.is_animated
306+
with pytest.warns(UserWarning, match="Invalid APNG"):
307+
im = Image.open("Tests/images/apng/chunk_multi_actl.png")
308+
assert isinstance(im, PngImagePlugin.PngImageFile)
309+
assert not im.is_animated
310+
im.close()
311311

312312
with Image.open("Tests/images/apng/chunk_actl_after_idat.png") as im:
313313
assert isinstance(im, PngImagePlugin.PngImageFile)
@@ -330,18 +330,20 @@ def test_apng_chunk_errors() -> None:
330330

331331

332332
def test_apng_syntax_errors() -> None:
333-
with pytest.warns(UserWarning):
334-
with Image.open("Tests/images/apng/syntax_num_frames_zero.png") as im:
335-
assert isinstance(im, PngImagePlugin.PngImageFile)
336-
assert not im.is_animated
337-
with pytest.raises(OSError):
338-
im.load()
333+
with pytest.warns(UserWarning, match="Invalid APNG"):
334+
im = Image.open("Tests/images/apng/syntax_num_frames_zero.png")
335+
assert isinstance(im, PngImagePlugin.PngImageFile)
336+
assert not im.is_animated
337+
with pytest.raises(OSError):
338+
im.load()
339+
im.close()
339340

340-
with pytest.warns(UserWarning):
341-
with Image.open("Tests/images/apng/syntax_num_frames_zero_default.png") as im:
342-
assert isinstance(im, PngImagePlugin.PngImageFile)
343-
assert not im.is_animated
344-
im.load()
341+
with pytest.warns(UserWarning, match="Invalid APNG"):
342+
im = Image.open("Tests/images/apng/syntax_num_frames_zero_default.png")
343+
assert isinstance(im, PngImagePlugin.PngImageFile)
344+
assert not im.is_animated
345+
im.load()
346+
im.close()
345347

346348
# we can handle this case gracefully
347349
with Image.open("Tests/images/apng/syntax_num_frames_low.png") as im:
@@ -354,11 +356,12 @@ def test_apng_syntax_errors() -> None:
354356
im.seek(im.n_frames - 1)
355357
im.load()
356358

357-
with pytest.warns(UserWarning):
358-
with Image.open("Tests/images/apng/syntax_num_frames_invalid.png") as im:
359-
assert isinstance(im, PngImagePlugin.PngImageFile)
360-
assert not im.is_animated
361-
im.load()
359+
with pytest.warns(UserWarning, match="Invalid APNG"):
360+
im = Image.open("Tests/images/apng/syntax_num_frames_invalid.png")
361+
assert isinstance(im, PngImagePlugin.PngImageFile)
362+
assert not im.is_animated
363+
im.load()
364+
im.close()
362365

363366

364367
@pytest.mark.parametrize(

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
@@ -1232,7 +1232,9 @@ def test_removed_transparency(tmp_path: Path) -> None:
12321232
im.putpixel((x, 0), (x, 0, 0))
12331233

12341234
im.info["transparency"] = (255, 255, 255)
1235-
with pytest.warns(UserWarning):
1235+
with pytest.warns(
1236+
UserWarning, match="Couldn't allocate palette entry for transparency"
1237+
):
12361238
im.save(out)
12371239

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

12601262
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
@@ -234,7 +234,7 @@ def test_save_append_images(tmp_path: Path) -> None:
234234
def test_unexpected_size() -> None:
235235
# This image has been manually hexedited to state that it is 16x32
236236
# while the image within is still 16x16
237-
with pytest.warns(UserWarning):
237+
with pytest.warns(UserWarning, match="Image was not the expected size"):
238238
with Image.open("Tests/images/hopper_unexpected.ico") as im:
239239
assert im.size == (16, 16)
240240

Tests/test_file_iptc.py

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

104104
# Act
105-
with pytest.warns(DeprecationWarning):
105+
with pytest.warns(DeprecationWarning, match="IptcImagePlugin.i"):
106106
ret = IptcImagePlugin.i(c)
107107

108108
# Assert
@@ -117,13 +117,13 @@ def test_dump(monkeypatch: pytest.MonkeyPatch) -> None:
117117
monkeypatch.setattr(sys, "stdout", mystdout)
118118

119119
# Act
120-
with pytest.warns(DeprecationWarning):
120+
with pytest.warns(DeprecationWarning, match="IptcImagePlugin.dump"):
121121
IptcImagePlugin.dump(c)
122122

123123
# Assert
124124
assert mystdout.getvalue() == "61 62 63 \n"
125125

126126

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

0 commit comments

Comments
 (0)