From b8c0c4a4bbba7ac415e590d8bfd0300fbed0b1a4 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Thu, 26 Oct 2023 21:56:40 +0100 Subject: [PATCH] Fix warning displayed by default (#7677) (#7752) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> (cherry picked from commit 32a1bae0e7513a7708708c41ee4b93fc3c9952cf) --- CHANGES/7677.bugfix | 1 + aiohttp/web.py | 6 ++++++ aiohttp/web_app.py | 2 ++ aiohttp/web_exceptions.py | 4 ++++ 4 files changed, 13 insertions(+) create mode 100644 CHANGES/7677.bugfix 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 aa656b92e9a..3246674d4f5 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 as HTTPUseProxy, HTTPVariantAlsoNegotiates as HTTPVariantAlsoNegotiates, HTTPVersionNotSupported as HTTPVersionNotSupported, + NotAppKeyWarning as NotAppKeyWarning, ) from .web_fileresponse import FileResponse as FileResponse from .web_log import AccessLogger @@ -159,6 +161,7 @@ "Application", "CleanupError", # web_exceptions + "NotAppKeyWarning", "HTTPAccepted", "HTTPBadGateway", "HTTPBadRequest", @@ -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] diff --git a/aiohttp/web_app.py b/aiohttp/web_app.py index 917fd1346a6..ed5f8ee2636 100644 --- a/aiohttp/web_app.py +++ b/aiohttp/web_app.py @@ -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 @@ -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 diff --git a/aiohttp/web_exceptions.py b/aiohttp/web_exceptions.py index 86727429ffe..ee2c1e72d40 100644 --- a/aiohttp/web_exceptions.py +++ b/aiohttp/web_exceptions.py @@ -68,6 +68,10 @@ ) +class NotAppKeyWarning(UserWarning): + """Warning when not using AppKey in Application.""" + + ############################################################ # HTTP Exceptions ############################################################