Fix mypy errors on Twisted 24.11.0 (#17998)
Fixes various `mypy` errors associated with Twisted `24.11.0`. Hopefully addresses https://github.com/element-hq/synapse/issues/17075, though I've yet to test against `trunk`. Changes should be compatible with our currently pinned Twisted version of `24.7.0`.
This commit is contained in:
parent
09f377fa52
commit
3eb92369ca
|
@ -0,0 +1 @@
|
||||||
|
Fix various type errors across the codebase.
|
|
@ -41,7 +41,7 @@ from canonicaljson import encode_canonical_json
|
||||||
from netaddr import AddrFormatError, IPAddress, IPSet
|
from netaddr import AddrFormatError, IPAddress, IPSet
|
||||||
from prometheus_client import Counter
|
from prometheus_client import Counter
|
||||||
from typing_extensions import Protocol
|
from typing_extensions import Protocol
|
||||||
from zope.interface import implementer, provider
|
from zope.interface import implementer
|
||||||
|
|
||||||
from OpenSSL import SSL
|
from OpenSSL import SSL
|
||||||
from OpenSSL.SSL import VERIFY_NONE
|
from OpenSSL.SSL import VERIFY_NONE
|
||||||
|
@ -225,7 +225,7 @@ class _IPBlockingResolver:
|
||||||
recv.addressResolved(address)
|
recv.addressResolved(address)
|
||||||
recv.resolutionComplete()
|
recv.resolutionComplete()
|
||||||
|
|
||||||
@provider(IResolutionReceiver)
|
@implementer(IResolutionReceiver)
|
||||||
class EndpointReceiver:
|
class EndpointReceiver:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def resolutionBegan(resolutionInProgress: IHostResolution) -> None:
|
def resolutionBegan(resolutionInProgress: IHostResolution) -> None:
|
||||||
|
@ -239,8 +239,9 @@ class _IPBlockingResolver:
|
||||||
def resolutionComplete() -> None:
|
def resolutionComplete() -> None:
|
||||||
_callback()
|
_callback()
|
||||||
|
|
||||||
|
endpoint_receiver_wrapper = EndpointReceiver()
|
||||||
self._reactor.nameResolver.resolveHostName(
|
self._reactor.nameResolver.resolveHostName(
|
||||||
EndpointReceiver, hostname, portNumber=portNumber
|
endpoint_receiver_wrapper, hostname, portNumber=portNumber
|
||||||
)
|
)
|
||||||
|
|
||||||
return recv
|
return recv
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
import re
|
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.parse import urlparse
|
||||||
from urllib.request import ( # type: ignore[attr-defined]
|
from urllib.request import ( # type: ignore[attr-defined]
|
||||||
getproxies_environment,
|
getproxies_environment,
|
||||||
|
@ -351,7 +351,9 @@ def http_proxy_endpoint(
|
||||||
proxy: Optional[bytes],
|
proxy: Optional[bytes],
|
||||||
reactor: IReactorCore,
|
reactor: IReactorCore,
|
||||||
tls_options_factory: Optional[IPolicyForHTTPS],
|
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]]:
|
) -> Tuple[Optional[IStreamClientEndpoint], Optional[ProxyCredentials]]:
|
||||||
"""Parses an http proxy setting and returns an endpoint for the proxy
|
"""Parses an http proxy setting and returns an endpoint for the proxy
|
||||||
|
|
||||||
|
@ -382,12 +384,15 @@ def http_proxy_endpoint(
|
||||||
# 3.9+) on scheme-less proxies, e.g. host:port.
|
# 3.9+) on scheme-less proxies, e.g. host:port.
|
||||||
scheme, host, port, credentials = parse_proxy(proxy)
|
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 scheme == b"https":
|
||||||
if tls_options_factory:
|
if tls_options_factory:
|
||||||
tls_options = tls_options_factory.creatorForNetloc(host, port)
|
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:
|
else:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f"No TLS options for a https connection via proxy {proxy!s}"
|
f"No TLS options for a https connection via proxy {proxy!s}"
|
||||||
|
|
|
@ -89,7 +89,7 @@ class ReplicationEndpointFactory:
|
||||||
location_config.port,
|
location_config.port,
|
||||||
)
|
)
|
||||||
if scheme == "https":
|
if scheme == "https":
|
||||||
endpoint = wrapClientTLS(
|
wrapped_endpoint = wrapClientTLS(
|
||||||
# The 'port' argument below isn't actually used by the function
|
# The 'port' argument below isn't actually used by the function
|
||||||
self.context_factory.creatorForNetloc(
|
self.context_factory.creatorForNetloc(
|
||||||
location_config.host.encode("utf-8"),
|
location_config.host.encode("utf-8"),
|
||||||
|
@ -97,6 +97,8 @@ class ReplicationEndpointFactory:
|
||||||
),
|
),
|
||||||
endpoint,
|
endpoint,
|
||||||
)
|
)
|
||||||
|
return wrapped_endpoint
|
||||||
|
|
||||||
return endpoint
|
return endpoint
|
||||||
elif isinstance(location_config, InstanceUnixLocationConfig):
|
elif isinstance(location_config, InstanceUnixLocationConfig):
|
||||||
return UNIXClientEndpoint(self.reactor, location_config.path)
|
return UNIXClientEndpoint(self.reactor, location_config.path)
|
||||||
|
|
|
@ -854,7 +854,7 @@ class MatrixFederationAgentTests(TestCase):
|
||||||
def test_proxy_with_no_scheme(self) -> None:
|
def test_proxy_with_no_scheme(self) -> None:
|
||||||
http_proxy_agent = ProxyAgent(self.reactor, use_proxy=True)
|
http_proxy_agent = ProxyAgent(self.reactor, use_proxy=True)
|
||||||
proxy_ep = checked_cast(HostnameEndpoint, http_proxy_agent.http_proxy_endpoint)
|
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)
|
self.assertEqual(proxy_ep._port, 8888)
|
||||||
|
|
||||||
@patch.dict(os.environ, {"http_proxy": "socks://proxy.com:8888"})
|
@patch.dict(os.environ, {"http_proxy": "socks://proxy.com:8888"})
|
||||||
|
@ -866,14 +866,14 @@ class MatrixFederationAgentTests(TestCase):
|
||||||
def test_proxy_with_http_scheme(self) -> None:
|
def test_proxy_with_http_scheme(self) -> None:
|
||||||
http_proxy_agent = ProxyAgent(self.reactor, use_proxy=True)
|
http_proxy_agent = ProxyAgent(self.reactor, use_proxy=True)
|
||||||
proxy_ep = checked_cast(HostnameEndpoint, http_proxy_agent.http_proxy_endpoint)
|
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)
|
self.assertEqual(proxy_ep._port, 8888)
|
||||||
|
|
||||||
@patch.dict(os.environ, {"http_proxy": "https://proxy.com:8888"})
|
@patch.dict(os.environ, {"http_proxy": "https://proxy.com:8888"})
|
||||||
def test_proxy_with_https_scheme(self) -> None:
|
def test_proxy_with_https_scheme(self) -> None:
|
||||||
https_proxy_agent = ProxyAgent(self.reactor, use_proxy=True)
|
https_proxy_agent = ProxyAgent(self.reactor, use_proxy=True)
|
||||||
proxy_ep = checked_cast(_WrapperEndpoint, https_proxy_agent.http_proxy_endpoint)
|
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)
|
self.assertEqual(proxy_ep._wrappedEndpoint._port, 8888)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue