From 684f8cd080bc2b8d58f1f0944d937ddbe4613e7a Mon Sep 17 00:00:00 2001 From: nightcityblade Date: Thu, 30 Jul 2026 23:27:49 +0800 Subject: [PATCH 1/2] Fix DatagramProtocol address type for IPv6 --- stdlib/@tests/test_cases/asyncio/check_protocols.py | 5 +++++ stdlib/asyncio/protocols.pyi | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 stdlib/@tests/test_cases/asyncio/check_protocols.py diff --git a/stdlib/@tests/test_cases/asyncio/check_protocols.py b/stdlib/@tests/test_cases/asyncio/check_protocols.py new file mode 100644 index 000000000000..e96af615a750 --- /dev/null +++ b/stdlib/@tests/test_cases/asyncio/check_protocols.py @@ -0,0 +1,5 @@ +import asyncio + +protocol = asyncio.DatagramProtocol() +protocol.datagram_received(b"", ("127.0.0.1", 80)) +protocol.datagram_received(b"", ("::1", 80, 0, 0)) diff --git a/stdlib/asyncio/protocols.pyi b/stdlib/asyncio/protocols.pyi index 3a8965f03e29..a897b278f264 100644 --- a/stdlib/asyncio/protocols.pyi +++ b/stdlib/asyncio/protocols.pyi @@ -31,7 +31,7 @@ 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: ... + 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): From c23e44a17b791786e24e217502564b6421a353f1 Mon Sep 17 00:00:00 2001 From: nightcityblade Date: Sat, 1 Aug 2026 08:56:40 +0800 Subject: [PATCH 2/2] Fix DatagramProtocol address type --- stdlib/@tests/test_cases/asyncio/check_protocols.py | 5 ----- stdlib/asyncio/protocols.pyi | 8 +++----- 2 files changed, 3 insertions(+), 10 deletions(-) delete mode 100644 stdlib/@tests/test_cases/asyncio/check_protocols.py diff --git a/stdlib/@tests/test_cases/asyncio/check_protocols.py b/stdlib/@tests/test_cases/asyncio/check_protocols.py deleted file mode 100644 index e96af615a750..000000000000 --- a/stdlib/@tests/test_cases/asyncio/check_protocols.py +++ /dev/null @@ -1,5 +0,0 @@ -import asyncio - -protocol = asyncio.DatagramProtocol() -protocol.datagram_received(b"", ("127.0.0.1", 80)) -protocol.datagram_received(b"", ("::1", 80, 0, 0)) diff --git a/stdlib/asyncio/protocols.pyi b/stdlib/asyncio/protocols.pyi index a897b278f264..f63b9e084382 100644 --- a/stdlib/asyncio/protocols.pyi +++ b/stdlib/asyncio/protocols.pyi @@ -27,11 +27,9 @@ 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] | tuple[str, int, int, int]) -> None: ... + # addr is a tuple[str, int] for IPv4 or tuple[str, int, int, int] for IPv6. + # It can also be a tuple[int, int] for unusual protocols like socket.AF_NETLINK. + def datagram_received(self, data: bytes, addr: tuple[Any, ...]) -> None: ... def error_received(self, exc: Exception) -> None: ... class SubprocessProtocol(BaseProtocol):