Fix PT011 (`pytest.raises(ValueError)` is too broad)

This commit is contained in:
Adam Turner 2023-08-10 12:46:03 +01:00
parent 78976662d9
commit b91d763db8
8 changed files with 24 additions and 24 deletions

View File

@ -221,8 +221,6 @@ ignore = [
"PLR5501", # consider using elif to remove an indentation level
"PLW0603", # using the global statement to update variables is discouraged
"PLW2901", # outer loop variable overwritten by inner assignment
# flake8-pytest-style
"PT011", # `pytest.raises(ValueError)` is too broad, set the `match` parameter
# flake8-use-pathlib
"PTH",
# flake8-quotes

View File

@ -214,8 +214,8 @@ def parselinenos(spec: str, total: int) -> list[int]:
items.extend(range(start - 1, end))
else:
raise ValueError
except Exception as exc:
raise ValueError('invalid line number spec: %r' % spec) from exc
except ValueError as exc:
raise ValueError(f'invalid line number spec: {spec!r}') from exc
return items

View File

@ -128,7 +128,7 @@ def test_html4_error(make_app, tmp_path):
(tmp_path / 'conf.py').write_text('', encoding='utf-8')
with pytest.raises(
ConfigError,
match=r'HTML 4 is no longer supported by Sphinx',
match='HTML 4 is no longer supported by Sphinx',
):
make_app(
buildername='html',

View File

@ -117,16 +117,16 @@ def test_LiteralIncludeReader_lines_and_lineno_match1(literal_inc_path):
def test_LiteralIncludeReader_lines_and_lineno_match2(literal_inc_path, app, status, warning):
options = {'lines': '0,3,5', 'lineno-match': True}
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
with pytest.raises(ValueError):
content, lines = reader.read()
with pytest.raises(ValueError, match='Cannot use "lineno-match" with a disjoint set of "lines"'):
reader.read()
@pytest.mark.sphinx() # init locale for errors
def test_LiteralIncludeReader_lines_and_lineno_match3(literal_inc_path, app, status, warning):
options = {'lines': '100-', 'lineno-match': True}
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
with pytest.raises(ValueError):
content, lines = reader.read()
with pytest.raises(ValueError, match="Line spec '100-': no lines pulled from include file"):
reader.read()
@pytest.mark.xfail(os.name != 'posix', reason="Not working on windows")
@ -179,23 +179,23 @@ def test_LiteralIncludeReader_start_at_and_lines(literal_inc_path):
def test_LiteralIncludeReader_missing_start_and_end(literal_inc_path):
options = {'start-at': 'NOTHING'}
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
with pytest.raises(ValueError):
content, lines = reader.read()
with pytest.raises(ValueError, match='start-at pattern not found: NOTHING'):
reader.read()
options = {'end-at': 'NOTHING'}
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
with pytest.raises(ValueError):
content, lines = reader.read()
with pytest.raises(ValueError, match='end-at pattern not found: NOTHING'):
reader.read()
options = {'start-after': 'NOTHING'}
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
with pytest.raises(ValueError):
content, lines = reader.read()
with pytest.raises(ValueError, match='start-after pattern not found: NOTHING'):
reader.read()
options = {'end-before': 'NOTHING'}
reader = LiteralIncludeReader(literal_inc_path, options, DUMMY_CONFIG)
with pytest.raises(ValueError):
content, lines = reader.read()
with pytest.raises(ValueError, match='end-before pattern not found: NOTHING'):
reader.read()
def test_LiteralIncludeReader_end_before(literal_inc_path):

View File

@ -63,11 +63,11 @@ def test_parselinenos():
assert parselinenos('1,7-', 10) == [0, 6, 7, 8, 9]
assert parselinenos('7-7', 10) == [6]
assert parselinenos('11-', 10) == [10]
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="invalid line number spec: '1-2-3'"):
parselinenos('1-2-3', 10)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="invalid line number spec: 'abc-def'"):
parselinenos('abc-def', 10)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="invalid line number spec: '-'"):
parselinenos('-', 10)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="invalid line number spec: '3-1'"):
parselinenos('3-1', 10)

View File

@ -70,5 +70,5 @@ def test_parse_data_uri():
# invalid data URI (no properties)
uri = ("data:iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4"
"//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==")
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=r'not enough values to unpack \(expected 2, got 1\)'):
parse_data_uri(uri)

View File

@ -127,8 +127,10 @@ def test_signature():
sig = inspect.stringify_signature(inspect.signature(list))
assert sig == '(iterable=(), /)'
else:
with pytest.raises(ValueError):
with pytest.raises(ValueError, match='no signature found for builtin type'):
inspect.signature(list)
with pytest.raises(ValueError, match='no signature found for builtin type'):
inspect.signature(range)
# normal function
def func(a, b, c=1, d=2, *e, **f):

View File

@ -23,6 +23,6 @@ def test_rstdim_to_latexdim():
assert rstdim_to_latexdim('.5em') == '.5em'
# unknown values (it might be generated by 3rd party extension)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match='could not convert string to float: '):
rstdim_to_latexdim('unknown')
assert rstdim_to_latexdim('160.0unknown') == '160.0unknown'