Skip to content

Allow IPv6 datagram protocol addresses#15891

Open
popsiclelmlm wants to merge 1 commit into
python:mainfrom
popsiclelmlm:codex/asyncio-datagram-addr
Open

Allow IPv6 datagram protocol addresses#15891
popsiclelmlm wants to merge 1 commit into
python:mainfrom
popsiclelmlm:codex/asyncio-datagram-addr

Conversation

@popsiclelmlm

Copy link
Copy Markdown
Contributor

Summary

  • Widen asyncio.DatagramProtocol.datagram_received's addr parameter to use socket._RetAddress.
  • Add regression coverage for IPv4 and IPv6 datagram protocol overrides.

Reproduction

Root cause

  • DatagramProtocol.datagram_received used a two-item tuple type as a practical approximation even though datagram addresses vary by socket family.
  • socket.recvfrom already models returned addresses as _RetAddress, which is broad enough for IPv4, IPv6, and non-IP socket families.

Changes

  • Import socket._RetAddress in stdlib/asyncio/protocols.pyi.
  • Use _RetAddress for DatagramProtocol.datagram_received.
  • Add stdlib/@tests/test_cases/asyncio/check_datagram_protocol.py with IPv4 and IPv6 override examples.

Tests

  • PATH="$PWD/.venv/bin:$PATH" pre-commit run --files stdlib/asyncio/protocols.pyi stdlib/@tests/test_cases/asyncio/check_datagram_protocol.py
  • PATH="$PWD/.venv/bin:$PATH" .venv/bin/python tests/regr_test.py stdlib --platform darwin -p 3.10
  • PATH="$PWD/.venv/bin:$PATH" npx pyright@1.1.410 stdlib/@tests/test_cases --pythonversion 3.10 -p pyrightconfig.testcases.json
  • PATH="$PWD/.venv/bin:$PATH" npx pyright@1.1.410 stdlib/asyncio --pythonversion 3.10 -p pyrightconfig.stricter.json
  • git diff --check

Screenshots/Logs

  • Not applicable; stdlib stub/test-only change.
  • Note: tests/runtests.py stdlib/asyncio also ran locally. Its pyright/pre-commit phases passed, but stdlib stubtest failed on this local Python 3.14 environment due unrelated runtime availability/difference issues such as missing _tkinter, missing _curses.BUTTON5_*, and annotationlib differences.

AI disclosure

  • Prepared with assistance from Codex.

Fixes #15169

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Comment on lines +1 to +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: ...

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.

# 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: ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

asyncio: DatagramProtocol.datagram_received has incorrect annotation for addr parameter

2 participants