Skip to content

Commit

Permalink
Enable deprecation for second argument of handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaugustin committed Jan 28, 2024
1 parent 96fddaf commit 3b7fa76
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
26 changes: 20 additions & 6 deletions docs/project/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
............

Expand Down Expand Up @@ -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``.
Expand Down
4 changes: 1 addition & 3 deletions src/websockets/legacy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions tests/legacy/test_client_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 3b7fa76

Please sign in to comment.