Skip to content

Commit

Permalink
cast exception_handler because of contravariance
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Haen committed Jan 16, 2024
1 parent 571d67e commit c6ab9d4
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions diracx-routers/src/diracx/routers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
from collections.abc import AsyncGenerator
from functools import partial
from typing import Any, Iterable, TypeVar
from typing import Any, Awaitable, Callable, Iterable, TypeVar, cast

import dotenv
from cachetools import TTLCache
Expand Down Expand Up @@ -164,9 +164,16 @@ def create_app_inner(
)

# Add exception handlers
app.add_exception_handler(DiracError, dirac_error_handler)
app.add_exception_handler(DiracHttpResponse, http_response_handler)
app.add_exception_handler(DBUnavailable, route_unavailable_error_hander)
# We need to cast because callables are contravariant and we define our exception handlers
# with a subclass of Exception (https://mypy.readthedocs.io/en/latest/generics.html#variance-of-generic-types)
handler_signature = Callable[[Request, Exception], Response | Awaitable[Response]]
app.add_exception_handler(DiracError, cast(handler_signature, dirac_error_handler))
app.add_exception_handler(
DiracHttpResponse, cast(handler_signature, http_response_handler)
)
app.add_exception_handler(
DBUnavailable, cast(handler_signature, route_unavailable_error_hander)
)

# TODO: remove the CORSMiddleware once we figure out how to launch
# diracx and diracx-web under the same origin
Expand Down

0 comments on commit c6ab9d4

Please sign in to comment.