From a69ecc681460d5a0b43312667f0c302ca41f8793 Mon Sep 17 00:00:00 2001 From: Max Muoto Date: Sat, 27 Dec 2025 15:53:18 -0600 Subject: [PATCH 1/7] improve datagram received type --- stdlib/asyncio/protocols.pyi | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/stdlib/asyncio/protocols.pyi b/stdlib/asyncio/protocols.pyi index 3a8965f03e29..db1ce8e79f76 100644 --- a/stdlib/asyncio/protocols.pyi +++ b/stdlib/asyncio/protocols.pyi @@ -1,6 +1,5 @@ from _typeshed import ReadableBuffer from asyncio import transports -from typing import Any # Keep asyncio.__all__ updated with any changes to __all__ here __all__ = ("BaseProtocol", "Protocol", "DatagramProtocol", "SubprocessProtocol", "BufferedProtocol") @@ -27,11 +26,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]) -> None: ... + # Address is a 2-tuple (host, port) for IPv4, 4-tuple (host, port, flowinfo, scope_id) for IPv6, + # or tuple[int, int] for some unusual protocols like socket.AF_NETLINK. + def datagram_received(self, data: bytes, addr: tuple[str, int] | tuple[str, int, int, int] | tuple[int, int]) -> None: ... def error_received(self, exc: Exception) -> None: ... class SubprocessProtocol(BaseProtocol): From 51c7a2ba0cc07193a4d93729fd13bf2039251822 Mon Sep 17 00:00:00 2001 From: Max Muoto Date: Sat, 27 Dec 2025 15:57:37 -0600 Subject: [PATCH 2/7] simplify --- stdlib/asyncio/protocols.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/asyncio/protocols.pyi b/stdlib/asyncio/protocols.pyi index db1ce8e79f76..6be0caed84f6 100644 --- a/stdlib/asyncio/protocols.pyi +++ b/stdlib/asyncio/protocols.pyi @@ -28,7 +28,7 @@ class DatagramProtocol(BaseProtocol): def connection_made(self, transport: transports.DatagramTransport) -> None: ... # type: ignore[override] # Address is a 2-tuple (host, port) for IPv4, 4-tuple (host, port, flowinfo, scope_id) for IPv6, # or tuple[int, int] for some unusual protocols like socket.AF_NETLINK. - def datagram_received(self, data: bytes, addr: tuple[str, int] | tuple[str, int, int, int] | tuple[int, int]) -> None: ... + def datagram_received(self, data: bytes, addr: tuple[str | int, int] | tuple[str, int, int, int]) -> None: ... def error_received(self, exc: Exception) -> None: ... class SubprocessProtocol(BaseProtocol): From da11f181bf7689d19a24c7e3a0d9d95f08e9ea25 Mon Sep 17 00:00:00 2001 From: Max Muoto Date: Sun, 28 Dec 2025 09:37:22 -0600 Subject: [PATCH 3/7] fix + add tests --- .../@tests/test_cases/asyncio/check_protocols.py | 15 +++++++++++++++ stdlib/asyncio/protocols.pyi | 11 ++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) 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..d848420c191a --- /dev/null +++ b/stdlib/@tests/test_cases/asyncio/check_protocols.py @@ -0,0 +1,15 @@ +from __future__ import annotations + +from asyncio import DatagramProtocol + + +class IPv4Protocol(DatagramProtocol): + def datagram_received(self, data: bytes, addr: tuple[str, int]) -> None: ... + + +class IPv6Protocol(DatagramProtocol): + def datagram_received(self, data: bytes, addr: tuple[str, int, int, int]) -> None: ... + + +class NetlinkProtocol(DatagramProtocol): + def datagram_received(self, data: bytes, addr: tuple[int, int]) -> None: ... diff --git a/stdlib/asyncio/protocols.pyi b/stdlib/asyncio/protocols.pyi index 6be0caed84f6..c53a0220a806 100644 --- a/stdlib/asyncio/protocols.pyi +++ b/stdlib/asyncio/protocols.pyi @@ -1,5 +1,7 @@ from _typeshed import ReadableBuffer from asyncio import transports +from typing import Any +from typing_extensions import Unpack # Keep asyncio.__all__ updated with any changes to __all__ here __all__ = ("BaseProtocol", "Protocol", "DatagramProtocol", "SubprocessProtocol", "BufferedProtocol") @@ -26,9 +28,12 @@ class BufferedProtocol(BaseProtocol): class DatagramProtocol(BaseProtocol): __slots__ = () def connection_made(self, transport: transports.DatagramTransport) -> None: ... # type: ignore[override] - # Address is a 2-tuple (host, port) for IPv4, 4-tuple (host, port, flowinfo, scope_id) for IPv6, - # or tuple[int, int] for some unusual protocols like socket.AF_NETLINK. - def datagram_received(self, data: bytes, addr: tuple[str | int, int] | tuple[str, int, int, int]) -> None: ... + # addr can be a tuple[int, int] for some unusual protocols like socket.AF_NETLINK and + # a 4-tuple (host, port, flowinfo, scope_id) for IPv6, + # 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, Unpack[tuple[Any, ...]]]) -> None: ... def error_received(self, exc: Exception) -> None: ... class SubprocessProtocol(BaseProtocol): From 15dac128b8efbe6a5190c10de159c7fc993f6763 Mon Sep 17 00:00:00 2001 From: Max Muoto Date: Sun, 28 Dec 2025 09:43:56 -0600 Subject: [PATCH 4/7] tweak --- pyrightconfig.json | 9 +++------ pyrightconfig.testcases.json | 1 - stdlib/asyncio/protocols.pyi | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/pyrightconfig.json b/pyrightconfig.json index 74ddb65b85f6..89e756191098 100644 --- a/pyrightconfig.json +++ b/pyrightconfig.json @@ -1,17 +1,14 @@ { "$schema": "https://raw.githubusercontent.com/microsoft/pyright/main/packages/vscode-pyright/schemas/pyrightconfig.schema.json", "typeshedPath": ".", - "include": [ - "stdlib", - "stubs", - ], + "include": ["stdlib", "stubs"], "exclude": [ // Stubs that don't work in all Python versions "stubs/seaborn", "stubs/shapely", "stubs/geopandas", // test cases use a custom config file - "**/@tests/test_cases", + "**/@tests/test_cases" ], "typeCheckingMode": "strict", // Allowed in base settings for incomplete stubs, checked in stricter settings @@ -44,5 +41,5 @@ // The name of the self/cls parameter is out of typeshed's control. "reportSelfClsParameterName": "none", // Not actionable in typeshed - "reportDeprecated": "none", + "reportDeprecated": "none" } diff --git a/pyrightconfig.testcases.json b/pyrightconfig.testcases.json index ab1cb86b9900..b953dfd11e0e 100644 --- a/pyrightconfig.testcases.json +++ b/pyrightconfig.testcases.json @@ -1,5 +1,4 @@ { - "$schema": "https://raw.githubusercontent.com/microsoft/pyright/main/packages/vscode-pyright/schemas/pyrightconfig.schema.json", "typeshedPath": ".", "include": [ "**/@tests/test_cases", diff --git a/stdlib/asyncio/protocols.pyi b/stdlib/asyncio/protocols.pyi index c53a0220a806..8a9299f6ce04 100644 --- a/stdlib/asyncio/protocols.pyi +++ b/stdlib/asyncio/protocols.pyi @@ -33,7 +33,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, Unpack[tuple[Any, ...]]]) -> None: ... + def datagram_received(self, data: bytes, addr: tuple[Any, int, Unpack[tuple[Any, ...]]]) -> None: ... def error_received(self, exc: Exception) -> None: ... class SubprocessProtocol(BaseProtocol): From e47ca2cd678c6bb3d8109b8e4def687b1afed310 Mon Sep 17 00:00:00 2001 From: Max Muoto Date: Sun, 28 Dec 2025 09:45:51 -0600 Subject: [PATCH 5/7] revert --- pyrightconfig.json | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pyrightconfig.json b/pyrightconfig.json index 89e756191098..74ddb65b85f6 100644 --- a/pyrightconfig.json +++ b/pyrightconfig.json @@ -1,14 +1,17 @@ { "$schema": "https://raw.githubusercontent.com/microsoft/pyright/main/packages/vscode-pyright/schemas/pyrightconfig.schema.json", "typeshedPath": ".", - "include": ["stdlib", "stubs"], + "include": [ + "stdlib", + "stubs", + ], "exclude": [ // Stubs that don't work in all Python versions "stubs/seaborn", "stubs/shapely", "stubs/geopandas", // test cases use a custom config file - "**/@tests/test_cases" + "**/@tests/test_cases", ], "typeCheckingMode": "strict", // Allowed in base settings for incomplete stubs, checked in stricter settings @@ -41,5 +44,5 @@ // The name of the self/cls parameter is out of typeshed's control. "reportSelfClsParameterName": "none", // Not actionable in typeshed - "reportDeprecated": "none" + "reportDeprecated": "none", } From 0d469589cda178b86a5f6c6d7fe0d41d7bbeaf19 Mon Sep 17 00:00:00 2001 From: Max Muoto Date: Sun, 28 Dec 2025 09:46:44 -0600 Subject: [PATCH 6/7] revert --- pyrightconfig.testcases.json | 1 + 1 file changed, 1 insertion(+) diff --git a/pyrightconfig.testcases.json b/pyrightconfig.testcases.json index b953dfd11e0e..ab1cb86b9900 100644 --- a/pyrightconfig.testcases.json +++ b/pyrightconfig.testcases.json @@ -1,4 +1,5 @@ { + "$schema": "https://raw.githubusercontent.com/microsoft/pyright/main/packages/vscode-pyright/schemas/pyrightconfig.schema.json", "typeshedPath": ".", "include": [ "**/@tests/test_cases", From 4e4cafbbc1148d5d5208283189ba4d8e946c0b3f Mon Sep 17 00:00:00 2001 From: Max Muoto Date: Sun, 28 Dec 2025 09:51:01 -0600 Subject: [PATCH 7/7] tweak commment --- stdlib/asyncio/protocols.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/asyncio/protocols.pyi b/stdlib/asyncio/protocols.pyi index 8a9299f6ce04..b4dd3c7c767d 100644 --- a/stdlib/asyncio/protocols.pyi +++ b/stdlib/asyncio/protocols.pyi @@ -30,8 +30,8 @@ class DatagramProtocol(BaseProtocol): 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 and # a 4-tuple (host, port, flowinfo, scope_id) for IPv6, - # 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. + # Use tuple[Any, int, Unpack[tuple[Any, ...]]] to not cause typechecking issues on most usual cases. + # This could be improved by using tuple[AnyOf[str, int], int, Unpack[tuple[Any, ...]]] if the AnyOf feature is accepted. # See https://github.com/python/typing/issues/566 def datagram_received(self, data: bytes, addr: tuple[Any, int, Unpack[tuple[Any, ...]]]) -> None: ... def error_received(self, exc: Exception) -> None: ...