Make warnings for DeprecationWarning consistent (#2332)
This commit is contained in:
parent
426742b3e2
commit
b2a1bc69f5
|
@ -42,6 +42,7 @@ from typing import (
|
||||||
Union,
|
Union,
|
||||||
)
|
)
|
||||||
from urllib.parse import urlencode, urlunparse
|
from urllib.parse import urlencode, urlunparse
|
||||||
|
from warnings import filterwarnings
|
||||||
|
|
||||||
from sanic_routing.exceptions import ( # type: ignore
|
from sanic_routing.exceptions import ( # type: ignore
|
||||||
FinalizationError,
|
FinalizationError,
|
||||||
|
@ -95,6 +96,8 @@ from sanic.touchup import TouchUp, TouchUpMeta
|
||||||
if OS_IS_WINDOWS:
|
if OS_IS_WINDOWS:
|
||||||
enable_windows_color_support()
|
enable_windows_color_support()
|
||||||
|
|
||||||
|
filterwarnings("once", category=DeprecationWarning)
|
||||||
|
|
||||||
|
|
||||||
class Sanic(BaseSanic, metaclass=TouchUpMeta):
|
class Sanic(BaseSanic, metaclass=TouchUpMeta):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from inspect import signature
|
from inspect import signature
|
||||||
from typing import Dict, List, Optional, Tuple, Type
|
from typing import Dict, List, Optional, Tuple, Type
|
||||||
|
from warnings import warn
|
||||||
|
|
||||||
from sanic.errorpages import BaseRenderer, HTMLRenderer, exception_response
|
from sanic.errorpages import BaseRenderer, HTMLRenderer, exception_response
|
||||||
from sanic.exceptions import (
|
from sanic.exceptions import (
|
||||||
|
@ -53,16 +54,15 @@ class ErrorHandler:
|
||||||
|
|
||||||
sig = signature(error_handler.lookup)
|
sig = signature(error_handler.lookup)
|
||||||
if len(sig.parameters) == 1:
|
if len(sig.parameters) == 1:
|
||||||
error_logger.warning(
|
warn(
|
||||||
DeprecationWarning(
|
"You are using a deprecated error handler. The lookup "
|
||||||
"You are using a deprecated error handler. The lookup "
|
"method should accept two positional parameters: "
|
||||||
"method should accept two positional parameters: "
|
"(exception, route_name: Optional[str]). "
|
||||||
"(exception, route_name: Optional[str]). "
|
"Until you upgrade your ErrorHandler.lookup, Blueprint "
|
||||||
"Until you upgrade your ErrorHandler.lookup, Blueprint "
|
"specific exceptions will not work properly. Beginning "
|
||||||
"specific exceptions will not work properly. Beginning "
|
"in v22.3, the legacy style lookup method will not "
|
||||||
"in v22.3, the legacy style lookup method will not "
|
"work at all.",
|
||||||
"work at all."
|
DeprecationWarning,
|
||||||
),
|
|
||||||
)
|
)
|
||||||
error_handler._lookup = error_handler._legacy_lookup
|
error_handler._lookup = error_handler._legacy_lookup
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from typing import TYPE_CHECKING, Optional, Sequence, cast
|
from typing import TYPE_CHECKING, Optional, Sequence, cast
|
||||||
|
from warnings import warn
|
||||||
|
|
||||||
from websockets.connection import CLOSED, CLOSING, OPEN
|
from websockets.connection import CLOSED, CLOSING, OPEN
|
||||||
from websockets.server import ServerConnection
|
from websockets.server import ServerConnection
|
||||||
|
@ -34,27 +35,24 @@ class WebSocketProtocol(HttpProtocol):
|
||||||
self.websocket_max_size = websocket_max_size
|
self.websocket_max_size = websocket_max_size
|
||||||
if websocket_max_queue is not None and websocket_max_queue > 0:
|
if websocket_max_queue is not None and websocket_max_queue > 0:
|
||||||
# TODO: Reminder remove this warning in v22.3
|
# TODO: Reminder remove this warning in v22.3
|
||||||
error_logger.warning(
|
warn(
|
||||||
DeprecationWarning(
|
"Websocket no longer uses queueing, so websocket_max_queue"
|
||||||
"Websocket no longer uses queueing, so websocket_max_queue"
|
" is no longer required.",
|
||||||
" is no longer required."
|
DeprecationWarning,
|
||||||
)
|
|
||||||
)
|
)
|
||||||
if websocket_read_limit is not None and websocket_read_limit > 0:
|
if websocket_read_limit is not None and websocket_read_limit > 0:
|
||||||
# TODO: Reminder remove this warning in v22.3
|
# TODO: Reminder remove this warning in v22.3
|
||||||
error_logger.warning(
|
warn(
|
||||||
DeprecationWarning(
|
"Websocket no longer uses read buffers, so "
|
||||||
"Websocket no longer uses read buffers, so "
|
"websocket_read_limit is not required.",
|
||||||
"websocket_read_limit is not required."
|
DeprecationWarning,
|
||||||
)
|
|
||||||
)
|
)
|
||||||
if websocket_write_limit is not None and websocket_write_limit > 0:
|
if websocket_write_limit is not None and websocket_write_limit > 0:
|
||||||
# TODO: Reminder remove this warning in v22.3
|
# TODO: Reminder remove this warning in v22.3
|
||||||
error_logger.warning(
|
warn(
|
||||||
DeprecationWarning(
|
"Websocket no longer uses write buffers, so "
|
||||||
"Websocket no longer uses write buffers, so "
|
"websocket_write_limit is not required.",
|
||||||
"websocket_write_limit is not required."
|
DeprecationWarning,
|
||||||
)
|
|
||||||
)
|
)
|
||||||
self.websocket_ping_interval = websocket_ping_interval
|
self.websocket_ping_interval = websocket_ping_interval
|
||||||
self.websocket_ping_timeout = websocket_ping_timeout
|
self.websocket_ping_timeout = websocket_ping_timeout
|
||||||
|
|
|
@ -218,20 +218,18 @@ def test_single_arg_exception_handler_notice(exception_handler_app, caplog):
|
||||||
|
|
||||||
exception_handler_app.error_handler = CustomErrorHandler()
|
exception_handler_app.error_handler = CustomErrorHandler()
|
||||||
|
|
||||||
with caplog.at_level(logging.WARNING):
|
message = (
|
||||||
_, response = exception_handler_app.test_client.get("/1")
|
|
||||||
|
|
||||||
for record in caplog.records:
|
|
||||||
if record.message.startswith("You are"):
|
|
||||||
break
|
|
||||||
|
|
||||||
assert record.message == (
|
|
||||||
"You are using a deprecated error handler. The lookup method should "
|
"You are using a deprecated error handler. The lookup method should "
|
||||||
"accept two positional parameters: (exception, route_name: "
|
"accept two positional parameters: (exception, route_name: "
|
||||||
"Optional[str]). Until you upgrade your ErrorHandler.lookup, "
|
"Optional[str]). Until you upgrade your ErrorHandler.lookup, "
|
||||||
"Blueprint specific exceptions will not work properly. Beginning in "
|
"Blueprint specific exceptions will not work properly. Beginning in "
|
||||||
"v22.3, the legacy style lookup method will not work at all."
|
"v22.3, the legacy style lookup method will not work at all."
|
||||||
)
|
)
|
||||||
|
with pytest.warns(DeprecationWarning) as record:
|
||||||
|
_, response = exception_handler_app.test_client.get("/1")
|
||||||
|
|
||||||
|
assert len(record) == 1
|
||||||
|
assert record[0].message.args[0] == message
|
||||||
assert response.status == 400
|
assert response.status == 400
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user