Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Pass str to twisted's IReactorTCP (#10895)
Browse files Browse the repository at this point in the history
This follows a correction made in twisted/twisted#1664 and should fix our Twisted Trial CI job.

Until that change is in a twisted release, we'll have to ignore the type
of the `host` argument. I've raised #10899 to remind us to review the
issue in a few months' time.
  • Loading branch information
David Robertson authored Sep 30, 2021
1 parent 3aefc7b commit 2936414
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions changelog.d/10895.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix type hints to be compatible with an upcoming change to Twisted.
9 changes: 7 additions & 2 deletions synapse/handlers/send_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,13 @@ def build_sender_factory(**kwargs: Any) -> ESMTPSenderFactory:
# set to enable TLS.
factory = build_sender_factory(hostname=smtphost if enable_tls else None)

# the IReactorTCP interface claims host has to be a bytes, which seems to be wrong
reactor.connectTCP(smtphost, smtpport, factory, timeout=30, bindAddress=None) # type: ignore[arg-type]
reactor.connectTCP(
smtphost, # type: ignore[arg-type]
smtpport,
factory,
timeout=30,
bindAddress=None,
)

await make_deferred_yieldable(d)

Expand Down
8 changes: 6 additions & 2 deletions synapse/replication/tcp/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def start_replication(self, hs):
hs, outbound_redis_connection
)
hs.get_reactor().connectTCP(
hs.config.redis.redis_host.encode(),
hs.config.redis.redis_host, # type: ignore[arg-type]
hs.config.redis.redis_port,
self._factory,
)
Expand All @@ -324,7 +324,11 @@ def start_replication(self, hs):
self._factory = DirectTcpReplicationClientFactory(hs, client_name, self)
host = hs.config.worker.worker_replication_host
port = hs.config.worker.worker_replication_port
hs.get_reactor().connectTCP(host.encode(), port, self._factory)
hs.get_reactor().connectTCP(
host, # type: ignore[arg-type]
port,
self._factory,
)

def get_streams(self) -> Dict[str, Stream]:
"""Get a map from stream name to all streams."""
Expand Down
8 changes: 7 additions & 1 deletion synapse/replication/tcp/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,12 @@ def lazyConnection(
factory.continueTrying = reconnect

reactor = hs.get_reactor()
reactor.connectTCP(host.encode(), port, factory, timeout=30, bindAddress=None)
reactor.connectTCP(
host, # type: ignore[arg-type]
port,
factory,
timeout=30,
bindAddress=None,
)

return factory.handler
4 changes: 2 additions & 2 deletions tests/replication/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def setUp(self):
if self.hs.config.redis.redis_enabled:
# Handle attempts to connect to fake redis server.
self.reactor.add_tcp_client_callback(
b"localhost",
"localhost",
6379,
self.connect_any_redis_attempts,
)
Expand Down Expand Up @@ -424,7 +424,7 @@ def connect_any_redis_attempts(self):
clients = self.reactor.tcpClients
while clients:
(host, port, client_factory, _timeout, _bindAddress) = clients.pop(0)
self.assertEqual(host, b"localhost")
self.assertEqual(host, "localhost")
self.assertEqual(port, 6379)

client_protocol = client_factory.buildProtocol(None)
Expand Down
8 changes: 4 additions & 4 deletions tests/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class ThreadedMemoryReactorClock(MemoryReactorClock):
def __init__(self):
self.threadpool = ThreadPool(self)

self._tcp_callbacks = {}
self._tcp_callbacks: Dict[Tuple[str, int], Callable] = {}
self._udp = []
self.lookups: Dict[str, str] = {}
self._thread_callbacks: Deque[Callable[[], None]] = deque()
Expand Down Expand Up @@ -355,7 +355,7 @@ def callFromThread(self, callback, *args, **kwargs):
def getThreadPool(self):
return self.threadpool

def add_tcp_client_callback(self, host, port, callback):
def add_tcp_client_callback(self, host: str, port: int, callback: Callable):
"""Add a callback that will be invoked when we receive a connection
attempt to the given IP/port using `connectTCP`.
Expand All @@ -364,7 +364,7 @@ def add_tcp_client_callback(self, host, port, callback):
"""
self._tcp_callbacks[(host, port)] = callback

def connectTCP(self, host, port, factory, timeout=30, bindAddress=None):
def connectTCP(self, host: str, port: int, factory, timeout=30, bindAddress=None):
"""Fake L{IReactorTCP.connectTCP}."""

conn = super().connectTCP(
Expand Down Expand Up @@ -475,7 +475,7 @@ def runInteraction(interaction, *args, **kwargs):
return server


def get_clock():
def get_clock() -> Tuple[ThreadedMemoryReactorClock, Clock]:
clock = ThreadedMemoryReactorClock()
hs_clock = Clock(clock)
return clock, hs_clock
Expand Down

0 comments on commit 2936414

Please sign in to comment.