Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic ExceptionHandler type #2403

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d9037c5
generic exception
tekumara Jan 8, 2024
7cecce7
ruff format
tekumara Jan 8, 2024
f35c796
bind exc_class_or_status_code to E
tekumara Jan 8, 2024
2300839
Revert "ruff format"
tekumara Jan 8, 2024
6cf35d6
ruff format starlette/types.py
tekumara Jan 8, 2024
ba30a49
s/E/ExceptionType/
tekumara Jan 8, 2024
e94fc1c
Merge branch 'master' of https://github.com/tekumara/starlette into e…
tekumara Jan 8, 2024
77cca8b
contravariance is redundant because Callable is contravariant
tekumara Jan 9, 2024
1aebedb
Merge branch 'master' into exception-handler-typing
tekumara Jan 9, 2024
da3f8bb
Merge branch 'master' into exception-handler-typing
tekumara Jan 12, 2024
145a9b1
Merge branch 'master' into exception-handler-typing
tekumara Jan 17, 2024
c71d90c
Merge branch 'master' into exception-handler-typing
Kludex Feb 3, 2024
64328f4
Merge branch 'master' of https://github.com/tekumara/starlette into e…
tekumara Mar 11, 2024
6f89af7
Merge branch 'master' into exception-handler-typing
tekumara Mar 29, 2024
1890a85
Merge branch 'master' into exception-handler-typing
tekumara May 11, 2024
adfafdf
Merge branch 'master' into exception-handler-typing
tekumara Jul 20, 2024
ac72313
Merge branch 'master' into exception-handler-typing
tekumara Aug 6, 2024
25fafd2
Merge branch 'master' into exception-handler-typing
tekumara Aug 17, 2024
c0cbf4a
Merge branch 'master' of https://github.com/tekumara/starlette into e…
tekumara Sep 2, 2024
bd81d9f
ruff format
tekumara Sep 2, 2024
292259c
overload __init__ to support multiple exception handlers
tekumara Sep 2, 2024
ebcd61f
use Callable for exception_handlers
tekumara Sep 4, 2024
c1b05dd
fix mypy lints
tekumara Sep 4, 2024
9cfe2bd
fix ruff lints
tekumara Sep 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions starlette/_exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
)
from starlette.websockets import WebSocket

ExceptionHandlers = typing.Dict[typing.Any, ExceptionHandler]
StatusHandlers = typing.Dict[int, ExceptionHandler]
ExceptionHandlers = typing.Dict[typing.Any, ExceptionHandler[Exception]]
StatusHandlers = typing.Dict[int, ExceptionHandler[Exception]]


def _lookup_exception_handler(
exc_handlers: ExceptionHandlers, exc: Exception
) -> typing.Optional[ExceptionHandler]:
) -> typing.Optional[ExceptionHandler[Exception]]:
for cls in type(exc).__mro__:
if cls in exc_handlers:
return exc_handlers[cls]
Expand Down Expand Up @@ -69,15 +69,15 @@ async def sender(message: Message) -> None:

if scope["type"] == "http":
nonlocal conn
handler = typing.cast(HTTPExceptionHandler, handler)
handler = typing.cast(HTTPExceptionHandler[Exception], handler)
conn = typing.cast(Request, conn)
if is_async_callable(handler):
response = await handler(conn, exc)
else:
response = await run_in_threadpool(handler, conn, exc)
await response(scope, receive, sender)
elif scope["type"] == "websocket":
handler = typing.cast(WebSocketExceptionHandler, handler)
handler = typing.cast(WebSocketExceptionHandler[Exception], handler)
conn = typing.cast(WebSocket, conn)
if is_async_callable(handler):
await handler(conn, exc)
Expand Down
9 changes: 5 additions & 4 deletions starlette/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import BaseRoute, Router
from starlette.types import ASGIApp, ExceptionHandler, Lifespan, Receive, Scope, Send
from starlette.types import ASGIApp, E, ExceptionHandler, Lifespan, Receive, Scope, Send
from starlette.websockets import WebSocket

AppType = typing.TypeVar("AppType", bound="Starlette")
Expand Down Expand Up @@ -55,7 +55,8 @@ def __init__(
debug: bool = False,
routes: typing.Sequence[BaseRoute] | None = None,
middleware: typing.Sequence[Middleware] | None = None,
exception_handlers: typing.Mapping[typing.Any, ExceptionHandler] | None = None,
exception_handlers: typing.Mapping[typing.Any, ExceptionHandler[E]]
| None = None,
on_startup: typing.Sequence[typing.Callable[[], typing.Any]] | None = None,
on_shutdown: typing.Sequence[typing.Callable[[], typing.Any]] | None = None,
lifespan: typing.Optional[Lifespan["AppType"]] = None,
Expand Down Expand Up @@ -139,8 +140,8 @@ def add_middleware(

def add_exception_handler(
self,
exc_class_or_status_code: int | typing.Type[Exception],
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: this has now been tightened so that the Exception for exc_class_or_status_code must match the Exception in the handler Callable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kludex isn't this a deprecated function?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. We never deprecated add_exception_handler... 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, to be more precise, the Exception in the handler Callable is still contravariant, so this passes as one would hope:

def fallback_exception_handler(request: Request, exc: Exception) -> JSONResponse:
    ...

app.add_exception_handler(Exception, fallback_exception_handler)
app.add_exception_handler(MyException, fallback_exception_handler)

ie: the ExceptionType TypeVar changes the handler Callable Exception to a type parameter. The value of this type parameter is provided at the call site, eg: in the example here its Exception in the first call, and MyException in the second. But the contravariance of Callable remains.

And also the Callable is still not covariant, so this fails as one might expect:

def my_exception_handler(request: Request, exc: MyException) -> JSONResponse:
    ...

# this fails, because subtypes of the exc_class_or_status_code Exception in the Callable 
# aren't permitted ie: Callable is not covariant in the function's input parameter types
app.add_exception_handler(Exception, my_exception_handler)

handler: ExceptionHandler,
exc_class_or_status_code: int | typing.Type[E],
handler: ExceptionHandler[E],
) -> None: # pragma: no cover
self.exception_handlers[exc_class_or_status_code] = handler

Expand Down
10 changes: 5 additions & 5 deletions starlette/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
]
Lifespan = typing.Union[StatelessLifespan[AppType], StatefulLifespan[AppType]]

E = typing.TypeVar("E", bound=Exception, contravariant=True)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call it ExceptionType?


HTTPExceptionHandler = typing.Callable[
["Request", Exception], typing.Union["Response", typing.Awaitable["Response"]]
]
WebSocketExceptionHandler = typing.Callable[
["WebSocket", Exception], typing.Awaitable[None]
["Request", E], typing.Union["Response", typing.Awaitable["Response"]]
]
ExceptionHandler = typing.Union[HTTPExceptionHandler, WebSocketExceptionHandler]
WebSocketExceptionHandler = typing.Callable[["WebSocket", E], typing.Awaitable[None]]
ExceptionHandler = typing.Union[HTTPExceptionHandler[E], WebSocketExceptionHandler[E]]