]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Mitigate deprecation of aiohttp's `@middleware` decorator (#3259)
authorRichard Si <63936253+ichard26@users.noreply.github.com>
Mon, 5 Sep 2022 20:27:05 +0000 (16:27 -0400)
committerGitHub <noreply@github.com>
Mon, 5 Sep 2022 20:27:05 +0000 (16:27 -0400)
This is deprecated since aiohttp 4.0. If it doesn't exist just define a
no-op decorator that does nothing (after the other aiohttp imports
though!). By doing this, it's safe to ignore the DeprecationWarning
without needing to require the latest aiohttp once they remove
`@middleware`.

pyproject.toml
src/blackd/middlewares.py

index 813e86b2e930a658dae49b9b406527d7fb4279f8..849891f8798024f9a6fd636af19b8894b30eb947 100644 (file)
@@ -105,6 +105,9 @@ filterwarnings = [
     # this is mitigated by a try/catch in https://github.com/psf/black/pull/2974/
     # this ignore can be removed when support for aiohttp 3.7 is dropped.
     '''ignore:Decorator `@unittest_run_loop` is no longer needed in aiohttp 3\.8\+:DeprecationWarning''',
+    # this is mitigated by a try/catch in https://github.com/psf/black/pull/3198/
+    # this ignore can be removed when support for aiohttp 3.x is dropped.
+    '''ignore:Middleware decorator is deprecated since 4\.0 and its behaviour is default, you can simply remove this decorator:DeprecationWarning''',
     # this is mitigated by https://github.com/python/cpython/issues/79071 in python 3.8+
     # this ignore can be removed when support for 3.7 is dropped.
     '''ignore:Bare functions are deprecated, use async ones:DeprecationWarning''',
index e71f5082686aff52dfe545f3a7f323b32a5b3416..370e0ae222eebfef1b73c185b4fc6130e87974cc 100644 (file)
@@ -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