zeromq / pyzmq

PyZMQ: Python bindings for zeromq
http://zguide.zeromq.org/py:all
BSD 3-Clause "New" or "Revised" License
3.73k stars 636 forks source link

BUG: zmq.asyncio.Socket.send_pyobj returns a Future, but is not typed as such #2034

Closed Ultrasonic1209 closed 1 month ago

Ultrasonic1209 commented 1 month ago

This is a pyzmq bug

What pyzmq version?

26.2.0

What libzmq version?

4.3.5

Python version (and how it was installed)

python 3.11.10, via uv venv

OS

Debian 11 (WSL2) - VSCode Dev Container

What happened?

zmq.asyncio.Socket.send_pyobj returns a Future, but is not typed as such.

This is VSCode (Pylance) incorrectly stating that the function return is not awaitable: image

And this is VSCode's debugger showing that the function does return an awaitable: image

Code to reproduce bug

import asyncio
import zmq.asyncio

context = zmq.asyncio.Context()  # type: ignore (#2028)

socket = context.socket(zmq.PUSH)
socket.connect('tcp://localhost:5555')

async def start():
    await socket.send_string("123")
    ret = await socket.send_pyobj([1, 2, 3])
    print("made it to the end!")

if __name__ == "__main__":
    asyncio.run(start())

Traceback, if applicable

No response

More info

No response

minrk commented 1 month ago

I don't quite follow the screenshots or what they mean, but this sounds like a pylance/vscode bug, not pyzmq. Futures are awaitable.

The important thing here is that if these methods were changed to coroutines, the typing would not change. That wouldn't be true if they were typed as the more specific Future.

minrk commented 1 month ago

Ah, I think this is actually the same as the (pylance) bug causing #2028, so your zmq.asyncio.Context is ending up typed as zmq.Context, instead. The fix is probably the same. I think if you cast your Context to zmq.asyncio.Context, the typing of your Socket will be fixed.

minrk commented 1 month ago

I see it now, the async sockets don't have annotations for some of the wrapper methods. Should be easy enough to fix, I think. Thanks for the report!