[flake8-bugbear] Re-raise all exceptions with proper exception chaining

This commit is contained in:
Pierre Sassoulas 2024-02-04 11:13:47 +01:00
parent 7eef4619d5
commit fcb818b73c
4 changed files with 8 additions and 9 deletions

View File

@ -151,7 +151,6 @@ ignore = [
"B018", # Found useless expression. "B018", # Found useless expression.
"B023", # Function definition does not bind loop variable `warning` "B023", # Function definition does not bind loop variable `warning`
"B028", # No explicit `stacklevel` keyword argument found "B028", # No explicit `stacklevel` keyword argument found
"B904", # Within an `except` clause, raise exceptions with `raise ... from err`
# pycodestyle ignore # pycodestyle ignore
# pytest can do weird low-level things, and we usually know # pytest can do weird low-level things, and we usually know
# what we're doing when we use type(..) is ... # what we're doing when we use type(..) is ...

View File

@ -79,7 +79,7 @@ def prepare_release_pr(
) )
except InvalidFeatureRelease as e: except InvalidFeatureRelease as e:
print(f"{Fore.RED}{e}") print(f"{Fore.RED}{e}")
raise SystemExit(1) raise SystemExit(1) from None
print(f"Version: {Fore.CYAN}{version}") print(f"Version: {Fore.CYAN}{version}")

View File

@ -232,17 +232,17 @@ class TerminalWriter:
# which may lead to the previous color being propagated to the # which may lead to the previous color being propagated to the
# start of the expression, so reset first. # start of the expression, so reset first.
return "\x1b[0m" + highlighted return "\x1b[0m" + highlighted
except pygments.util.ClassNotFound: except pygments.util.ClassNotFound as e:
raise UsageError( raise UsageError(
"PYTEST_THEME environment variable had an invalid value: '{}'. " "PYTEST_THEME environment variable had an invalid value: '{}'. "
"Only valid pygment styles are allowed.".format( "Only valid pygment styles are allowed.".format(
os.getenv("PYTEST_THEME") os.getenv("PYTEST_THEME")
) )
) ) from e
except pygments.util.OptionError: except pygments.util.OptionError as e:
raise UsageError( raise UsageError(
"PYTEST_THEME_MODE environment variable had an invalid value: '{}'. " "PYTEST_THEME_MODE environment variable had an invalid value: '{}'. "
"The only allowed values are 'dark' and 'light'.".format( "The only allowed values are 'dark' and 'light'.".format(
os.getenv("PYTEST_THEME_MODE") os.getenv("PYTEST_THEME_MODE")
) )
) ) from e

View File

@ -1848,13 +1848,13 @@ def parse_warning_filter(
try: try:
action: "warnings._ActionKind" = warnings._getaction(action_) # type: ignore[attr-defined] action: "warnings._ActionKind" = warnings._getaction(action_) # type: ignore[attr-defined]
except warnings._OptionError as e: except warnings._OptionError as e:
raise UsageError(error_template.format(error=str(e))) raise UsageError(error_template.format(error=str(e))) from None
try: try:
category: Type[Warning] = _resolve_warning_category(category_) category: Type[Warning] = _resolve_warning_category(category_)
except Exception: except Exception:
exc_info = ExceptionInfo.from_current() exc_info = ExceptionInfo.from_current()
exception_text = exc_info.getrepr(style="native") exception_text = exc_info.getrepr(style="native")
raise UsageError(error_template.format(error=exception_text)) raise UsageError(error_template.format(error=exception_text)) from None
if message and escape: if message and escape:
message = re.escape(message) message = re.escape(message)
if module and escape: if module and escape:
@ -1867,7 +1867,7 @@ def parse_warning_filter(
except ValueError as e: except ValueError as e:
raise UsageError( raise UsageError(
error_template.format(error=f"invalid lineno {lineno_!r}: {e}") error_template.format(error=f"invalid lineno {lineno_!r}: {e}")
) ) from None
else: else:
lineno = 0 lineno = 0
return action, message, category, module, lineno return action, message, category, module, lineno