Fix B904 (raise exceptions with `raise ... from ...`)

This commit is contained in:
Adam Turner
2023-08-13 17:37:51 +01:00
committed by Adam Turner
parent db6d5aee2c
commit e010e24728
8 changed files with 13 additions and 15 deletions

View File

@@ -162,8 +162,6 @@ ignore = [
"ARG002", # unused method argument
"ARG003", # unused class method argument
"ARG005", # unused lambda argument
# flake8-bugbear opinionated (disabled by default in flake8)
"B904", # within an except clause, raise exceptions with `raise ... from ...`
# flake8-blind-except
"BLE001", # do not catch blind exception
# mccabe

View File

@@ -6646,7 +6646,7 @@ class DefinitionParser(BaseParser):
elif outer == 'function':
desc = "If the function has no return type"
else:
raise AssertionError
raise AssertionError from exUntyped
prevErrors.append((exUntyped, desc))
self.pos = startPos
try:
@@ -6659,7 +6659,7 @@ class DefinitionParser(BaseParser):
elif outer == 'function':
desc = "If the function has a return type"
else:
raise AssertionError
raise AssertionError from exUntyped
prevErrors.append((exTyped, desc))
# Retain the else branch for easier debugging.
# TODO: it would be nice to save the previous stacktrace
@@ -6671,7 +6671,7 @@ class DefinitionParser(BaseParser):
elif outer == 'function':
header = "Error when parsing function declaration."
else:
raise AssertionError
raise AssertionError from exUntyped
raise self._make_multi_error(prevErrors, header) from exTyped
else: # NoQA: RET506
# For testing purposes.
@@ -6733,7 +6733,7 @@ class DefinitionParser(BaseParser):
return ASTTemplateParamConstrainedTypeWithInit(type, typeInit)
except DefinitionError as eType:
if eExpr is None:
raise eType
raise
errs = []
errs.append((eExpr, "If default template argument is an expression"))
errs.append((eType, "If default template argument is a type"))
@@ -6877,7 +6877,7 @@ class DefinitionParser(BaseParser):
(eType, "If unconstrained type parameter or template type parameter"))
errs.append(
(eNonType, "If constrained type parameter or non-type parameter"))
raise self._make_multi_error(errs, header)
raise self._make_multi_error(errs, header) from None
def _parse_template_parameter_list(self) -> ASTTemplateParams:
# only: '<' parameter-list '>'

View File

@@ -286,7 +286,7 @@ class Autosummary(SphinxDirective):
else:
errors = exc.exceptions + [exc2]
raise ImportExceptionGroup(exc.args[0], errors)
raise ImportExceptionGroup(exc.args[0], errors) from None
def create_documenter(self, app: Sphinx, obj: Any,
parent: Any, full_name: str) -> Documenter:
@@ -695,7 +695,7 @@ def _import_by_name(name: str, grouped_exception: bool = True) -> tuple[Any, Any
except (ValueError, ImportError, AttributeError, KeyError) as exc:
errors.append(exc)
if grouped_exception:
raise ImportExceptionGroup('', errors)
raise ImportExceptionGroup('', errors) from None
else:
raise ImportError(*exc.args) from exc

View File

@@ -385,7 +385,7 @@ def test_run_epubcheck(app):
except CalledProcessError as exc:
print(exc.stdout.decode('utf-8'))
print(exc.stderr.decode('utf-8'))
raise AssertionError('epubcheck exited with return code %s' % exc.returncode)
raise AssertionError(f'epubcheck exited with return code {exc.returncode}') from exc
def test_xml_name_pattern_check():

View File

@@ -74,7 +74,7 @@ def test_msgfmt(app):
except CalledProcessError as exc:
print(exc.stdout)
print(exc.stderr)
raise AssertionError('msginit exited with return code %s' % exc.returncode)
raise AssertionError(f'msginit exited with return code {exc.returncode}') from exc
assert (app.outdir / 'en_US.po').is_file(), 'msginit failed'
try:
@@ -86,7 +86,7 @@ def test_msgfmt(app):
except CalledProcessError as exc:
print(exc.stdout)
print(exc.stderr)
raise AssertionError('msgfmt exited with return code %s' % exc.returncode)
raise AssertionError(f'msgfmt exited with return code {exc.returncode}') from exc
mo = app.outdir / 'en' / 'LC_MESSAGES' / 'test_root.mo'
assert mo.is_file(), 'msgfmt failed'

View File

@@ -73,7 +73,7 @@ def compile_latex_document(app, filename='python.tex', docclass='manual'):
print(exc.stdout.decode('utf8'))
print(exc.stderr.decode('utf8'))
raise AssertionError(f'{app.config.latex_engine} exited with '
f'return code {exc.returncode}')
f'return code {exc.returncode}') from exc
def skip_if_requested(testfunc):

View File

@@ -56,7 +56,7 @@ def test_texinfo(app, status, warning):
except CalledProcessError as exc:
print(exc.stdout)
print(exc.stderr)
raise AssertionError('makeinfo exited with return code %s' % exc.retcode)
raise AssertionError(f'makeinfo exited with return code {exc.retcode}') from exc
@pytest.mark.sphinx('texinfo', testroot='markup-rubric')

View File

@@ -231,7 +231,7 @@ if __name__ == '__main__':
action = sys.argv[1].lower()
except IndexError:
print(__doc__, file=sys.stderr)
raise SystemExit(2)
raise SystemExit(2) from None
os.chdir(ROOT)
if action == "extract":