From 60be8e95a5144f865e2a9ea529dc22ac63625e67 Mon Sep 17 00:00:00 2001 From: Sedat Date: Mon, 23 Mar 2026 20:30:32 +1100 Subject: [PATCH] [asyncio] Support IPv6 addr type in DatagramProtocol.datagram_received When using IPv6, the `addr` parameter passed to `datagram_received` is a 4-tuple `(host, port, flowinfo, scope_id)` of type `tuple[str, int, int, int]`, not a 2-tuple. Add this case to the union type so IPv6 datagram protocols can be properly typed without resorting to `Any`. Fixes #15169 --- stdlib/asyncio/protocols.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/asyncio/protocols.pyi b/stdlib/asyncio/protocols.pyi index 3a8965f03e29..3055a2c01b76 100644 --- a/stdlib/asyncio/protocols.pyi +++ b/stdlib/asyncio/protocols.pyi @@ -31,7 +31,8 @@ class DatagramProtocol(BaseProtocol): # Use tuple[str | Any, int] to not cause typechecking issues on most usual cases. # This could be improved by using tuple[AnyOf[str, int], int] if the AnyOf feature is accepted. # See https://github.com/python/typing/issues/566 - def datagram_received(self, data: bytes, addr: tuple[str | Any, int]) -> None: ... + # For IPv6, addr is a 4-tuple: (host, port, flowinfo, scope_id). + def datagram_received(self, data: bytes, addr: tuple[str | Any, int] | tuple[str, int, int, int]) -> None: ... def error_received(self, exc: Exception) -> None: ... class SubprocessProtocol(BaseProtocol):