X-Git-Url: https://git.madduck.net/etc/vim.git/blobdiff_plain/2c90480e1a102ab0fac57737d2ba5143d82abed7..738c2789cc07dbbd6bcf5908fd06627ee8d5cd6c:/src/blackd/middlewares.py?ds=sidebyside diff --git a/src/blackd/middlewares.py b/src/blackd/middlewares.py index e71f508..370e0ae 100644 --- a/src/blackd/middlewares.py +++ b/src/blackd/middlewares.py @@ -1,15 +1,25 @@ -from typing import Awaitable, Callable, Iterable +from typing import TYPE_CHECKING, Any, Awaitable, Callable, Iterable, TypeVar -from aiohttp.web_middlewares import middleware from aiohttp.web_request import Request from aiohttp.web_response import StreamResponse +if TYPE_CHECKING: + F = TypeVar("F", bound=Callable[..., Any]) + middleware: Callable[[F], F] +else: + try: + from aiohttp.web_middlewares import middleware + except ImportError: + # @middleware is deprecated and its behaviour is the default since aiohttp 4.0 + # so if it doesn't exist anymore, define a no-op for forward compatibility. + middleware = lambda x: x # noqa: E731 + Handler = Callable[[Request], Awaitable[StreamResponse]] Middleware = Callable[[Request, Handler], Awaitable[StreamResponse]] def cors(allow_headers: Iterable[str]) -> Middleware: - @middleware # type: ignore[misc] + @middleware async def impl(request: Request, handler: Handler) -> StreamResponse: is_options = request.method == "OPTIONS" is_preflight = is_options and "Access-Control-Request-Method" in request.headers @@ -32,4 +42,4 @@ def cors(allow_headers: Iterable[str]) -> Middleware: return resp - return impl # type: ignore[no-any-return] + return impl