black icon indicating copy to clipboard operation
black copied to clipboard

Added depreciated warning in filter warning

Open shivamdurgbuns opened this issue 3 years ago • 1 comments

Signed-off-by: Shivam Durgbuns [email protected] fixes: https://github.com/psf/black/issues/3176

Description

Checklist - did you ...

  • [ ] Add a CHANGELOG entry if necessary?
  • [ ] Add / update tests if necessary?
  • [ ] Add new / update outdated documentation?

shivamdurgbuns avatar Jul 30 '22 09:07 shivamdurgbuns

diff-shades reports zero changes comparing this PR (a514fa21e16f842c9d78ac420e483bcb06127015) to main (eaa048925e4443cc0e2b57b795f2852bedb4287f).


What is this? | Workflow run | diff-shades documentation

github-actions[bot] avatar Jul 31 '22 00:07 github-actions[bot]

There is no mitigation here, it's simply ignoring the issue. We might want to also try except this decorator though similar to #2974 so when aiohttp does remove this decorator, nothing breaks :)

Thoughts @graingert ?

Yeah you need to mitigate this with a try/catch ImportError

graingert avatar Aug 02 '22 00:08 graingert

Sounds like we should not do this and instead actually fix the warning.

JelleZijlstra avatar Aug 13 '22 03:08 JelleZijlstra

Sounds like we should not do this and instead actually fix the warning.

Has the fix been made? Or should I add an exception for this?

shivamdurgbuns avatar Aug 19 '22 17:08 shivamdurgbuns

Sorry @shivamdurgbuns, but looks like we haven't been clear enough on what needs to be done. We want to mitigate the deprecation of @middleware in aiohttp 4.x by adding a try/except in src/blackd/middlewares.py that simply does middleware = lambda x: x if aiohttp.web_middlewares.middleware doesn't exist. After that then it's safe to ignore the deprecation warning in pyproject.toml.

diff --git a/pyproject.toml b/pyproject.toml
index 813e86b..849891f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -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''',
diff --git a/src/blackd/middlewares.py b/src/blackd/middlewares.py
index 7abde52..2be42b0 100644
--- a/src/blackd/middlewares.py
+++ b/src/blackd/middlewares.py
@@ -1,9 +1,19 @@
-from typing import Awaitable, Callable, Iterable
+from typing import TYPE_CHECKING, Awaitable, Callable, Iterable, TypeVar
 
-from aiohttp.web_middlewares import middleware
 from aiohttp.web_request import Request
 from aiohttp.web_response import StreamResponse
 
+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, just define a no-op decorator for compatibility.
+    middleware = lambda x: x
+
+if TYPE_CHECKING:
+    F = TypeVar("F", bound=Callable[..., Any])
+    middleware: Callable[[F], F] = lambda x: x
+
 Handler = Callable[[Request], Awaitable[StreamResponse]]

Here are roughly the changes we'd like, although this is mostly untested.

ichard26 avatar Aug 27 '22 20:08 ichard26