diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 10cf0f1..8542634 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,12 @@ Release History =============== +1.3.2 (2025-11-XX) +------------------ + +- Fix type hints to allow BytesMessage to accept bytes again, as well as + bytearray. + 1.3.1 (2025-11-12) ------------------ diff --git a/src/wsproto/connection.py b/src/wsproto/connection.py index 84751ab..1af3aac 100644 --- a/src/wsproto/connection.py +++ b/src/wsproto/connection.py @@ -191,7 +191,7 @@ def events(self) -> Generator[Event, None, None]: elif frame.opcode is Opcode.BINARY: assert isinstance(frame.payload, (bytes, bytearray)) yield BytesMessage( - data=bytearray(frame.payload), + data=frame.payload, frame_finished=frame.frame_finished, message_finished=frame.message_finished, ) diff --git a/src/wsproto/events.py b/src/wsproto/events.py index 4faf6e4..9474654 100644 --- a/src/wsproto/events.py +++ b/src/wsproto/events.py @@ -4,6 +4,7 @@ Events that result from processing data on a WebSocket connection. """ + from __future__ import annotations from abc import ABC @@ -193,7 +194,7 @@ def response(self) -> CloseConnection: return CloseConnection(code=self.code, reason=self.reason) -T = TypeVar("T", bytes, bytearray, str) +T = TypeVar("T", bytes | bytearray, str) @dataclass(frozen=True) @@ -242,11 +243,11 @@ class TextMessage(Message[str]): # pylint: disable=unsubscriptable-object and reassemble these chunks to get the full message. """ - data: str - @dataclass(frozen=True) -class BytesMessage(Message[bytearray]): # pylint: disable=unsubscriptable-object +class BytesMessage( + Message[bytearray | bytes] # pylint: disable=unsubscriptable-object +): """ Fired when a data frame with BINARY payload is received. @@ -254,14 +255,12 @@ class BytesMessage(Message[bytearray]): # pylint: disable=unsubscriptable-objec .. attribute:: data - The message data as bytearray, can be decoded as UTF-8 for + The message data as bytes or a bytearray, can be decoded as UTF-8 for TEXT messages. This only represents a single chunk of data and not a full WebSocket message. You need to buffer and reassemble these chunks to get the full message. """ - data: bytearray - @dataclass(frozen=True) class Ping(Event):