use exception chaining
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixes flake8-bugbear B904
authored by David Lord · committed by David Lord · parent ca0033c · Browse files · 5 files·+21 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+21 added−20 removed
- other:+21 added−20 removed
Provenance
- Commit
6a4bf9e- Tree
76f3b5f- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
ca0033c
5 files changed+21 added−20 removed
modified:src/flask/app.py+2 added-2 removed
1619 unmodified lines1620 from asgiref.sync import async_to_sync as asgiref_async_to_sync1621 except ImportError:1622 raise RuntimeError(1623 "Install Flask with the 'async' extra in order to use async views."1624- )1624+ ) from None1625 1626 # Check that Werkzeug isn't using its fallback ContextVar class.1627 if ContextVar.__module__ == "werkzeug.local":1628 raise RuntimeError(97 unmodified lines1726 f"{e}\nThe view function did not return a valid"1727 " response. The return type must be a string,"1728 " dict, tuple, Response instance, or WSGI"1729 f" callable, but it was a {type(rv).__name__}."1730- ).with_traceback(sys.exc_info()[2])1730+ ).with_traceback(sys.exc_info()[2]) from None1731 else:1732 raise TypeError(1733 "The view function did not return a valid"1734 " response. The return type must be a string,"359 unmodified linesmodified:src/flask/cli.py+14 added-14 removed
67 unmodified lines68 app = call_factory(script_info, app_factory)69 70 if isinstance(app, Flask):71 return app72- except TypeError:72+ except TypeError as e:73 if not _called_with_wrong_args(app_factory):74 raise75+76 raise NoAppException(77 f"Detected factory {attr_name!r} in module {module.__name__!r},"78 " but could not call it without arguments. Use"79 f" \"FLASK_APP='{module.__name__}:{attr_name}(args)'\""80 " to specify arguments."80- )81+ ) from e82 83 raise NoAppException(84 "Failed to find Flask application or factory in module"85 f" {module.__name__!r}. Use 'FLASK_APP={module.__name__}:name'"75 unmodified lines161 expr = ast.parse(app_name.strip(), mode="eval").body162 except SyntaxError:163 raise NoAppException(164 f"Failed to parse {app_name!r} as an attribute name or function call."164- )165+ ) from None166 167 if isinstance(expr, ast.Name):168 name = expr.id169 args = kwargs = None14 unmodified lines184 # literal_eval gives cryptic error messages, show a generic185 # message with the full expression instead.186 raise NoAppException(187 f"Failed to parse arguments as literal values: {app_name!r}."187- )188+ ) from None189 else:190 raise NoAppException(191 f"Failed to parse {app_name!r} as an attribute name or function call."192 )193 194 try:195 attr = getattr(module, name)195- except AttributeError:196+ except AttributeError as e:197 raise NoAppException(198 f"Failed to find attribute {name!r} in {module.__name__!r}."198- )199+ ) from e200 201 # If the attribute is a function, call it with any args and kwargs202 # to get the real application.203 if inspect.isfunction(attr):204 try:205 app = call_factory(script_info, attr, args, kwargs)205- except TypeError:206+ except TypeError as e:207 if not _called_with_wrong_args(attr):208 raise209 210 raise NoAppException(211 f"The factory {app_name!r} in module"212 f" {module.__name__!r} could not be called with the"213 " specified arguments."213- )214+ ) from e215 else:216 app = attr217 218 if isinstance(app, Flask):38 unmodified lines257 __traceback_hide__ = True # noqa: F841258 259 try:260 __import__(module_name)260- except ImportError:261+ except ImportError as e:262 # Reraise the ImportError if it occurred within the imported module.263 # Determine this by checking whether the trace has a depth > 1.264 if sys.exc_info()[2].tb_next:265 raise NoAppException(265- f"While importing {module_name!r}, an ImportError was"266- f" raised:\n\n{traceback.format_exc()}"267- )266+ f"While importing {module_name!r}, an ImportError was raised."267+ ) from e268 elif raise_if_not_found:269- raise NoAppException(f"Could not import {module_name!r}.")269+ raise NoAppException(f"Could not import {module_name!r}.") from e270 else:271 return272 273 module = sys.modules[module_name]450 unmodified lines724 raise click.BadParameter(725 "Using ad-hoc certificates requires the cryptography library.",726 ctx,727 param,728- )728+ ) from None729 730 return value731 732 obj = import_string(value, silent=True)266 unmodified linesmodified:src/flask/debughelpers.py+3 added-2 removed
81 unmodified lines82 class newcls(oldcls):83 def __getitem__(self, key):84 try:85 return oldcls.__getitem__(self, key)86- except KeyError:86+ except KeyError as e:87 if key not in request.form:88 raise89- raise DebugFilesKeyError(request, key)89 90+ raise DebugFilesKeyError(request, key) from e91+92 newcls.__name__ = oldcls.__name__93 newcls.__module__ = oldcls.__module__94 request.files.__class__ = newcls95 77 unmodified linesmodified:src/flask/scaffold.py+1 added-1 removed
713 unmodified lines714 raise KeyError(715 f"'{code_or_exception}' is not a recognized HTTP error"716 " code. Use a subclass of HTTPException with that code"717 " instead."718- )718+ ) from None719 720 self.error_handler_spec[None][code][exc_class] = t.cast(721 "ErrorHandlerCallable[Exception]", f722 )153 unmodified linesmodified:src/flask/signals.py+1 added-1 removed
27 unmodified lines28 def _fail(self, *args: t.Any, **kwargs: t.Any) -> t.Any:29 raise RuntimeError(30 "Signalling support is unavailable because the blinker"31 " library is not installed."32- )32+ ) from None33 34 connect = connect_via = connected_to = temporarily_connected_to = _fail35 disconnect = _fail36 has_receivers_for = receivers_for = _fail20 unmodified lines