Skip to content

Commit

Permalink
Fix mypy errors on Twisted 24.11.0 (#17998)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
anoadragon453 authored Dec 18, 2024
1 parent 09f377f commit 3eb9236
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions changelog.d/17998.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix various type errors across the codebase.
7 changes: 4 additions & 3 deletions synapse/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from netaddr import AddrFormatError, IPAddress, IPSet
from prometheus_client import Counter
from typing_extensions import Protocol
from zope.interface import implementer, provider
from zope.interface import implementer

from OpenSSL import SSL
from OpenSSL.SSL import VERIFY_NONE
Expand Down Expand Up @@ -225,7 +225,7 @@ def _callback() -> None:
recv.addressResolved(address)
recv.resolutionComplete()

@provider(IResolutionReceiver)
@implementer(IResolutionReceiver)
class EndpointReceiver:
@staticmethod
def resolutionBegan(resolutionInProgress: IHostResolution) -> None:
Expand All @@ -239,8 +239,9 @@ def addressResolved(address: IAddress) -> None:
def resolutionComplete() -> None:
_callback()

endpoint_receiver_wrapper = EndpointReceiver()
self._reactor.nameResolver.resolveHostName(
EndpointReceiver, hostname, portNumber=portNumber
endpoint_receiver_wrapper, hostname, portNumber=portNumber
)

return recv
Expand Down
13 changes: 9 additions & 4 deletions synapse/http/proxyagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import logging
import random
import re
from typing import Any, Collection, Dict, List, Optional, Sequence, Tuple
from typing import Any, Collection, Dict, List, Optional, Sequence, Tuple, Union
from urllib.parse import urlparse
from urllib.request import ( # type: ignore[attr-defined]
getproxies_environment,
Expand Down Expand Up @@ -351,7 +351,9 @@ def http_proxy_endpoint(
proxy: Optional[bytes],
reactor: IReactorCore,
tls_options_factory: Optional[IPolicyForHTTPS],
**kwargs: object,
timeout: float = 30,
bindAddress: Optional[Union[bytes, str, tuple[Union[bytes, str], int]]] = None,
attemptDelay: Optional[float] = None,
) -> Tuple[Optional[IStreamClientEndpoint], Optional[ProxyCredentials]]:
"""Parses an http proxy setting and returns an endpoint for the proxy
Expand Down Expand Up @@ -382,12 +384,15 @@ def http_proxy_endpoint(
# 3.9+) on scheme-less proxies, e.g. host:port.
scheme, host, port, credentials = parse_proxy(proxy)

proxy_endpoint = HostnameEndpoint(reactor, host, port, **kwargs)
proxy_endpoint = HostnameEndpoint(
reactor, host, port, timeout, bindAddress, attemptDelay
)

if scheme == b"https":
if tls_options_factory:
tls_options = tls_options_factory.creatorForNetloc(host, port)
proxy_endpoint = wrapClientTLS(tls_options, proxy_endpoint)
wrapped_proxy_endpoint = wrapClientTLS(tls_options, proxy_endpoint)
return wrapped_proxy_endpoint, credentials
else:
raise RuntimeError(
f"No TLS options for a https connection via proxy {proxy!s}"
Expand Down
4 changes: 3 additions & 1 deletion synapse/http/replicationagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ def endpointForURI(self, uri: URI) -> IStreamClientEndpoint:
location_config.port,
)
if scheme == "https":
endpoint = wrapClientTLS(
wrapped_endpoint = wrapClientTLS(
# The 'port' argument below isn't actually used by the function
self.context_factory.creatorForNetloc(
location_config.host.encode("utf-8"),
location_config.port,
),
endpoint,
)
return wrapped_endpoint

return endpoint
elif isinstance(location_config, InstanceUnixLocationConfig):
return UNIXClientEndpoint(self.reactor, location_config.path)
Expand Down
6 changes: 3 additions & 3 deletions tests/http/test_proxyagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ def test_https_request_via_uppercase_proxy_with_blocklist(self) -> None:
def test_proxy_with_no_scheme(self) -> None:
http_proxy_agent = ProxyAgent(self.reactor, use_proxy=True)
proxy_ep = checked_cast(HostnameEndpoint, http_proxy_agent.http_proxy_endpoint)
self.assertEqual(proxy_ep._hostStr, "proxy.com")
self.assertEqual(proxy_ep._hostText, "proxy.com")
self.assertEqual(proxy_ep._port, 8888)

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

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


Expand Down

0 comments on commit 3eb9236

Please sign in to comment.