From 46050ba4ea4286421cc8a7b5996e7aba81c28804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?mac=20=F0=9F=A6=A6?= Date: Wed, 1 Apr 2026 07:11:40 -0400 Subject: [PATCH] fix: guard WebSocketClientProtocol import behind TYPE_CHECKING (fixes #1666) With websockets>=14, importing WebSocketClientProtocol at runtime triggers DeprecationWarning about websockets.legacy being deprecated. - Move the WebSocketClientProtocol import behind TYPE_CHECKING guard so it's only used for static type hints - Replace isinstance check with hasattr check for the 'closed' attribute since the class is no longer imported at runtime This eliminates the deprecation warning without changing behavior. See: https://websockets.readthedocs.io/en/stable/howto/upgrade.html --- binance/ws/websocket_api.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/binance/ws/websocket_api.py b/binance/ws/websocket_api.py index ae62e788..b44e09ad 100644 --- a/binance/ws/websocket_api.py +++ b/binance/ws/websocket_api.py @@ -1,7 +1,8 @@ -from typing import Dict, Optional +from typing import TYPE_CHECKING, Dict, Optional import asyncio -from websockets import WebSocketClientProtocol # type: ignore +if TYPE_CHECKING: + from websockets import WebSocketClientProtocol # type: ignore # noqa: F401 from .constants import WSListenerState from .reconnecting_websocket import ReconnectingWebsocket @@ -117,7 +118,7 @@ async def _ensure_ws_connection(self) -> None: try: if ( self.ws is None - or (isinstance(self.ws, WebSocketClientProtocol) and self.ws.closed) + or (hasattr(self.ws, "closed") and self.ws.closed) or self.ws_state != WSListenerState.STREAMING ): await self.connect()