-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix websockets deprecation warning #3749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 8 commits
24d215e
814657a
13c51da
d5dc1e4
68105d0
bdf5e5a
aa1f155
304f994
b473927
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Remove websockets deprecation warning by using the asyncio websocket provider |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,6 @@ | |
|
|
||
| from websockets import ( | ||
| WebSocketException, | ||
| ) | ||
| from websockets.legacy.client import ( | ||
| connect, | ||
| ) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| from __future__ import ( | ||
| annotations, | ||
| ) | ||
|
|
||
| import asyncio | ||
| import json | ||
| import logging | ||
| import os | ||
| from typing import ( | ||
| Any, | ||
| Dict, | ||
| Optional, | ||
| Union, | ||
| ) | ||
|
|
||
| from eth_typing import ( | ||
|
|
@@ -15,14 +16,21 @@ | |
| from toolz import ( | ||
| merge, | ||
| ) | ||
|
|
||
| # python3.8 supports up to version 13, | ||
| # which does not default to the asyncio implementation yet. | ||
| # For this reason connect and ClientConnection need to be imported | ||
| # from asyncio.client explicitly. | ||
| # When web3.py stops supporting python3.8, | ||
| # it'll be possible to use `from websockets import connect, ClientConnection`. | ||
| from websockets.asyncio.client import ( | ||
| ClientConnection, | ||
| connect, | ||
| ) | ||
| from websockets.exceptions import ( | ||
| ConnectionClosedOK, | ||
| WebSocketException, | ||
| ) | ||
| from websockets.legacy.client import ( | ||
| WebSocketClientProtocol, | ||
| connect, | ||
| ) | ||
|
|
||
| from web3.exceptions import ( | ||
| PersistentConnectionClosedOK, | ||
|
|
@@ -57,12 +65,14 @@ class WebSocketProvider(PersistentConnectionProvider): | |
| logger = logging.getLogger("web3.providers.WebSocketProvider") | ||
| is_async: bool = True | ||
|
|
||
| _ws: ClientConnection | ||
|
|
||
| def __init__( | ||
| self, | ||
| endpoint_uri: Optional[Union[URI, str]] = None, | ||
| websocket_kwargs: Optional[Dict[str, Any]] = None, | ||
| endpoint_uri: URI | str | None = None, | ||
| websocket_kwargs: dict[str, Any] | None = None, | ||
| # uses binary frames by default | ||
| use_text_frames: Optional[bool] = False, | ||
| use_text_frames: bool | None = False, | ||
| # `PersistentConnectionProvider` kwargs can be passed through | ||
| **kwargs: Any, | ||
| ) -> None: | ||
|
|
@@ -72,7 +82,7 @@ def __init__( | |
| ) | ||
| super().__init__(**kwargs) | ||
| self.use_text_frames = use_text_frames | ||
| self._ws: Optional[WebSocketClientProtocol] = None | ||
| self._ws: ClientConnection | None = None | ||
|
|
||
| if not any( | ||
| self.endpoint_uri.startswith(prefix) | ||
|
|
@@ -119,7 +129,7 @@ async def socket_send(self, request_data: bytes) -> None: | |
| "Connection to websocket has not been initiated for the provider." | ||
| ) | ||
|
|
||
| payload: Union[bytes, str] = request_data | ||
| payload: bytes | str = request_data | ||
| if self.use_text_frames: | ||
| payload = request_data.decode("utf-8") | ||
|
|
||
|
|
@@ -136,7 +146,7 @@ async def _provider_specific_connect(self) -> None: | |
|
|
||
| async def _provider_specific_disconnect(self) -> None: | ||
| # this should remain idempotent | ||
| if self._ws is not None and not self._ws.closed: | ||
| if self._ws is not None: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we still need to figure out how to check for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey, I did check this and I found out that I also noticed this issue #3679 which plans to move websockets bottom pin to >=14 #3530, which would essentially remove this problem completely.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could either go down the path of supporting all the variations (aka <13; >=13,<14; >=14), i.e. import websockets
websockets_version = tuple(int(x) for x in websockets.__version__.split(".") if x.isdigit())
if websockets_version < (13, 0):
from websockets.legacy.client import (
WebSocketClientProtocol as ClientConnection, # we are safe to steal the name as the scope of interface we use is the same
connect,
)
else:
# python3.8 supports up to version 13,
# which does not default to the asyncio implementation yet.
# For this reason connect and ClientConnection need to be imported
# from asyncio.client explicitly.
# When web3.py stops supporting python3.8,
# it'll be possible to use `from websockets import connect, ClientConnection`.
from websockets.asyncio.client import (
ClientConnection,
connect,
)or simply drop support for at least version <13. Given the amount of usage of the websockets library interface either solution is fine, not a big deal. |
||
| await self._ws.close() | ||
| self._ws = None | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.