-
-
Notifications
You must be signed in to change notification settings - Fork 569
Closed
Labels
Description
Currently recv and recv_streaming are typed as always returning Data, even if decode is set. This means that to satisfy type checkers, we need to use isinstance, cast, etc.
async for chunk in ws.recv_streaming(decode=False):
assert isinstance(chunk, bytes)
...
I propose using typing.overload to narrow the return type when decode is not None:
@overload
async def recv(self, decode: Literal[True]) -> str:
...
@overload
async def recv(self, decode: Literal[False]) -> bytes:
...
@overload
async def recv(self, decode: None) -> Data:
...
async def recv(self, decode: bool | None = None) -> Data:
"""(actual implementation)"""
Alternatively, new recv_str/recv_bytes methods can automatically raise an Exception if the received data is not of the expected type.