Skip to content

Commit 4fa81cc

Browse files
[asyncio] Use tagged union for getaddrinfo() (#16010)
1 parent af0c7e6 commit 4fa81cc

5 files changed

Lines changed: 44 additions & 6 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from __future__ import annotations
2+
3+
import asyncio
4+
from socket import AddressFamily
5+
from typing_extensions import assert_type
6+
7+
8+
async def check_getaddrinfo(loop: asyncio.AbstractEventLoop, base_loop: asyncio.BaseEventLoop) -> None:
9+
# The address family (item 0) is a tag that discriminates the sockaddr (item 4).
10+
for info in await loop.getaddrinfo("localhost", 80):
11+
if info[0] == AddressFamily.AF_INET:
12+
assert_type(info[4], "tuple[str, int]")
13+
elif info[0] == AddressFamily.AF_INET6:
14+
assert_type(info[4], "tuple[str, int, int, int] | tuple[int, bytes]")
15+
16+
for info in await base_loop.getaddrinfo("localhost", 80):
17+
if info[0] == AddressFamily.AF_INET:
18+
assert_type(info[4], "tuple[str, int]")
19+
elif info[0] == AddressFamily.AF_INET6:
20+
assert_type(info[4], "tuple[str, int, int, int] | tuple[int, bytes]")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from __future__ import annotations
2+
3+
import socket
4+
from typing_extensions import assert_type
5+
6+
7+
def check_getaddrinfo() -> None:
8+
# The address family (item 0) is a tag that discriminates the sockaddr (item 4).
9+
for info in socket.getaddrinfo("localhost", 80):
10+
if info[0] == socket.AddressFamily.AF_INET:
11+
assert_type(info[4], "tuple[str, int]")
12+
elif info[0] == socket.AddressFamily.AF_INET6:
13+
assert_type(info[4], "tuple[str, int, int, int] | tuple[int, bytes]")

stdlib/asyncio/base_events.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ from asyncio.transports import BaseTransport, DatagramTransport, ReadTransport,
1010
from collections.abc import Callable, Iterable, Sequence
1111
from concurrent.futures import Executor, ThreadPoolExecutor
1212
from contextvars import Context
13-
from socket import AddressFamily, AddressInfo, SocketKind, _Address, _RetAddress, socket
13+
from socket import AddressFamily, AddressInfo, _Address, _GetAddrInfoResult, _RetAddress, socket
1414
from typing import IO, Any, Literal, TypeAlias, TypeVar, overload
1515
from typing_extensions import TypeVarTuple, Unpack
1616

@@ -115,7 +115,7 @@ class BaseEventLoop(AbstractEventLoop):
115115
type: int = 0,
116116
proto: int = 0,
117117
flags: int = 0,
118-
) -> list[tuple[AddressFamily, SocketKind, int, str, tuple[str, int] | tuple[str, int, int, int] | tuple[int, bytes]]]: ...
118+
) -> _GetAddrInfoResult: ...
119119
async def getnameinfo(self, sockaddr: tuple[str, int] | tuple[str, int, int, int], flags: int = 0) -> tuple[str, str]: ...
120120

121121
if sys.version_info >= (3, 12):

stdlib/asyncio/events.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ from abc import ABCMeta, abstractmethod
1111
from collections.abc import Callable, Sequence
1212
from concurrent.futures import Executor
1313
from contextvars import Context
14-
from socket import AddressFamily, AddressInfo, SocketKind, _Address, _RetAddress, socket
14+
from socket import AddressFamily, AddressInfo, _Address, _GetAddrInfoResult, _RetAddress, socket
1515
from typing import IO, Any, Literal, Protocol, TypeAlias, TypeVar, overload, type_check_only
1616
from typing_extensions import Self, TypeVarTuple, Unpack, deprecated
1717

@@ -205,7 +205,7 @@ class AbstractEventLoop:
205205
type: int = 0,
206206
proto: int = 0,
207207
flags: int = 0,
208-
) -> list[tuple[AddressFamily, SocketKind, int, str, tuple[str, int] | tuple[str, int, int, int] | tuple[int, bytes]]]: ...
208+
) -> _GetAddrInfoResult: ...
209209
@abstractmethod
210210
async def getnameinfo(self, sockaddr: tuple[str, int] | tuple[str, int, int, int], flags: int = 0) -> tuple[str, str]: ...
211211

stdlib/socket.pyi

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ from _typeshed import ReadableBuffer, Unused, WriteableBuffer
137137
from collections.abc import Iterable
138138
from enum import IntEnum, IntFlag
139139
from io import BufferedReader, BufferedRWPair, BufferedWriter, IOBase, RawIOBase, TextIOWrapper
140-
from typing import Any, Final, Literal, Protocol, SupportsIndex, overload, type_check_only
140+
from typing import Any, Final, Literal, Protocol, SupportsIndex, TypeAlias, overload, type_check_only
141141
from typing_extensions import Self
142142

143143
__all__ = [
@@ -1576,6 +1576,11 @@ def create_server(
15761576
) -> socket: ...
15771577

15781578
# The 5th tuple item is the socket address, for IP4, IP6, or IP6 if Python is compiled with --disable-ipv6, respectively.
1579+
_GetAddrInfoResult: TypeAlias = list[
1580+
tuple[Literal[AddressFamily.AF_INET], SocketKind, int, str, tuple[str, int]]
1581+
| tuple[Literal[AddressFamily.AF_INET6], SocketKind, int, str, tuple[str, int, int, int] | tuple[int, bytes]]
1582+
]
1583+
15791584
def getaddrinfo(
15801585
host: bytes | str | None, port: bytes | str | int | None, family: int = 0, type: int = 0, proto: int = 0, flags: int = 0
1581-
) -> list[tuple[AddressFamily, SocketKind, int, str, tuple[str, int] | tuple[str, int, int, int] | tuple[int, bytes]]]: ...
1586+
) -> _GetAddrInfoResult: ...

0 commit comments

Comments
 (0)