-
-
Notifications
You must be signed in to change notification settings - Fork 581
Description
A text argument to broadcast() similar to how send() handles it would allow me to change a line like
websockets.broadcast(connections, data.decode("utf-8")) (only to immediately encode it again) to
websockets.broadcast(connections, data, text=True)
which would also relieve the code at that point from knowing whether data is bytes or str, only that it needs to go into a "Text" frame.
I've been testing this change to asyncio-broadcast so far with no ill effects.
src/websockets/asyncio/connection.py:
def broadcast(
connections: Iterable[Connection],
message: DataLike,
raise_exceptions: bool = False,
*,
text: bool | None = None,
) -> None:
"""
...
"""
if isinstance(message, str):
if text is None:
send_method = "send_text"
message = message.encode()
elif isinstance(message, BytesLike):
if text is None:
send_method = "send_binary"
else:
raise TypeError("data must be str or bytes")
if text is not None:
send_method = "send_text" if text else "send_binary"
( in a branch here )
What would be the chances of getting type-overrides backported to 13.x? (Mostly j/k, but I have one test environment with Python 3.8/websockets 13 left and the patch applies the same to the 13.x branch but lacks the change to send())