Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions stdlib/@tests/test_cases/asyncio/check_datagram_protocol.py
Original file line number Diff line number Diff line change
@@ -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: ...
Comment on lines +1 to +11

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For performance and other reasons we only use tests in extraordinary or edge cases. These tests basically test whether type checkers process Any correctly. We don't need them.

11 changes: 5 additions & 6 deletions stdlib/asyncio/protocols.pyi
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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: ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Any here makes sense to me, as this is hard to type correctly, but we should just use it directly than obscuring it by going through the _RetAddress alias:

Suggested change
def datagram_received(self, data: bytes, addr: _RetAddress) -> 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.
def datagram_received(self, data: bytes, addr: Any) -> None: ...

def error_received(self, exc: Exception) -> None: ...

class SubprocessProtocol(BaseProtocol):
Expand Down
Loading