Skip to content

Commit

Permalink
Fix warning displayed by default (#7677)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
(cherry picked from commit 32a1bae)
  • Loading branch information
Dreamsorcerer committed Oct 26, 2023
1 parent adb3b3a commit a4b8e72
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/7677.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed ``AppKey`` warning to ``web.NotAppKeyWarning`` and stop it being displayed by default. -- by :user:`Dreamsorcerer`
6 changes: 6 additions & 0 deletions aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import socket
import sys
import warnings
from argparse import ArgumentParser
from collections.abc import Iterable
from importlib import import_module
Expand Down Expand Up @@ -83,6 +84,7 @@
HTTPUseProxy as HTTPUseProxy,
HTTPVariantAlsoNegotiates as HTTPVariantAlsoNegotiates,
HTTPVersionNotSupported as HTTPVersionNotSupported,
NotAppKeyWarning as NotAppKeyWarning,
)
from .web_fileresponse import FileResponse as FileResponse
from .web_log import AccessLogger
Expand Down Expand Up @@ -159,6 +161,7 @@
"Application",
"CleanupError",
# web_exceptions
"NotAppKeyWarning",
"HTTPAccepted",
"HTTPBadGateway",
"HTTPBadRequest",
Expand Down Expand Up @@ -288,6 +291,9 @@
except ImportError: # pragma: no cover
SSLContext = Any # type: ignore[misc,assignment]

# Only display warning when using -Wdefault, -We, -X dev or similar.
warnings.filterwarnings("ignore", category=NotAppKeyWarning, append=True)

HostSequence = TypingIterable[str]


Expand Down
2 changes: 2 additions & 0 deletions aiohttp/web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from .log import web_logger
from .streams import StreamReader
from .typedefs import Middleware
from .web_exceptions import NotAppKeyWarning
from .web_log import AccessLogger
from .web_middlewares import _fix_request_current_app
from .web_protocol import RequestHandler
Expand Down Expand Up @@ -215,6 +216,7 @@ def __setitem__(self, key: Union[str, AppKey[_T]], value: Any) -> None:
"It is recommended to use web.AppKey instances for keys.\n"
+ "https://docs.aiohttp.org/en/stable/web_advanced.html"
+ "#application-s-config",
category=NotAppKeyWarning,
stacklevel=2,
)
self._state[key] = value
Expand Down
4 changes: 4 additions & 0 deletions aiohttp/web_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
)


class NotAppKeyWarning(UserWarning):
"""Warning when not using AppKey in Application."""


############################################################
# HTTP Exceptions
############################################################
Expand Down

0 comments on commit a4b8e72

Please sign in to comment.