Skip to content

Commit 5f36c9a

Browse files
authored
Merge pull request #8801 from radarhere/match
Use match argument
2 parents 7552893 + 3607d1a commit 5f36c9a

9 files changed

+26
-51
lines changed

Tests/test_file_libtiff.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1140,11 +1140,9 @@ def test_sampleformat_not_corrupted(self) -> None:
11401140
def test_realloc_overflow(self, monkeypatch: pytest.MonkeyPatch) -> None:
11411141
monkeypatch.setattr(TiffImagePlugin, "READ_LIBTIFF", True)
11421142
with Image.open("Tests/images/tiff_overflow_rows_per_strip.tif") as im:
1143-
with pytest.raises(OSError) as e:
1144-
im.load()
1145-
11461143
# Assert that the error code is IMAGING_CODEC_MEMORY
1147-
assert str(e.value) == "decoder error -9"
1144+
with pytest.raises(OSError, match="decoder error -9"):
1145+
im.load()
11481146

11491147
@pytest.mark.parametrize("compression", ("tiff_adobe_deflate", "jpeg"))
11501148
def test_save_multistrip(self, compression: str, tmp_path: Path) -> None:

Tests/test_file_ppm.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -293,25 +293,21 @@ def test_header_token_too_long(tmp_path: Path) -> None:
293293
with open(path, "wb") as f:
294294
f.write(b"P6\n 01234567890")
295295

296-
with pytest.raises(ValueError) as e:
296+
with pytest.raises(ValueError, match="Token too long in file header: 01234567890"):
297297
with Image.open(path):
298298
pass
299299

300-
assert str(e.value) == "Token too long in file header: 01234567890"
301-
302300

303301
def test_truncated_file(tmp_path: Path) -> None:
304302
# Test EOF in header
305303
path = str(tmp_path / "temp.pgm")
306304
with open(path, "wb") as f:
307305
f.write(b"P6")
308306

309-
with pytest.raises(ValueError) as e:
307+
with pytest.raises(ValueError, match="Reached EOF while reading header"):
310308
with Image.open(path):
311309
pass
312310

313-
assert str(e.value) == "Reached EOF while reading header"
314-
315311
# Test EOF for PyDecoder
316312
fp = BytesIO(b"P5 3 1 4")
317313
with Image.open(fp) as im:
@@ -335,12 +331,12 @@ def test_invalid_maxval(maxval: bytes, tmp_path: Path) -> None:
335331
with open(path, "wb") as f:
336332
f.write(b"P6\n3 1 " + maxval)
337333

338-
with pytest.raises(ValueError) as e:
334+
with pytest.raises(
335+
ValueError, match="maxval must be greater than 0 and less than 65536"
336+
):
339337
with Image.open(path):
340338
pass
341339

342-
assert str(e.value) == "maxval must be greater than 0 and less than 65536"
343-
344340

345341
def test_neg_ppm() -> None:
346342
# Storage.c accepted negative values for xsize, ysize. the

Tests/test_file_tiff.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ def test_seek_too_large(self) -> None:
134134

135135
def test_set_legacy_api(self) -> None:
136136
ifd = TiffImagePlugin.ImageFileDirectory_v2()
137-
with pytest.raises(Exception) as e:
137+
with pytest.raises(Exception, match="Not allowing setting of legacy api"):
138138
ifd.legacy_api = False
139-
assert str(e.value) == "Not allowing setting of legacy api"
140139

141140
def test_xyres_tiff(self) -> None:
142141
filename = "Tests/images/pil168.tif"

Tests/test_file_webp.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,8 @@ def test_write_unsupported_mode_P(self, tmp_path: Path) -> None:
154154
@pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system")
155155
def test_write_encoding_error_message(self, tmp_path: Path) -> None:
156156
im = Image.new("RGB", (15000, 15000))
157-
with pytest.raises(ValueError) as e:
157+
with pytest.raises(ValueError, match="encoding error 6"):
158158
im.save(tmp_path / "temp.webp", method=0)
159-
assert str(e.value) == "encoding error 6"
160159

161160
@pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system")
162161
def test_write_encoding_error_bad_dimension(self, tmp_path: Path) -> None:

Tests/test_image.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ def test_image_modes_success(self, mode: str) -> None:
6565

6666
@pytest.mark.parametrize("mode", ("", "bad", "very very long"))
6767
def test_image_modes_fail(self, mode: str) -> None:
68-
with pytest.raises(ValueError) as e:
68+
with pytest.raises(ValueError, match="unrecognized image mode"):
6969
Image.new(mode, (1, 1))
70-
assert str(e.value) == "unrecognized image mode"
7170

7271
def test_exception_inheritance(self) -> None:
7372
assert issubclass(UnidentifiedImageError, OSError)

Tests/test_imagedraw.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ def test_compute_regular_polygon_vertices(
16261626
0,
16271627
ValueError,
16281628
"bounding_circle should contain 2D coordinates "
1629-
"and a radius (e.g. (x, y, r) or ((x, y), r) )",
1629+
r"and a radius \(e.g. \(x, y, r\) or \(\(x, y\), r\) \)",
16301630
),
16311631
(
16321632
3,
@@ -1640,7 +1640,7 @@ def test_compute_regular_polygon_vertices(
16401640
((50, 50, 50), 25),
16411641
0,
16421642
ValueError,
1643-
"bounding_circle centre should contain 2D coordinates (e.g. (x, y))",
1643+
r"bounding_circle centre should contain 2D coordinates \(e.g. \(x, y\)\)",
16441644
),
16451645
(
16461646
3,
@@ -1665,9 +1665,8 @@ def test_compute_regular_polygon_vertices_input_error_handling(
16651665
expected_error: type[Exception],
16661666
error_message: str,
16671667
) -> None:
1668-
with pytest.raises(expected_error) as e:
1668+
with pytest.raises(expected_error, match=error_message):
16691669
ImageDraw._compute_regular_polygon_vertices(bounding_circle, n_sides, rotation) # type: ignore[arg-type]
1670-
assert str(e.value) == error_message
16711670

16721671

16731672
def test_continuous_horizontal_edges_polygon() -> None:

Tests/test_imagefile.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,8 @@ def test_truncated(self) -> None:
176176
b"0" * ImageFile.SAFEBLOCK
177177
) # only SAFEBLOCK bytes, so that the header is truncated
178178
)
179-
with pytest.raises(OSError) as e:
179+
with pytest.raises(OSError, match="Truncated File Read"):
180180
BmpImagePlugin.BmpImageFile(b)
181-
assert str(e.value) == "Truncated File Read"
182181

183182
@skip_unless_feature("zlib")
184183
def test_truncated_with_errors(self) -> None:

Tests/test_imagemorph.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,12 @@ def test_lut(op: str) -> None:
8080
def test_no_operator_loaded() -> None:
8181
im = Image.new("L", (1, 1))
8282
mop = ImageMorph.MorphOp()
83-
with pytest.raises(Exception) as e:
83+
with pytest.raises(Exception, match="No operator loaded"):
8484
mop.apply(im)
85-
assert str(e.value) == "No operator loaded"
86-
with pytest.raises(Exception) as e:
85+
with pytest.raises(Exception, match="No operator loaded"):
8786
mop.match(im)
88-
assert str(e.value) == "No operator loaded"
89-
with pytest.raises(Exception) as e:
87+
with pytest.raises(Exception, match="No operator loaded"):
9088
mop.save_lut("")
91-
assert str(e.value) == "No operator loaded"
9289

9390

9491
# Test the named patterns
@@ -238,15 +235,12 @@ def test_incorrect_mode() -> None:
238235
im = hopper("RGB")
239236
mop = ImageMorph.MorphOp(op_name="erosion8")
240237

241-
with pytest.raises(ValueError) as e:
238+
with pytest.raises(ValueError, match="Image mode must be L"):
242239
mop.apply(im)
243-
assert str(e.value) == "Image mode must be L"
244-
with pytest.raises(ValueError) as e:
240+
with pytest.raises(ValueError, match="Image mode must be L"):
245241
mop.match(im)
246-
assert str(e.value) == "Image mode must be L"
247-
with pytest.raises(ValueError) as e:
242+
with pytest.raises(ValueError, match="Image mode must be L"):
248243
mop.get_on_pixels(im)
249-
assert str(e.value) == "Image mode must be L"
250244

251245

252246
def test_add_patterns() -> None:
@@ -279,9 +273,10 @@ def test_pattern_syntax_error() -> None:
279273
lb.add_patterns(new_patterns)
280274

281275
# Act / Assert
282-
with pytest.raises(Exception) as e:
276+
with pytest.raises(
277+
Exception, match='Syntax error in pattern "a pattern with a syntax error"'
278+
):
283279
lb.build_lut()
284-
assert str(e.value) == 'Syntax error in pattern "a pattern with a syntax error"'
285280

286281

287282
def test_load_invalid_mrl() -> None:
@@ -290,9 +285,8 @@ def test_load_invalid_mrl() -> None:
290285
mop = ImageMorph.MorphOp()
291286

292287
# Act / Assert
293-
with pytest.raises(Exception) as e:
288+
with pytest.raises(Exception, match="Wrong size operator file!"):
294289
mop.load_lut(invalid_mrl)
295-
assert str(e.value) == "Wrong size operator file!"
296290

297291

298292
def test_roundtrip_mrl(tmp_path: Path) -> None:

Tests/test_imagepath.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,9 @@ def test_path_constructors(
8181
def test_invalid_path_constructors(
8282
coords: tuple[str, str] | Sequence[Sequence[int]],
8383
) -> None:
84-
# Act
85-
with pytest.raises(ValueError) as e:
84+
with pytest.raises(ValueError, match="incorrect coordinate type"):
8685
ImagePath.Path(coords)
8786

88-
# Assert
89-
assert str(e.value) == "incorrect coordinate type"
90-
9187

9288
@pytest.mark.parametrize(
9389
"coords",
@@ -99,13 +95,9 @@ def test_invalid_path_constructors(
9995
),
10096
)
10197
def test_path_odd_number_of_coordinates(coords: Sequence[int]) -> None:
102-
# Act
103-
with pytest.raises(ValueError) as e:
98+
with pytest.raises(ValueError, match="wrong number of coordinates"):
10499
ImagePath.Path(coords)
105100

106-
# Assert
107-
assert str(e.value) == "wrong number of coordinates"
108-
109101

110102
@pytest.mark.parametrize(
111103
"coords, expected",

0 commit comments

Comments
 (0)