Allow IPv6 datagram protocol addresses#15891
Open
popsiclelmlm wants to merge 1 commit into
Open
Conversation
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
srittau
requested changes
Jun 11, 2026
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: ... |
Collaborator
There was a problem hiding this comment.
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: ... |
Collaborator
There was a problem hiding this comment.
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: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
asyncio.DatagramProtocol.datagram_received'saddrparameter to usesocket._RetAddress.Reproduction
DatagramProtocol.datagram_receivedhas incorrect annotation foraddrparameter #15169 reports that IPv6 datagram endpoints can pass(host, port, flowinfo, scope_id), but the stub only allowed a two-item address tuple.Root cause
DatagramProtocol.datagram_receivedused a two-item tuple type as a practical approximation even though datagram addresses vary by socket family.socket.recvfromalready models returned addresses as_RetAddress, which is broad enough for IPv4, IPv6, and non-IP socket families.Changes
socket._RetAddressinstdlib/asyncio/protocols.pyi._RetAddressforDatagramProtocol.datagram_received.stdlib/@tests/test_cases/asyncio/check_datagram_protocol.pywith 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.pyPATH="$PWD/.venv/bin:$PATH" .venv/bin/python tests/regr_test.py stdlib --platform darwin -p 3.10PATH="$PWD/.venv/bin:$PATH" npx pyright@1.1.410 stdlib/@tests/test_cases --pythonversion 3.10 -p pyrightconfig.testcases.jsonPATH="$PWD/.venv/bin:$PATH" npx pyright@1.1.410 stdlib/asyncio --pythonversion 3.10 -p pyrightconfig.stricter.jsongit diff --checkScreenshots/Logs
tests/runtests.py stdlib/asyncioalso 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
Fixes #15169