Skip to content

Commit

Permalink
expressions are more idiomatic usage of Deferreds
Browse files Browse the repository at this point in the history
if you use bound names, you get incorrect mypy type information on the
intermediate states
  • Loading branch information
glyph committed Nov 22, 2022
1 parent 889ce41 commit 14be982
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/twisted/internet/_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
from twisted.internet.threads import deferToThreadPool
from twisted.logger import Logger
from twisted.python.compat import nativeString
from twisted.python.failure import Failure

if TYPE_CHECKING:
from twisted.python.threadpool import ThreadPool
Expand Down Expand Up @@ -247,26 +246,25 @@ def resolveHostName(

resolution = HostResolution(hostName)
resolutionReceiver.resolutionBegan(resolution)
onAddress = self._simpleResolver.getHostByName(hostName)

def addressReceived(address: str) -> None:
resolutionReceiver.addressResolved(IPv4Address("TCP", address, portNumber))

def errorReceived(error: Failure) -> None:
if not error.check(DNSLookupError):
self._log.failure(
(
self._simpleResolver.getHostByName(hostName)
.addCallback(
lambda address: resolutionReceiver.addressResolved(
IPv4Address("TCP", address, portNumber)
)
)
.addErrback(
lambda error: None
if error.check(DNSLookupError)
else self._log.failure(
"while looking up {name} with {resolver}",
error,
name=hostName,
resolver=self._simpleResolver,
)

onAddress.addCallbacks(addressReceived, errorReceived)

def finish(result: None) -> None:
resolutionReceiver.resolutionComplete()

onAddress.addCallback(finish)
)
.addCallback(lambda nothing: resolutionReceiver.resolutionComplete())
)
return resolution


Expand Down

0 comments on commit 14be982

Please sign in to comment.