Skip to content

Commit

Permalink
Add partial types to GAIResolver and associated interfaces.
Browse files Browse the repository at this point in the history
This corrects the type hints on IHostnameResolver.resolveHostName:

* addressTypes is a sequence of IAddress types, not IAddress instances.
* The return value should be IHostResolution, not IResolutionReceiver.

Ports matrix-org/synapse#11328.
  • Loading branch information
clokep committed Jul 26, 2022
1 parent b7b044f commit 885f446
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
61 changes: 50 additions & 11 deletions src/twisted/internet/_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,22 @@
AF_UNSPEC,
SOCK_DGRAM,
SOCK_STREAM,
AddressFamily,
SocketKind,
gaierror,
getaddrinfo,
)
from typing import (
TYPE_CHECKING,
Callable,
List,
NoReturn,
Optional,
Sequence,
Tuple,
Type,
Union,
)

from zope.interface import implementer

Expand All @@ -26,29 +39,39 @@
from twisted.internet.defer import Deferred
from twisted.internet.error import DNSLookupError
from twisted.internet.interfaces import (
IAddress,
IHostnameResolver,
IHostResolution,
IReactorThreads,
IResolutionReceiver,
IResolverSimple,
)
from twisted.internet.threads import deferToThreadPool
from twisted.logger import Logger
from twisted.python.compat import nativeString

if TYPE_CHECKING:
from twisted.python.runtime import platform

if platform.supportsThreads():
from twisted.python.threadpool import ThreadPool
else:
ThreadPool = object # type: ignore[misc, assignment]


@implementer(IHostResolution)
class HostResolution:
"""
The in-progress resolution of a given hostname.
"""

def __init__(self, name):
def __init__(self, name: str):
"""
Create a L{HostResolution} with the given name.
"""
self.name = name

def cancel(self):
def cancel(self) -> NoReturn:
# IHostResolution.cancel
raise NotImplementedError()

Expand Down Expand Up @@ -77,14 +100,30 @@ def cancel(self):
}


_GETADDRINFO_RESULT = List[
Tuple[
AddressFamily,
SocketKind,
int,
str,
Union[Tuple[str, int], Tuple[str, int, int, int]],
]
]


@implementer(IHostnameResolver)
class GAIResolver:
"""
L{IHostnameResolver} implementation that resolves hostnames by calling
L{getaddrinfo} in a thread.
"""

def __init__(self, reactor, getThreadPool=None, getaddrinfo=getaddrinfo):
def __init__(
self,
reactor: IReactorThreads,
getThreadPool: Optional[Callable[[], "ThreadPool"]] = None,
getaddrinfo: Callable[[str, int, int, int], _GETADDRINFO_RESULT] = getaddrinfo,
):
"""
Create a L{GAIResolver}.
Expand All @@ -109,12 +148,12 @@ def __init__(self, reactor, getThreadPool=None, getaddrinfo=getaddrinfo):

def resolveHostName(
self,
resolutionReceiver,
hostName,
portNumber=0,
addressTypes=None,
transportSemantics="TCP",
):
resolutionReceiver: IResolutionReceiver,
hostName: str,
portNumber: int = 0,
addressTypes: Optional[Sequence[Type[IAddress]]] = None,
transportSemantics: str = "TCP",
) -> IHostResolution:
"""
See L{IHostnameResolver.resolveHostName}
Expand All @@ -136,7 +175,7 @@ def resolveHostName(
]
socketType = _transportToSocket[transportSemantics]

def get():
def get() -> _GETADDRINFO_RESULT:
try:
return self._getaddrinfo(
hostName, portNumber, addressFamily, socketType
Expand All @@ -149,7 +188,7 @@ def get():
resolutionReceiver.resolutionBegan(resolution)

@d.addCallback
def deliverResults(result):
def deliverResults(result: _GETADDRINFO_RESULT) -> None:
for family, socktype, proto, cannoname, sockaddr in result:
addrType = _afToType[family]
resolutionReceiver.addressResolved(
Expand Down
5 changes: 3 additions & 2 deletions src/twisted/internet/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Optional,
Sequence,
Tuple,
Type,
Union,
)

Expand Down Expand Up @@ -190,9 +191,9 @@ def resolveHostName(
resolutionReceiver: IResolutionReceiver,
hostName: str,
portNumber: int,
addressTypes: Sequence[IAddress],
addressTypes: Sequence[Type[IAddress]],
transportSemantics: str,
) -> IResolutionReceiver:
) -> IHostResolution:
"""
Initiate a hostname resolution.
Expand Down

0 comments on commit 885f446

Please sign in to comment.