python-hyper / wsproto

Sans-IO WebSocket protocol implementation
https://wsproto.readthedocs.io/
MIT License
261 stars 38 forks source link

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

Open frankie567 opened 1 year ago

frankie567 commented 1 year ago

I'm using wsproto in my library httpx-ws. One contributor reported in https://github.com/frankie567/httpx-ws/discussions/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.

njsmith commented 1 year ago

Oh interesting.

The type should probably be "bytes | bytearray", ie you should assume it's something that quacks like a bytes but maybe not that exactly.

And it looks like this is actually what the type already is, of you're using mypy:

https://github.com/python/typing/issues/552

But apparently mypy didn't get around to pushing this upstream to the standards – so I'm guessing that pyright doesn't implement this little hack, and these days it's popular enough that this matters?

(I guess we should confirm that. If pyright also has this hack then we might not want to bother fixing it.)

On Wed, Jul 12, 2023, 04:57 François Voron @.***> wrote:

I'm using wsproto in my library httpx-ws. One contributor reported in frankie567/httpx-ws#38 https://github.com/frankie567/httpx-ws/discussions/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, ConnectionTypefrom 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.

— Reply to this email directly, view it on GitHub https://github.com/python-hyper/wsproto/issues/185, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEU42EXXHAVURZXLCWPX5LXP2GLNANCNFSM6AAAAAA2HMABXA . You are receiving this because you are subscribed to this thread.Message ID: @.***>