Skip to content

Commit

Permalink
overload __init__ to support multiple exception handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
tekumara committed Sep 4, 2024
1 parent bd81d9f commit 292259c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
35 changes: 35 additions & 0 deletions starlette/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
ASGIApp,
ExceptionHandler,
ExceptionType,
ExceptionType2,
ExceptionType3,
Lifespan,
Receive,
Scope,
Expand Down Expand Up @@ -62,6 +64,39 @@ class Starlette:
`on_startup` and `on_shutdown` handlers. Use one or the other, not both.
"""

@typing.overload
def __init__(
self: AppType,
debug: bool = False,
routes: typing.Sequence[BaseRoute] | None = None,
middleware: typing.Sequence[Middleware] | None = None,
exception_handlers: typing.Mapping[
typing.Any, ExceptionHandler[ExceptionType] | ExceptionHandler[ExceptionType2]
]
| None = None,
on_startup: typing.Sequence[typing.Callable[[], typing.Any]] | None = None,
on_shutdown: typing.Sequence[typing.Callable[[], typing.Any]] | None = None,
lifespan: Lifespan[AppType] | None = None,
) -> None:
...

@typing.overload
def __init__(
self: AppType,
debug: bool = False,
routes: typing.Sequence[BaseRoute] | None = None,
middleware: typing.Sequence[Middleware] | None = None,
exception_handlers: typing.Mapping[
typing.Any,
ExceptionHandler[ExceptionType] | ExceptionHandler[ExceptionType2] | ExceptionHandler[ExceptionType3],
]
| None = None,
on_startup: typing.Sequence[typing.Callable[[], typing.Any]] | None = None,
on_shutdown: typing.Sequence[typing.Callable[[], typing.Any]] | None = None,
lifespan: Lifespan[AppType] | None = None,
) -> None:
...

def __init__(
self: AppType,
debug: bool = False,
Expand Down
7 changes: 6 additions & 1 deletion starlette/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
Lifespan = typing.Union[StatelessLifespan[AppType], StatefulLifespan[AppType]]

ExceptionType = typing.TypeVar("ExceptionType", bound=Exception)
ExceptionType2 = typing.TypeVar("ExceptionType2", bound=Exception)
ExceptionType3 = typing.TypeVar("ExceptionType3", bound=Exception)

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

0 comments on commit 292259c

Please sign in to comment.