Skip to content

Commit 1ea2026

Browse files
cursoragentP4X-ng
andcommitted
Stabilize websocket connection typing
Co-authored-by: P4x-ng <P4X-ng@users.noreply.github.com>
1 parent 731aff7 commit 1ea2026

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

cdp/connection.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,33 @@
1111
import json
1212
import logging
1313
import typing
14-
from dataclasses import dataclass, field
14+
from dataclasses import dataclass
1515

1616
try:
1717
import websockets
18-
from websockets.client import WebSocketClientProtocol
1918
WEBSOCKETS_AVAILABLE = True
2019
except ImportError:
2120
WEBSOCKETS_AVAILABLE = False
22-
WebSocketClientProtocol = typing.Any # type: ignore
2321

2422
from cdp.util import parse_json_event, T_JSON_DICT
2523

2624

2725
logger = logging.getLogger(__name__)
2826

2927

28+
class WebSocketConnection(typing.Protocol):
29+
"""Minimal websocket surface used by CDPConnection."""
30+
31+
async def send(self, message: str) -> None:
32+
...
33+
34+
async def recv(self) -> str:
35+
...
36+
37+
async def close(self) -> None:
38+
...
39+
40+
3041
class CDPError(Exception):
3142
"""Base exception for CDP errors."""
3243
pass
@@ -92,7 +103,7 @@ def __init__(self, url: str, timeout: float = 30.0):
92103

93104
self.url = url
94105
self.timeout = timeout
95-
self._ws: typing.Optional[WebSocketClientProtocol] = None
106+
self._ws: typing.Optional[WebSocketConnection] = None
96107
self._next_command_id = 1
97108
self._pending_commands: typing.Dict[int, PendingCommand] = {}
98109
self._event_queue: asyncio.Queue = asyncio.Queue()

0 commit comments

Comments
 (0)