Skip to content

Commit 0b0f98c

Browse files
Enforce ruff/flake8-pytest-style rule PT011
PT011 `pytest.raises(...)` is too broad, set the `match` parameter or use a more specific exception
1 parent 43b50e2 commit 0b0f98c

File tree

6 files changed

+20
-21
lines changed

6 files changed

+20
-21
lines changed

Diff for: pkg_resources/tests/test_pkg_resources.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def test_distribution_version_missing(
261261
metadata_path = os.path.join(dist_dir, expected_filename)
262262

263263
# Now check the exception raised when the "version" attribute is accessed.
264-
with pytest.raises(ValueError) as excinfo:
264+
with pytest.raises(ValueError, match="xxxxx") as excinfo:
265265
dist.version
266266

267267
err = str(excinfo.value)
@@ -289,7 +289,7 @@ def test_distribution_version_missing_undetected_path():
289289
# Create a Distribution object with no metadata argument, which results
290290
# in an empty metadata provider.
291291
dist = Distribution('/foo')
292-
with pytest.raises(ValueError) as excinfo:
292+
with pytest.raises(ValueError, match="xxxxx") as excinfo:
293293
dist.version
294294

295295
msg, dist = excinfo.value.args

Diff for: pkg_resources/tests/test_resources.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ def testParse(self):
460460

461461
@pytest.mark.parametrize("reject_spec", reject_specs)
462462
def test_reject_spec(self, reject_spec):
463-
with pytest.raises(ValueError):
463+
with pytest.raises(ValueError, match=reject_spec):
464464
EntryPoint.parse(reject_spec)
465465

466466
def test_printable_name(self):
@@ -497,9 +497,9 @@ def checkSubMap(self, m):
497497

498498
def testParseList(self):
499499
self.checkSubMap(EntryPoint.parse_group("xyz", self.submap_str))
500-
with pytest.raises(ValueError):
500+
with pytest.raises(ValueError, match="xxxxx"):
501501
EntryPoint.parse_group("x a", "foo=bar")
502-
with pytest.raises(ValueError):
502+
with pytest.raises(ValueError, match="xxxxx"):
503503
EntryPoint.parse_group("x", ["foo=baz", "foo=bar"])
504504

505505
def testParseMap(self):
@@ -509,9 +509,9 @@ def testParseMap(self):
509509
m = EntryPoint.parse_map("[xyz]\n" + self.submap_str)
510510
self.checkSubMap(m['xyz'])
511511
assert list(m.keys()) == ['xyz']
512-
with pytest.raises(ValueError):
512+
with pytest.raises(ValueError, match="xxxxx"):
513513
EntryPoint.parse_map(["[xyz]", "[xyz]"])
514-
with pytest.raises(ValueError):
514+
with pytest.raises(ValueError, match="xxxxx"):
515515
EntryPoint.parse_map(self.submap_str)
516516

517517
def testDeprecationWarnings(self):
@@ -642,7 +642,7 @@ def testSplitting(self):
642642
("d", []),
643643
("q", ["v"]),
644644
]
645-
with pytest.raises(ValueError):
645+
with pytest.raises(ValueError, match="xxxxx"):
646646
list(pkg_resources.split_sections("[foo"))
647647

648648
def testSafeName(self):
@@ -667,15 +667,15 @@ def testSimpleRequirements(self):
667667
Requirement('Twisted>=1.2,<2.0')
668668
]
669669
assert Requirement.parse("FooBar==1.99a3") == Requirement("FooBar==1.99a3")
670-
with pytest.raises(ValueError):
670+
with pytest.raises(ValueError, match="xxxxx"):
671671
Requirement.parse(">=2.3")
672-
with pytest.raises(ValueError):
673-
Requirement.parse("x\\")
674-
with pytest.raises(ValueError):
672+
with pytest.raises(ValueError, match="xxxxx"):
673+
Requirement.parse("x\\", match="xxxxx")
674+
with pytest.raises(ValueError, match="xxxxx"):
675675
Requirement.parse("x==2 q")
676-
with pytest.raises(ValueError):
676+
with pytest.raises(ValueError, match="xxxxx"):
677677
Requirement.parse("X==1\nY==2")
678-
with pytest.raises(ValueError):
678+
with pytest.raises(ValueError, match="xxxxx"):
679679
Requirement.parse("#")
680680

681681
def test_requirements_with_markers(self):

Diff for: ruff.toml

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ extend-select = [
2929
]
3030
ignore = [
3131
"PERF203", # try-except-in-loop, micro-optimisation with many false-positive. Worth checking but don't block CI
32-
"PT011", # temporarily disabled, TODO: tighten expected error
3332
"PT012", # pytest-raises-with-multiple-statements, avoid extra dummy methods for a few lines, sometimes we explicitly assert in case of no error
3433
"TRY003", # raise-vanilla-args, avoid multitude of exception classes
3534
"TRY301", # raise-within-try, it's handy

Diff for: setuptools/tests/config/test_setupcfg.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -882,9 +882,9 @@ def test_python_requires_invalid(self, tmpdir):
882882
"""
883883
),
884884
)
885-
with pytest.raises(Exception):
886-
with get_dist(tmpdir) as dist:
887-
dist.parse_config_files()
885+
############# with pytest.raises(Exception):
886+
with get_dist(tmpdir) as dist:
887+
dist.parse_config_files()
888888

889889
def test_cmdclass(self, tmpdir):
890890
module_path = Path(tmpdir, "src/custom_build.py") # auto discovery for src

Diff for: setuptools/tests/test_sandbox.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_exception_resumed(self):
4747
with setuptools.sandbox.ExceptionSaver() as saved_exc:
4848
raise ValueError("details")
4949

50-
with pytest.raises(ValueError) as caught:
50+
with pytest.raises(ValueError, match="details") as caught:
5151
saved_exc.resume()
5252

5353
assert isinstance(caught.value, ValueError)
@@ -59,7 +59,7 @@ def test_exception_reconstructed(self):
5959
with setuptools.sandbox.ExceptionSaver() as saved_exc:
6060
raise orig_exc
6161

62-
with pytest.raises(ValueError) as caught:
62+
with pytest.raises(ValueError, match="details") as caught:
6363
saved_exc.resume()
6464

6565
assert isinstance(caught.value, ValueError)

Diff for: setuptools/tests/test_wheel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ def test_wheel_no_dist_dir():
609609
# create an empty zip file
610610
zipfile.ZipFile(wheel_path, 'w').close()
611611
with tempdir() as install_dir:
612-
with pytest.raises(ValueError):
612+
with pytest.raises(ValueError, match="xxxxx"):
613613
_check_wheel_install(
614614
wheel_path, install_dir, None, project_name, version, None
615615
)

0 commit comments

Comments
 (0)