diff --git a/CHANGES/7677.bugfix b/CHANGES/7677.bugfix new file mode 100644 index 00000000000..ca20004121a --- /dev/null +++ b/CHANGES/7677.bugfix @@ -0,0 +1 @@ +Changed ``AppKey`` warning to ``web.NotAppKeyWarning`` and stop it being displayed by default. -- by :user:`Dreamsorcerer` diff --git a/aiohttp/web.py b/aiohttp/web.py index f87d57988be..242f7a96ced 100644 --- a/aiohttp/web.py +++ b/aiohttp/web.py @@ -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 @@ -83,6 +84,7 @@ HTTPUseProxy, HTTPVariantAlsoNegotiates, HTTPVersionNotSupported, + NotAppKeyWarning, ) from .web_fileresponse import FileResponse from .web_log import AccessLogger @@ -139,6 +141,7 @@ "Application", "CleanupError", # web_exceptions + "NotAppKeyWarning", "HTTPAccepted", "HTTPBadGateway", "HTTPBadRequest", @@ -268,6 +271,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] diff --git a/aiohttp/web_app.py b/aiohttp/web_app.py index 5be96084693..614c81fb9c4 100644 --- a/aiohttp/web_app.py +++ b/aiohttp/web_app.py @@ -31,6 +31,7 @@ from .helpers import AppKey from .log import web_logger from .typedefs import Middleware +from .web_exceptions import NotAppKeyWarning from .web_middlewares import _fix_request_current_app from .web_request import Request from .web_response import StreamResponse @@ -173,6 +174,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 diff --git a/aiohttp/web_exceptions.py b/aiohttp/web_exceptions.py index 9ae61b43c9f..a4ba2e99bf2 100644 --- a/aiohttp/web_exceptions.py +++ b/aiohttp/web_exceptions.py @@ -71,6 +71,10 @@ ) +class NotAppKeyWarning(UserWarning): + """Warning when not using AppKey in Application.""" + + ############################################################ # HTTP Exceptions ############################################################