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 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
25 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
aa2a879
Merge branch 'master' into exception-handler-typing
tekumara Oct 28, 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
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
2 changes: 1 addition & 1 deletion starlette/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def requires(
scopes_list = [scopes] if isinstance(scopes, str) else list(scopes)

def decorator(
func: typing.Callable[_P, typing.Any]
func: typing.Callable[_P, typing.Any],
) -> typing.Callable[_P, typing.Any]:
sig = inspect.signature(func)
for idx, parameter in enumerate(sig.parameters.values()):
Expand Down
6 changes: 4 additions & 2 deletions starlette/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def iscoroutinefunction_or_partial(obj: typing.Any) -> bool: # pragma: no cover


def request_response(
func: typing.Callable[[Request], typing.Union[typing.Awaitable[Response], Response]]
func: typing.Callable[
[Request], typing.Union[typing.Awaitable[Response], Response]
],
) -> ASGIApp:
"""
Takes a function or coroutine `func(request) -> response`,
Expand All @@ -78,7 +80,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:


def websocket_session(
func: typing.Callable[[WebSocket], typing.Awaitable[None]]
func: typing.Callable[[WebSocket], typing.Awaitable[None]],
) -> ASGIApp:
"""
Takes a coroutine `func(session)`, and returns an ASGI application.
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
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]]
18 changes: 9 additions & 9 deletions tests/middleware/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ async def downstream_app(scope, receive, send):


def test_read_request_stream_in_app_after_middleware_calls_stream(
test_client_factory: Callable[[ASGIApp], TestClient]
test_client_factory: Callable[[ASGIApp], TestClient],
) -> None:
async def homepage(request: Request):
expected = [b""]
Expand Down Expand Up @@ -527,7 +527,7 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint):


def test_read_request_stream_in_app_after_middleware_calls_body(
test_client_factory: Callable[[ASGIApp], TestClient]
test_client_factory: Callable[[ASGIApp], TestClient],
) -> None:
async def homepage(request: Request):
expected = [b"a", b""]
Expand All @@ -552,7 +552,7 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint):


def test_read_request_body_in_app_after_middleware_calls_stream(
test_client_factory: Callable[[ASGIApp], TestClient]
test_client_factory: Callable[[ASGIApp], TestClient],
) -> None:
async def homepage(request: Request):
assert await request.body() == b""
Expand All @@ -577,7 +577,7 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint):


def test_read_request_body_in_app_after_middleware_calls_body(
test_client_factory: Callable[[ASGIApp], TestClient]
test_client_factory: Callable[[ASGIApp], TestClient],
) -> None:
async def homepage(request: Request):
assert await request.body() == b"a"
Expand All @@ -599,7 +599,7 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint):


def test_read_request_stream_in_dispatch_after_app_calls_stream(
test_client_factory: Callable[[ASGIApp], TestClient]
test_client_factory: Callable[[ASGIApp], TestClient],
) -> None:
async def homepage(request: Request):
expected = [b"a", b""]
Expand Down Expand Up @@ -627,7 +627,7 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint):


def test_read_request_stream_in_dispatch_after_app_calls_body(
test_client_factory: Callable[[ASGIApp], TestClient]
test_client_factory: Callable[[ASGIApp], TestClient],
) -> None:
async def homepage(request: Request):
assert await request.body() == b"a"
Expand Down Expand Up @@ -705,7 +705,7 @@ async def send(msg: Message) -> None:


def test_read_request_stream_in_dispatch_after_app_calls_body_with_middleware_calling_body_before_call_next( # noqa: E501
test_client_factory: Callable[[ASGIApp], TestClient]
test_client_factory: Callable[[ASGIApp], TestClient],
) -> None:
async def homepage(request: Request):
assert await request.body() == b"a"
Expand Down Expand Up @@ -733,7 +733,7 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint):


def test_read_request_body_in_dispatch_after_app_calls_body_with_middleware_calling_body_before_call_next( # noqa: E501
test_client_factory: Callable[[ASGIApp], TestClient]
test_client_factory: Callable[[ASGIApp], TestClient],
) -> None:
async def homepage(request: Request):
assert await request.body() == b"a"
Expand Down Expand Up @@ -837,7 +837,7 @@ async def send(msg: Message):


def test_downstream_middleware_modifies_receive(
test_client_factory: Callable[[ASGIApp], TestClient]
test_client_factory: Callable[[ASGIApp], TestClient],
) -> None:
"""If a downstream middleware modifies receive() the final ASGI app
should see the modified version.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_background.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async def app(scope, receive, send):


def test_multi_tasks_failure_avoids_next_execution(
test_client_factory: Callable[..., TestClient]
test_client_factory: Callable[..., TestClient],
) -> None:
TASK_COUNTER = 0

Expand Down
2 changes: 1 addition & 1 deletion tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ async def modified_send(msg: Message) -> None:


def test_websocket_route_middleware(
test_client_factory: typing.Callable[..., TestClient]
test_client_factory: typing.Callable[..., TestClient],
):
async def websocket_endpoint(session: WebSocket):
await session.accept()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:


def test_websocket_ensure_unicode_on_send_json(
test_client_factory: Callable[..., TestClient]
test_client_factory: Callable[..., TestClient],
):
async def app(scope: Scope, receive: Receive, send: Send) -> None:
websocket = WebSocket(scope, receive=receive, send=send)
Expand Down