From 3c688672559fa4e10c35553eae6f9bde5810be1c Mon Sep 17 00:00:00 2001 From: popsiclelmlm <7487674+popsiclelmlm@users.noreply.github.com> Date: Thu, 11 Jun 2026 03:42:48 +0800 Subject: [PATCH] Allow IPv6 datagram protocol addresses --- .../test_cases/asyncio/check_datagram_protocol.py | 11 +++++++++++ stdlib/asyncio/protocols.pyi | 11 +++++------ 2 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 stdlib/@tests/test_cases/asyncio/check_datagram_protocol.py diff --git a/stdlib/@tests/test_cases/asyncio/check_datagram_protocol.py b/stdlib/@tests/test_cases/asyncio/check_datagram_protocol.py new file mode 100644 index 000000000000..f51557aa28c5 --- /dev/null +++ b/stdlib/@tests/test_cases/asyncio/check_datagram_protocol.py @@ -0,0 +1,11 @@ +from __future__ import annotations + +import asyncio + + +class IPv4DatagramProtocol(asyncio.DatagramProtocol): + def datagram_received(self, data: bytes, addr: tuple[str, int]) -> None: ... + + +class IPv6DatagramProtocol(asyncio.DatagramProtocol): + def datagram_received(self, data: bytes, addr: tuple[str, int, int, int]) -> None: ... diff --git a/stdlib/asyncio/protocols.pyi b/stdlib/asyncio/protocols.pyi index 3a8965f03e29..600acadc08fe 100644 --- a/stdlib/asyncio/protocols.pyi +++ b/stdlib/asyncio/protocols.pyi @@ -1,6 +1,6 @@ from _typeshed import ReadableBuffer from asyncio import transports -from typing import Any +from socket import _RetAddress # Keep asyncio.__all__ updated with any changes to __all__ here __all__ = ("BaseProtocol", "Protocol", "DatagramProtocol", "SubprocessProtocol", "BufferedProtocol") @@ -27,11 +27,10 @@ class BufferedProtocol(BaseProtocol): class DatagramProtocol(BaseProtocol): __slots__ = () def connection_made(self, transport: transports.DatagramTransport) -> None: ... # type: ignore[override] - # addr can be a tuple[int, int] for some unusual protocols like socket.AF_NETLINK. - # 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: ... + # addr varies by socket family and can be a tuple[str, int, int, int] for IPv6, + # tuple[int, int] for some unusual protocols like socket.AF_NETLINK, or other + # shapes. Keep this aligned with socket.recvfrom's return address type. + def datagram_received(self, data: bytes, addr: _RetAddress) -> None: ... def error_received(self, exc: Exception) -> None: ... class SubprocessProtocol(BaseProtocol):