From b91d763db8cfb28fb6aace9a10ea19995f9fc148 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Thu, 10 Aug 2023 12:46:03 +0100 Subject: [PATCH] Fix PT011 (``pytest.raises(ValueError)`` is too broad) --- pyproject.toml | 2 -- sphinx/util/__init__.py | 4 ++-- tests/test_build_html.py | 2 +- tests/test_directive_code.py | 24 ++++++++++++------------ tests/test_util.py | 8 ++++---- tests/test_util_images.py | 2 +- tests/test_util_inspect.py | 4 +++- tests/test_writer_latex.py | 2 +- 8 files changed, 24 insertions(+), 24 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4020cfb96..7675100a1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 diff --git a/sphinx/util/__init__.py b/sphinx/util/__init__.py index 9485ce1eb..18d8519cc 100644 --- a/sphinx/util/__init__.py +++ b/sphinx/util/__init__.py @@ -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 diff --git a/tests/test_build_html.py b/tests/test_build_html.py index cce5035ae..4a14a105a 100644 --- a/tests/test_build_html.py +++ b/tests/test_build_html.py @@ -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', diff --git a/tests/test_directive_code.py b/tests/test_directive_code.py index e53393fd8..3892540a8 100644 --- a/tests/test_directive_code.py +++ b/tests/test_directive_code.py @@ -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): diff --git a/tests/test_util.py b/tests/test_util.py index 40a551507..4389894ce 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -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) diff --git a/tests/test_util_images.py b/tests/test_util_images.py index e2c407d4c..15853c779 100644 --- a/tests/test_util_images.py +++ b/tests/test_util_images.py @@ -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) diff --git a/tests/test_util_inspect.py b/tests/test_util_inspect.py index 09be66573..73f96562f 100644 --- a/tests/test_util_inspect.py +++ b/tests/test_util_inspect.py @@ -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): diff --git a/tests/test_writer_latex.py b/tests/test_writer_latex.py index d51879949..a0ab3ee59 100644 --- a/tests/test_writer_latex.py +++ b/tests/test_writer_latex.py @@ -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'