Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect type annotation for Session.peer #518

Open
cuu508 opened this issue Oct 21, 2024 · 1 comment · May be fixed by #522
Open

Incorrect type annotation for Session.peer #518

cuu508 opened this issue Oct 21, 2024 · 1 comment · May be fixed by #522

Comments

@cuu508
Copy link
Contributor

cuu508 commented Oct 21, 2024

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 added a commit to cuu508/aiosmtpd that referenced this issue Oct 28, 2024
@cuu508 cuu508 linked a pull request Oct 28, 2024 that will close this issue
10 tasks
@cuu508
Copy link
Contributor Author

cuu508 commented Oct 28, 2024

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:

  • IPv4: tuple[str, int] (hostname, port)
  • IPv6: tuple[str, int, int, int], (hostname, port, flowinfo, scope_id)
  • UNIX socket: str (not sure what it is supposed to return, in practice when testing locally with swaks as the client, I got an empty string back)

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant