X-Git-Url: https://git.madduck.net/etc/vim.git/blobdiff_plain/a5381ba7648f7308145c78c248e29118e18dc530..c4bd2e31ceeac84d68592986fe70920f3d3d0443:/src/blackd/middlewares.py?ds=inline diff --git a/src/blackd/middlewares.py b/src/blackd/middlewares.py index 97994ec..370e0ae 100644 --- a/src/blackd/middlewares.py +++ b/src/blackd/middlewares.py @@ -1,7 +1,18 @@ -from typing import Iterable, Awaitable, Callable -from aiohttp.web_response import StreamResponse +from typing import TYPE_CHECKING, Any, Awaitable, Callable, Iterable, TypeVar + from aiohttp.web_request import Request -from aiohttp.web_middlewares import middleware +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]] @@ -31,4 +42,4 @@ def cors(allow_headers: Iterable[str]) -> Middleware: return resp - return impl # type: ignore + return impl