Skip to content

Commit 3eb9236

Browse files
Fix mypy errors on Twisted 24.11.0 (#17998)
Fixes various `mypy` errors associated with Twisted `24.11.0`. Hopefully addresses #17075, though I've yet to test against `trunk`. Changes should be compatible with our currently pinned Twisted version of `24.7.0`.
1 parent 09f377f commit 3eb9236

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

changelog.d/17998.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix various type errors across the codebase.

synapse/http/client.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from netaddr import AddrFormatError, IPAddress, IPSet
4242
from prometheus_client import Counter
4343
from typing_extensions import Protocol
44-
from zope.interface import implementer, provider
44+
from zope.interface import implementer
4545

4646
from OpenSSL import SSL
4747
from OpenSSL.SSL import VERIFY_NONE
@@ -225,7 +225,7 @@ def _callback() -> None:
225225
recv.addressResolved(address)
226226
recv.resolutionComplete()
227227

228-
@provider(IResolutionReceiver)
228+
@implementer(IResolutionReceiver)
229229
class EndpointReceiver:
230230
@staticmethod
231231
def resolutionBegan(resolutionInProgress: IHostResolution) -> None:
@@ -239,8 +239,9 @@ def addressResolved(address: IAddress) -> None:
239239
def resolutionComplete() -> None:
240240
_callback()
241241

242+
endpoint_receiver_wrapper = EndpointReceiver()
242243
self._reactor.nameResolver.resolveHostName(
243-
EndpointReceiver, hostname, portNumber=portNumber
244+
endpoint_receiver_wrapper, hostname, portNumber=portNumber
244245
)
245246

246247
return recv

synapse/http/proxyagent.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import logging
2222
import random
2323
import re
24-
from typing import Any, Collection, Dict, List, Optional, Sequence, Tuple
24+
from typing import Any, Collection, Dict, List, Optional, Sequence, Tuple, Union
2525
from urllib.parse import urlparse
2626
from urllib.request import ( # type: ignore[attr-defined]
2727
getproxies_environment,
@@ -351,7 +351,9 @@ def http_proxy_endpoint(
351351
proxy: Optional[bytes],
352352
reactor: IReactorCore,
353353
tls_options_factory: Optional[IPolicyForHTTPS],
354-
**kwargs: object,
354+
timeout: float = 30,
355+
bindAddress: Optional[Union[bytes, str, tuple[Union[bytes, str], int]]] = None,
356+
attemptDelay: Optional[float] = None,
355357
) -> Tuple[Optional[IStreamClientEndpoint], Optional[ProxyCredentials]]:
356358
"""Parses an http proxy setting and returns an endpoint for the proxy
357359
@@ -382,12 +384,15 @@ def http_proxy_endpoint(
382384
# 3.9+) on scheme-less proxies, e.g. host:port.
383385
scheme, host, port, credentials = parse_proxy(proxy)
384386

385-
proxy_endpoint = HostnameEndpoint(reactor, host, port, **kwargs)
387+
proxy_endpoint = HostnameEndpoint(
388+
reactor, host, port, timeout, bindAddress, attemptDelay
389+
)
386390

387391
if scheme == b"https":
388392
if tls_options_factory:
389393
tls_options = tls_options_factory.creatorForNetloc(host, port)
390-
proxy_endpoint = wrapClientTLS(tls_options, proxy_endpoint)
394+
wrapped_proxy_endpoint = wrapClientTLS(tls_options, proxy_endpoint)
395+
return wrapped_proxy_endpoint, credentials
391396
else:
392397
raise RuntimeError(
393398
f"No TLS options for a https connection via proxy {proxy!s}"

synapse/http/replicationagent.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,16 @@ def endpointForURI(self, uri: URI) -> IStreamClientEndpoint:
8989
location_config.port,
9090
)
9191
if scheme == "https":
92-
endpoint = wrapClientTLS(
92+
wrapped_endpoint = wrapClientTLS(
9393
# The 'port' argument below isn't actually used by the function
9494
self.context_factory.creatorForNetloc(
9595
location_config.host.encode("utf-8"),
9696
location_config.port,
9797
),
9898
endpoint,
9999
)
100+
return wrapped_endpoint
101+
100102
return endpoint
101103
elif isinstance(location_config, InstanceUnixLocationConfig):
102104
return UNIXClientEndpoint(self.reactor, location_config.path)

tests/http/test_proxyagent.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ def test_https_request_via_uppercase_proxy_with_blocklist(self) -> None:
854854
def test_proxy_with_no_scheme(self) -> None:
855855
http_proxy_agent = ProxyAgent(self.reactor, use_proxy=True)
856856
proxy_ep = checked_cast(HostnameEndpoint, http_proxy_agent.http_proxy_endpoint)
857-
self.assertEqual(proxy_ep._hostStr, "proxy.com")
857+
self.assertEqual(proxy_ep._hostText, "proxy.com")
858858
self.assertEqual(proxy_ep._port, 8888)
859859

860860
@patch.dict(os.environ, {"http_proxy": "socks://proxy.com:8888"})
@@ -866,14 +866,14 @@ def test_proxy_with_unsupported_scheme(self) -> None:
866866
def test_proxy_with_http_scheme(self) -> None:
867867
http_proxy_agent = ProxyAgent(self.reactor, use_proxy=True)
868868
proxy_ep = checked_cast(HostnameEndpoint, http_proxy_agent.http_proxy_endpoint)
869-
self.assertEqual(proxy_ep._hostStr, "proxy.com")
869+
self.assertEqual(proxy_ep._hostText, "proxy.com")
870870
self.assertEqual(proxy_ep._port, 8888)
871871

872872
@patch.dict(os.environ, {"http_proxy": "https://proxy.com:8888"})
873873
def test_proxy_with_https_scheme(self) -> None:
874874
https_proxy_agent = ProxyAgent(self.reactor, use_proxy=True)
875875
proxy_ep = checked_cast(_WrapperEndpoint, https_proxy_agent.http_proxy_endpoint)
876-
self.assertEqual(proxy_ep._wrappedEndpoint._hostStr, "proxy.com")
876+
self.assertEqual(proxy_ep._wrappedEndpoint._hostText, "proxy.com")
877877
self.assertEqual(proxy_ep._wrappedEndpoint._port, 8888)
878878

879879

0 commit comments

Comments
 (0)