Skip to content

Added depreciated warning in filter warning#3198

Closed
shivamdurgbuns wants to merge 3 commits into
psf:mainfrom
shivamdurgbuns:issue3176
Closed

Added depreciated warning in filter warning#3198
shivamdurgbuns wants to merge 3 commits into
psf:mainfrom
shivamdurgbuns:issue3176

Conversation

@shivamdurgbuns
Copy link
Copy Markdown

@shivamdurgbuns shivamdurgbuns commented Jul 30, 2022

Signed-off-by: Shivam Durgbuns shivamdurgbuns@gmail.com
fixes: #3176

Description

Checklist - did you ...

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

@github-actions
Copy link
Copy Markdown
Contributor

diff-shades reports zero changes comparing this PR (a514fa2) to main (eaa0489).


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

@ichard26 ichard26 added ci: skip news Pull requests that don't need a changelog entry. C: maintenance Related to project maintenance, e.g. CI, testing, policy changes, releases labels Aug 1, 2022
Copy link
Copy Markdown
Collaborator

@ichard26 ichard26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

Comment thread pyproject.toml Outdated
@graingert
Copy link
Copy Markdown
Contributor

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

Comment thread pyproject.toml Outdated
xfail_strict = true
filterwarnings = [
"error",
# this ignore can be removed when @middleware is removed from the codebase.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My intention was that you'd apply the mitigation rather than change the comment ;)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@graingert I am not getting it. You want me to raise Import Error exception on catching Depreciation Warning?

Copy link
Copy Markdown
Contributor

@graingert graingert Aug 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@graingert can you review now?

@JelleZijlstra
Copy link
Copy Markdown
Collaborator

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

Signed-off-by: Shivam Durgbuns <shivamdurgbuns@gmail.com>
@shivamdurgbuns
Copy link
Copy Markdown
Author

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?

Signed-off-by: Shivam Durgbuns <shivamdurgbuns@gmail.com>
@ichard26
Copy link
Copy Markdown
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C: maintenance Related to project maintenance, e.g. CI, testing, policy changes, releases ci: skip news Pull requests that don't need a changelog entry.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Upcoming aiohttp 4.0 is breaking our test suite with a new deprecation warning

4 participants