Skip to content

Commit

Permalink
Enforce ruff/flake8-pytest-style rule PT011
Browse files Browse the repository at this point in the history
PT011 `pytest.raises(...)` is too broad, set the `match` parameter or use a more specific exception
  • Loading branch information
DimitriPapadopoulos committed Dec 31, 2024
1 parent 3469642 commit ca6c210
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
4 changes: 2 additions & 2 deletions pkg_resources/tests/test_pkg_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions pkg_resources/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
1 change: 0 additions & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions setuptools/tests/config/test_setupcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions setuptools/tests/test_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setuptools/tests/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit ca6c210

Please sign in to comment.