From 0d7a948fd0778b07c078621b34ea8ac89f89f145 Mon Sep 17 00:00:00 2001 From: Glyph Date: Sun, 16 Nov 2025 12:19:25 -0800 Subject: [PATCH 1/3] bytes are still a valid type here --- src/wsproto/events.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) 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): From c8a877eed127a4700974ad45b886d13bb1151150 Mon Sep 17 00:00:00 2001 From: Glyph Date: Sun, 16 Nov 2025 12:19:55 -0800 Subject: [PATCH 2/3] don't unnecessarily construct bytearrays over and over again --- src/wsproto/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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, ) From 8eeb5958039862cd0c7c2780567356c531226ac7 Mon Sep 17 00:00:00 2001 From: Glyph Date: Sun, 16 Nov 2025 13:33:05 -0800 Subject: [PATCH 3/3] changelog entry --- CHANGELOG.rst | 6 ++++++ 1 file changed, 6 insertions(+) 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) ------------------