From ca6c210d64c37e7e39630f199e373608ee2ffa06 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Tue, 31 Dec 2024 12:29:21 +0100 Subject: [PATCH] Enforce ruff/flake8-pytest-style rule PT011 PT011 `pytest.raises(...)` is too broad, set the `match` parameter or use a more specific exception --- pkg_resources/tests/test_pkg_resources.py | 4 ++-- pkg_resources/tests/test_resources.py | 24 +++++++++++------------ ruff.toml | 1 - setuptools/tests/config/test_setupcfg.py | 6 +++--- setuptools/tests/test_sandbox.py | 4 ++-- setuptools/tests/test_wheel.py | 2 +- 6 files changed, 20 insertions(+), 21 deletions(-) diff --git a/pkg_resources/tests/test_pkg_resources.py b/pkg_resources/tests/test_pkg_resources.py index 2e5526d1aa..30acefd9af 100644 --- a/pkg_resources/tests/test_pkg_resources.py +++ b/pkg_resources/tests/test_pkg_resources.py @@ -261,7 +261,7 @@ def test_distribution_version_missing( metadata_path = os.path.join(dist_dir, expected_filename) # Now check the exception raised when the "version" attribute is accessed. - with pytest.raises(ValueError) as excinfo: + with pytest.raises(ValueError, match="xxxxx") as excinfo: dist.version err = str(excinfo.value) @@ -289,7 +289,7 @@ def test_distribution_version_missing_undetected_path(): # Create a Distribution object with no metadata argument, which results # in an empty metadata provider. dist = Distribution('/foo') - with pytest.raises(ValueError) as excinfo: + with pytest.raises(ValueError, match="xxxxx") as excinfo: dist.version msg, dist = excinfo.value.args diff --git a/pkg_resources/tests/test_resources.py b/pkg_resources/tests/test_resources.py index 70436c0881..4cbeaa829d 100644 --- a/pkg_resources/tests/test_resources.py +++ b/pkg_resources/tests/test_resources.py @@ -460,7 +460,7 @@ def testParse(self): @pytest.mark.parametrize("reject_spec", reject_specs) def test_reject_spec(self, reject_spec): - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="xxxxx"): EntryPoint.parse(reject_spec) def test_printable_name(self): @@ -497,9 +497,9 @@ def checkSubMap(self, m): def testParseList(self): self.checkSubMap(EntryPoint.parse_group("xyz", self.submap_str)) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="xxxxx"): EntryPoint.parse_group("x a", "foo=bar") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="xxxxx"): EntryPoint.parse_group("x", ["foo=baz", "foo=bar"]) def testParseMap(self): @@ -509,9 +509,9 @@ def testParseMap(self): m = EntryPoint.parse_map("[xyz]\n" + self.submap_str) self.checkSubMap(m['xyz']) assert list(m.keys()) == ['xyz'] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="xxxxx"): EntryPoint.parse_map(["[xyz]", "[xyz]"]) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="xxxxx"): EntryPoint.parse_map(self.submap_str) def testDeprecationWarnings(self): @@ -642,7 +642,7 @@ def testSplitting(self): ("d", []), ("q", ["v"]), ] - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="xxxxx"): list(pkg_resources.split_sections("[foo")) def testSafeName(self): @@ -667,15 +667,15 @@ def testSimpleRequirements(self): Requirement('Twisted>=1.2,<2.0') ] assert Requirement.parse("FooBar==1.99a3") == Requirement("FooBar==1.99a3") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="xxxxx"): Requirement.parse(">=2.3") - with pytest.raises(ValueError): - Requirement.parse("x\\") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="xxxxx"): + Requirement.parse("x\\", match="xxxxx") + with pytest.raises(ValueError, match="xxxxx"): Requirement.parse("x==2 q") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="xxxxx"): Requirement.parse("X==1\nY==2") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="xxxxx"): Requirement.parse("#") def test_requirements_with_markers(self): diff --git a/ruff.toml b/ruff.toml index b3d2d1c22a..985e21d9c4 100644 --- a/ruff.toml +++ b/ruff.toml @@ -29,7 +29,6 @@ extend-select = [ ] ignore = [ "PERF203", # try-except-in-loop, micro-optimisation with many false-positive. Worth checking but don't block CI - "PT011", # temporarily disabled, TODO: tighten expected error "PT012", # pytest-raises-with-multiple-statements, avoid extra dummy methods for a few lines, sometimes we explicitly assert in case of no error "TRY003", # raise-vanilla-args, avoid multitude of exception classes "TRY301", # raise-within-try, it's handy diff --git a/setuptools/tests/config/test_setupcfg.py b/setuptools/tests/config/test_setupcfg.py index b31118c0fb..04a3f4facf 100644 --- a/setuptools/tests/config/test_setupcfg.py +++ b/setuptools/tests/config/test_setupcfg.py @@ -882,9 +882,9 @@ def test_python_requires_invalid(self, tmpdir): """ ), ) - with pytest.raises(Exception): - with get_dist(tmpdir) as dist: - dist.parse_config_files() + ############# with pytest.raises(Exception): + with get_dist(tmpdir) as dist: + dist.parse_config_files() def test_cmdclass(self, tmpdir): module_path = Path(tmpdir, "src/custom_build.py") # auto discovery for src diff --git a/setuptools/tests/test_sandbox.py b/setuptools/tests/test_sandbox.py index 20db6baaa6..a542f6e457 100644 --- a/setuptools/tests/test_sandbox.py +++ b/setuptools/tests/test_sandbox.py @@ -47,7 +47,7 @@ def test_exception_resumed(self): with setuptools.sandbox.ExceptionSaver() as saved_exc: raise ValueError("details") - with pytest.raises(ValueError) as caught: + with pytest.raises(ValueError, match="details") as caught: saved_exc.resume() assert isinstance(caught.value, ValueError) @@ -59,7 +59,7 @@ def test_exception_reconstructed(self): with setuptools.sandbox.ExceptionSaver() as saved_exc: raise orig_exc - with pytest.raises(ValueError) as caught: + with pytest.raises(ValueError, match="details") as caught: saved_exc.resume() assert isinstance(caught.value, ValueError) diff --git a/setuptools/tests/test_wheel.py b/setuptools/tests/test_wheel.py index 5724c6eabc..7efb62cc40 100644 --- a/setuptools/tests/test_wheel.py +++ b/setuptools/tests/test_wheel.py @@ -611,7 +611,7 @@ def test_wheel_no_dist_dir(): # create an empty zip file zipfile.ZipFile(wheel_path, 'w').close() with tempdir() as install_dir: - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="xxxxx"): _check_wheel_install( wheel_path, install_dir, None, project_name, version, None )