Skip to content

BytesMessage.data is a bytearray but type-hinted as bytes #185

Description

@frankie567

I'm using wsproto in my library httpx-ws. One contributor reported in frankie567/httpx-ws#38, that the data property of BytesMessage is actually a bytearray at runtime, but it's typed as bytes.

Reproducible example

from wsproto import WSConnection, ConnectionType
from wsproto.events import Request, BytesMessage, AcceptConnection

client = WSConnection(ConnectionType.CLIENT)
server = WSConnection(ConnectionType.SERVER)

msg = client.send(Request(host='echo.websocket.org', target='/'))
server.receive_data(msg)

msg = server.send(AcceptConnection())
client.receive_data(msg)

msg = server.send(BytesMessage(b"Hello"))
client.receive_data(msg)

for event in client.events():
    if isinstance(event, BytesMessage):
        print(type(event.data))  # bytearray

Additional comment

It's not really a problem since bytes and bytearray are nearly the same, but from type-hints point-of-view, maybe BytesMessage.data should be typed as bytearray.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions