From 3b7fa7673bf6a96a5e9debd7dcfa65e04f85efbb Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sun, 28 Jan 2024 16:49:13 +0100 Subject: [PATCH] Enable deprecation for second argument of handlers. --- docs/project/changelog.rst | 26 ++++++++++++++++++++------ src/websockets/legacy/server.py | 4 +--- tests/legacy/test_client_server.py | 6 ++---- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/docs/project/changelog.rst b/docs/project/changelog.rst index dc84a5ae..fd186a5f 100644 --- a/docs/project/changelog.rst +++ b/docs/project/changelog.rst @@ -43,6 +43,21 @@ Backwards-incompatible changes For backwards compatibility, ``ssl_context`` is still supported. +.. admonition:: Receiving the request path in the second parameter of connection + handlers is deprecated. + :class: note + + If you implemented the connection handler of a server as:: + + async def handler(request, path): + ... + + You should switch to the recommended pattern since 10.1:: + + async def handler(request): + path = request.path # only if handler() uses the path argument + ... + New features ............ @@ -257,20 +272,19 @@ New features * Added a tutorial. -* Made the second parameter of connection handlers optional. It will be - deprecated in the next major release. The request path is available in - the :attr:`~legacy.protocol.WebSocketCommonProtocol.path` attribute of - the first argument. +* Made the second parameter of connection handlers optional. The request path is + available in the :attr:`~legacy.protocol.WebSocketCommonProtocol.path` + attribute of the first argument. If you implemented the connection handler of a server as:: async def handler(request, path): ... - You should replace it by:: + You should replace it with:: async def handler(request): - path = request.path # if handler() uses the path argument + path = request.path # only if handler() uses the path argument ... * Added ``python -m websockets --version``. diff --git a/src/websockets/legacy/server.py b/src/websockets/legacy/server.py index e8cf8220..4659ed9a 100644 --- a/src/websockets/legacy/server.py +++ b/src/websockets/legacy/server.py @@ -1168,9 +1168,7 @@ def remove_path_argument( pass else: # ws_handler accepts two arguments; activate backwards compatibility. - - # Enable deprecation warning and announce deprecation in 11.0. - # warnings.warn("remove second argument of ws_handler", DeprecationWarning) + warnings.warn("remove second argument of ws_handler", DeprecationWarning) async def _ws_handler(websocket: WebSocketServerProtocol) -> Any: return await cast( diff --git a/tests/legacy/test_client_server.py b/tests/legacy/test_client_server.py index 4a21f7ce..51a74734 100644 --- a/tests/legacy/test_client_server.py +++ b/tests/legacy/test_client_server.py @@ -480,8 +480,7 @@ async def handler_with_path(ws, path): with self.temp_server( handler=handler_with_path, - # Enable deprecation warning and announce deprecation in 11.0. - # deprecation_warnings=["remove second argument of ws_handler"], + deprecation_warnings=["remove second argument of ws_handler"], ): with self.temp_client("/path"): self.assertEqual( @@ -497,8 +496,7 @@ async def handler_with_path(ws, path, extra): with self.temp_server( handler=bound_handler_with_path, - # Enable deprecation warning and announce deprecation in 11.0. - # deprecation_warnings=["remove second argument of ws_handler"], + deprecation_warnings=["remove second argument of ws_handler"], ): with self.temp_client("/path"): self.assertEqual(