Skip to content

Commit

Permalink
Fixed some type annotation inconsistencies
Browse files Browse the repository at this point in the history
* AsyncBackend.getaddrinfo() may return 4-tuple socket addresses for IPv6, unlike anyio.getaddrinfo()
* The CancelScope facade for trio may in fact return a bool
* Removed type ignore comments for getaddrinfo() and getnameinfo() in the trio backend
  • Loading branch information
agronholm committed Apr 10, 2023
1 parent 38c75b4 commit 8364537
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
15 changes: 11 additions & 4 deletions src/anyio/_backends/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
EndOfStream,
WouldBlock,
)
from .._core._sockets import GetAddrInfoReturnType, convert_ipv6_sockaddr
from .._core._sockets import convert_ipv6_sockaddr
from .._core._streams import create_memory_object_stream
from .._core._synchronization import CapacityLimiter as BaseCapacityLimiter
from .._core._synchronization import Event as BaseEvent
Expand Down Expand Up @@ -2211,11 +2211,18 @@ async def getaddrinfo(
type: int | SocketKind = 0,
proto: int = 0,
flags: int = 0,
) -> GetAddrInfoReturnType:
result = await get_running_loop().getaddrinfo(
) -> list[
tuple[
AddressFamily,
SocketKind,
int,
str,
tuple[str, int] | tuple[str, int, int, int],
]
]:
return await get_running_loop().getaddrinfo(
host, port, family=family, type=type, proto=proto, flags=flags
)
return cast(GetAddrInfoReturnType, result)

@classmethod
async def getnameinfo(
Expand Down
25 changes: 16 additions & 9 deletions src/anyio/_backends/_trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
ClosedResourceError,
EndOfStream,
)
from .._core._sockets import GetAddrInfoReturnType, convert_ipv6_sockaddr
from .._core._sockets import convert_ipv6_sockaddr
from .._core._streams import create_memory_object_stream
from .._core._synchronization import CapacityLimiter as BaseCapacityLimiter
from .._core._synchronization import Event as BaseEvent
Expand Down Expand Up @@ -96,8 +96,11 @@ def __exit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
return self.__original.__exit__(exc_type, exc_val, exc_tb)
) -> bool | None:
# https://github.com/python-trio/trio-typing/pull/79
return self.__original.__exit__( # type: ignore[func-returns-value]
exc_type, exc_val, exc_tb
)

def cancel(self) -> None:
self.__original.cancel()
Expand Down Expand Up @@ -1007,17 +1010,21 @@ async def getaddrinfo(
type: int | SocketKind = 0,
proto: int = 0,
flags: int = 0,
) -> GetAddrInfoReturnType:
# https://github.com/python-trio/trio-typing/pull/57
return await trio.socket.getaddrinfo( # type: ignore[return-value]
host, port, family, type, proto, flags
)
) -> list[
tuple[
AddressFamily,
SocketKind,
int,
str,
tuple[str, int] | tuple[str, int, int, int],
]
]:
return await trio.socket.getaddrinfo(host, port, family, type, proto, flags)

@classmethod
async def getnameinfo(
cls, sockaddr: IPSockAddrType, flags: int = 0
) -> tuple[str, str]:
# https://github.com/python-trio/trio-typing/pull/56
return await trio.socket.getnameinfo(sockaddr, flags)

@classmethod
Expand Down
7 changes: 2 additions & 5 deletions src/anyio/_core/_sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from os import PathLike, chmod
from pathlib import Path
from socket import AddressFamily, SocketKind
from typing import List, Tuple, cast, overload
from typing import Tuple, cast, overload

from .. import to_thread
from ..abc import (
Expand Down Expand Up @@ -39,9 +39,6 @@

IPPROTO_IPV6 = getattr(socket, "IPPROTO_IPV6", 41) # https://bugs.python.org/issue29515

GetAddrInfoReturnType = List[
Tuple[AddressFamily, SocketKind, int, str, Tuple[str, int]]
]
AnyIPAddressFamily = Literal[
AddressFamily.AF_UNSPEC, AddressFamily.AF_INET, AddressFamily.AF_INET6
]
Expand Down Expand Up @@ -529,7 +526,7 @@ async def getaddrinfo(
type: int | SocketKind = 0,
proto: int = 0,
flags: int = 0,
) -> GetAddrInfoReturnType:
) -> list[tuple[AddressFamily, SocketKind, int, str, tuple[str, int]]]:
"""
Look up a numeric IP address given a host name.
Expand Down
11 changes: 9 additions & 2 deletions src/anyio/abc/_eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
if TYPE_CHECKING:
from typing import Literal

from .._core._sockets import GetAddrInfoReturnType
from .._core._synchronization import CapacityLimiter, Event
from .._core._tasks import CancelScope
from .._core._testing import TaskInfo
Expand Down Expand Up @@ -317,7 +316,15 @@ async def getaddrinfo(
type: int | SocketKind = 0,
proto: int = 0,
flags: int = 0,
) -> GetAddrInfoReturnType:
) -> list[
tuple[
AddressFamily,
SocketKind,
int,
str,
tuple[str, int] | tuple[str, int, int, int],
]
]:
pass

@classmethod
Expand Down

0 comments on commit 8364537

Please sign in to comment.