Skip to content

Wrong type hint for asyncio.base_events.Server sockets attribute #5535

@ramalho

Description

@ramalho

The fix for issue #2364 ("AbstractServer" has no attribute "sockets") added this declaration:

class AbstractServer:
    sockets: Optional[List[socket]]

However, in the Python 3.9 implementation of asyncio.base_events.Server, sockets is a property defined like this:

    @property
    def sockets(self):
        if self._sockets is None:
             return ()
        return tuple(trsock.TransportSocket(s) for s in self._sockets)

So we have two problems:

  1. The value of sockets is never None, it's always a tuple.
  2. It is really a tuple[TransportSocket, ...].

There is example code in the asyncio docs and test code in the standard library that assume sockets is a sequence of sockets.

@ymyzk contributed the patch for #2364 noting "I noticed that this class has some changes in Python 3.7, so we need to update it again in the near future."

The documentation for asyncio.Server.sockets states:

Changed in version 3.7: Prior to Python 3.7 Server.sockets used to return an internal list of server sockets directly. In 3.7 a copy of that list is returned.

As I mentioned, the actual value returned is a tuple of zero or more asyncio.Server.TransportSocket objects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions