fix: guard WebSocketClientProtocol import behind TYPE_CHECKING (fixes #1666)#1687
Open
mac-agent wants to merge 1 commit intosammchardy:masterfrom
Open
fix: guard WebSocketClientProtocol import behind TYPE_CHECKING (fixes #1666)#1687mac-agent wants to merge 1 commit intosammchardy:masterfrom
mac-agent wants to merge 1 commit intosammchardy:masterfrom
Conversation
…ammchardy#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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
With
websockets>=14, the runtime import ofWebSocketClientProtocoltriggers aDeprecationWarning:This comes from
binance/ws/websocket_api.py:4where the class is imported unconditionally, but only used for type hints and a runtimeisinstancecheck.Fix
TYPE_CHECKING: The class is only needed for static type hints, so moving it behind the guard removes the runtime import warning entirely.isinstancewithhasattr: Since the class is no longer available at runtime, the check forws.closednow useshasattrinstead.No behavioral change — the WebSocket connection logic remains identical.
Addresses #1666.