aio-libs / aiosmtpd

A reimplementation of the Python stdlib smtpd.py based on asyncio.
https://aiosmtpd.aio-libs.org
Apache License 2.0
325 stars 96 forks source link

Incorrect type annotation for Session.peer #518

Open cuu508 opened 1 month ago

cuu508 commented 1 month ago

The type annotation for Session.peer is currently:

self.peer: Optional[str] = None

This attribute appears to be set in connection_made() like so:

self.session.peer = transport.get_extra_info("peername")

Where transport is an instance of asyncio.BaseTransport.

Looking at BaseTransport docs, get_extra_info("peername") calls socket.getpeername() and returns the result.

For IPv4 sockets at least, socket.getpeername() seems to return an ip-address,port tuple. So I think a more accurate annotation for Session.peer would be something like tuple[str, int] | None.

cuu508 commented 1 month ago

This is a little tricky than I thought :-)

aiosmtpd can listen on IPv4 address, on IPv6 address (by using aiosmtpd.controller.Controller), and to an UNIX socket (by using aiosmtpd.controller.UnixSocketController). The type of Session.peer is different in each case:

So the combined type annotation would be something like

tuple[str, int] | tuple[str, int, int, int] | str | None

Or, in Python 3.8 compatible syntax:

Union[Tuple[str, int], Tuple[str, int, int, int], str, None]