Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -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)
------------------

Expand Down
2 changes: 1 addition & 1 deletion src/wsproto/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
13 changes: 6 additions & 7 deletions src/wsproto/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Events that result from processing data on a WebSocket connection.
"""

from __future__ import annotations

from abc import ABC
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -242,26 +243,24 @@ 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.

Fields:

.. 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):
Expand Down