Skip to content

Commit da11f18

Browse files
committed
fix + add tests
1 parent 51c7a2b commit da11f18

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from __future__ import annotations
2+
3+
from asyncio import DatagramProtocol
4+
5+
6+
class IPv4Protocol(DatagramProtocol):
7+
def datagram_received(self, data: bytes, addr: tuple[str, int]) -> None: ...
8+
9+
10+
class IPv6Protocol(DatagramProtocol):
11+
def datagram_received(self, data: bytes, addr: tuple[str, int, int, int]) -> None: ...
12+
13+
14+
class NetlinkProtocol(DatagramProtocol):
15+
def datagram_received(self, data: bytes, addr: tuple[int, int]) -> None: ...

stdlib/asyncio/protocols.pyi

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from _typeshed import ReadableBuffer
22
from asyncio import transports
3+
from typing import Any
4+
from typing_extensions import Unpack
35

46
# Keep asyncio.__all__ updated with any changes to __all__ here
57
__all__ = ("BaseProtocol", "Protocol", "DatagramProtocol", "SubprocessProtocol", "BufferedProtocol")
@@ -26,9 +28,12 @@ class BufferedProtocol(BaseProtocol):
2628
class DatagramProtocol(BaseProtocol):
2729
__slots__ = ()
2830
def connection_made(self, transport: transports.DatagramTransport) -> None: ... # type: ignore[override]
29-
# Address is a 2-tuple (host, port) for IPv4, 4-tuple (host, port, flowinfo, scope_id) for IPv6,
30-
# or tuple[int, int] for some unusual protocols like socket.AF_NETLINK.
31-
def datagram_received(self, data: bytes, addr: tuple[str | int, int] | tuple[str, int, int, int]) -> None: ...
31+
# addr can be a tuple[int, int] for some unusual protocols like socket.AF_NETLINK and
32+
# a 4-tuple (host, port, flowinfo, scope_id) for IPv6,
33+
# Use tuple[str | Any, int] to not cause typechecking issues on most usual cases.
34+
# This could be improved by using tuple[AnyOf[str, int], int] if the AnyOf feature is accepted.
35+
# See https://github.com/python/typing/issues/566
36+
def datagram_received(self, data: bytes, addr: tuple[str | Any, int, Unpack[tuple[Any, ...]]]) -> None: ...
3237
def error_received(self, exc: Exception) -> None: ...
3338

3439
class SubprocessProtocol(BaseProtocol):

0 commit comments

Comments
 (0)