From f303dd6edd6e23bc879076bff615bfce96e447ca Mon Sep 17 00:00:00 2001 From: Benjamin Gill Date: Tue, 26 Jan 2021 16:50:26 +0000 Subject: [PATCH] Mark `abort` as having type `NoReturn` In https://github.com/pallets/werkzeug/pull/1995, typing was added to large parts of this project. This will be really useful to me. One of the problems I have with the latest release is `abort`. PyCharm doesn't know that `abort` aborts. So it will complain about possibly uninitialised variables if I write code like this: ``` if foo: bar = 1 else: abort(404) print(bar) ``` In master, `abort` now returns type `None`: `def abort(status: t.Union[int, "Response"], *args, **kwargs) -> None:` This type annotation means that the function returns `None`. This is not the case - I think `abort` always raises an exception. Instead, what we want to do is change the return type to [`t.NoReturn`](https://www.python.org/dev/peps/pep-0484/#the-noreturn-type). This expresses the correct meaning, which is that the `abort` function never returns. In turn, this will help PyCharm and other analysers understand code that uses `abort` better. --- src/werkzeug/exceptions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/werkzeug/exceptions.py b/src/werkzeug/exceptions.py index 6872e427a..a722cd55c 100644 --- a/src/werkzeug/exceptions.py +++ b/src/werkzeug/exceptions.py @@ -830,7 +830,7 @@ def __init__( if extra is not None: self.mapping.update(extra) - def __call__(self, code: t.Union[int, "Response"], *args, **kwargs) -> None: + def __call__(self, code: t.Union[int, "Response"], *args, **kwargs) -> t.NoReturn: from .wrappers.response import Response if isinstance(code, Response): @@ -842,7 +842,7 @@ def __call__(self, code: t.Union[int, "Response"], *args, **kwargs) -> None: raise self.mapping[code](*args, **kwargs) -def abort(status: t.Union[int, "Response"], *args, **kwargs) -> None: +def abort(status: t.Union[int, "Response"], *args, **kwargs) -> t.NoReturn: """Raises an :py:exc:`HTTPException` for the given status code or WSGI application. @@ -857,7 +857,7 @@ def abort(status: t.Union[int, "Response"], *args, **kwargs) -> None: _aborter(status, *args, **kwargs) -_aborter = Aborter() +_aborter: Aborter = Aborter() #: An exception that is used to signal both a :exc:`KeyError` and a #: :exc:`BadRequest`. Used by many of the datastructures.