Skip to content

Commit 003c265

Browse files
authored
Fix Session.peer type annotation to support IPv6 and UNIX sockets
PR #522 Fixes #518
1 parent dba0ff7 commit 003c265

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

aiosmtpd/handlers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from aiosmtpd import _get_or_new_eventloop
2828
from aiosmtpd.smtp import SMTP as SMTPServer
2929
from aiosmtpd.smtp import Envelope as SMTPEnvelope
30+
from aiosmtpd.smtp import PeerType
3031
from aiosmtpd.smtp import Session as SMTPSession
3132

3233
T = TypeVar("T")
@@ -38,7 +39,7 @@
3839
log = logging.getLogger("mail.debug")
3940

4041

41-
def _format_peer(peer: str) -> str:
42+
def _format_peer(peer: PeerType) -> str:
4243
# This is a separate function mostly so the test suite can craft a
4344
# reproducible output.
4445
return "X-Peer: {!r}".format(peer)
@@ -76,7 +77,7 @@ def from_cli(cls: Type[T], parser: ArgumentParser, *args) -> T:
7677
parser.error("Debugging usage: [stdout|stderr]")
7778
return cls(stream) # type: ignore[call-arg]
7879

79-
def _print_message_content(self, peer: str, data: Union[str, bytes]) -> None:
80+
def _print_message_content(self, peer: PeerType, data: Union[str, bytes]) -> None:
8081
in_headers = True
8182
for line in data.splitlines():
8283
# Dump the RFC 2822 headers first.

aiosmtpd/smtp.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class _DataState(enum.Enum):
6565
RT = TypeVar("RT") # "ReturnType"
6666
DecoratorType = Callable[[Callable[..., RT]], Callable[..., RT]]
6767

68+
# IPv4, IPv6, UNIX socket, not set
69+
PeerType = Union[tuple[str, int], tuple[str, int, int, int], str, None]
6870

6971
# endregion
7072

@@ -155,7 +157,7 @@ def __repr__(self) -> str:
155157
@public
156158
class Session:
157159
def __init__(self, loop: asyncio.AbstractEventLoop):
158-
self.peer: Optional[str] = None
160+
self.peer: PeerType = None
159161
self.ssl: Optional[dict[str, Any]] = None
160162
self.host_name: Optional[str] = None
161163
self.extended_smtp = False

0 commit comments

Comments
 (0)