Skip to content

Commit

Permalink
Mark abort as having type NoReturn
Browse files Browse the repository at this point in the history
In pallets#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.
  • Loading branch information
bjgill committed Jan 26, 2021
1 parent 8bbc58e commit f303dd6
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/werkzeug/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit f303dd6

Please sign in to comment.