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

Properly typecheck types.http #14988

Merged
merged 22 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ exclude = (?x)
|tests/appservice/test_scheduler.py
|tests/federation/test_federation_catch_up.py
|tests/federation/test_federation_sender.py
|tests/http/federation/test_matrix_federation_agent.py
|tests/http/federation/test_srv_resolver.py
|tests/http/test_proxyagent.py
|tests/module_api/test_api.py
|tests/rest/media/v1/test_media_storage.py
|tests/server.py
Expand Down
5 changes: 4 additions & 1 deletion synapse/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
IAddress,
IDelayedCall,
IHostResolution,
IReactorCore,
IReactorPluggableNameResolver,
IReactorTime,
IResolutionReceiver,
Expand Down Expand Up @@ -226,7 +227,9 @@ def resolutionComplete() -> None:
return recv


@implementer(ISynapseReactor)
# ISynapseReactor implies IReactorCore, but explicitly marking it this as an implementer
# of IReactorCore seems to keep mypy-zope happier.
@implementer(IReactorCore, ISynapseReactor)
Comment on lines +230 to +232
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's strange that we have to do this, but oh well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

For completeness, without this change (and the similar one for ThreadedMemoryReactorClock I get:

$ mypy
tests/http/test_proxyagent.py:689: error: Argument 1 to "ProxyAgent" has incompatible type "BlacklistingReactorWrapper"; expected "IReactorCore"  [arg-type]
tests/http/test_proxyagent.py:690: error: Argument 1 to "BlacklistingReactorWrapper" has incompatible type "ThreadedMemoryReactorClock"; expected "IReactorPluggableNameResolver"  [arg-type]
tests/http/test_proxyagent.py:735: error: Argument 1 to "ProxyAgent" has incompatible type "BlacklistingReactorWrapper"; expected "IReactorCore"  [arg-type]
tests/http/test_proxyagent.py:736: error: Argument 1 to "BlacklistingReactorWrapper" has incompatible type "ThreadedMemoryReactorClock"; expected "IReactorPluggableNameResolver"  [arg-type]
Found 4 errors in 1 file (checked 775 source files)

which is unfortunate.

class BlacklistingReactorWrapper:
"""
A Reactor wrapper which will prevent DNS resolution to blacklisted IP
Expand Down
3 changes: 1 addition & 2 deletions synapse/http/proxyagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

from synapse.http import redact_uri
from synapse.http.connectproxyclient import HTTPConnectProxyEndpoint, ProxyCredentials
from synapse.types import ISynapseReactor

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -84,7 +83,7 @@ class ProxyAgent(_AgentBase):
def __init__(
self,
reactor: IReactorCore,
proxy_reactor: Optional[ISynapseReactor] = None,
proxy_reactor: Optional[IReactorCore] = None,
contextFactory: Optional[IPolicyForHTTPS] = None,
connectTimeout: Optional[float] = None,
bindAddress: Optional[bytes] = None,
Expand Down
28 changes: 21 additions & 7 deletions tests/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@

from OpenSSL import SSL
from OpenSSL.SSL import Connection
from twisted.internet.interfaces import IOpenSSLServerConnectionCreator
from twisted.internet.interfaces import IAddress, IOpenSSLServerConnectionCreator
from twisted.internet.ssl import Certificate, trustRootFromCertificates
from twisted.protocols.tls import TLSMemoryBIOProtocol
from twisted.web.client import BrowserLikePolicyForHTTPS # noqa: F401
from twisted.web.iweb import IPolicyForHTTPS # noqa: F401


def get_test_https_policy():
def get_test_https_policy() -> BrowserLikePolicyForHTTPS:
"""Get a test IPolicyForHTTPS which trusts the test CA cert

Returns:
Expand All @@ -39,7 +40,7 @@ def get_test_https_policy():
return BrowserLikePolicyForHTTPS(trustRoot=trust_root)


def get_test_ca_cert_file():
def get_test_ca_cert_file() -> str:
"""Get the path to the test CA cert

The keypair is generated with:
Expand All @@ -51,7 +52,7 @@ def get_test_ca_cert_file():
return os.path.join(os.path.dirname(__file__), "ca.crt")


def get_test_key_file():
def get_test_key_file() -> str:
"""get the path to the test key

The key file is made with:
Expand Down Expand Up @@ -137,15 +138,28 @@ class TestServerTLSConnectionFactory:
"""An SSL connection creator which returns connections which present a certificate
signed by our test CA."""

def __init__(self, sanlist):
def __init__(self, sanlist: List[bytes]):
"""
Args:
sanlist: list[bytes]: a list of subjectAltName values for the cert
sanlist: a list of subjectAltName values for the cert
"""
self._cert_file = create_test_cert_file(sanlist)

def serverConnectionForTLS(self, tlsProtocol):
def serverConnectionForTLS(self, tlsProtocol: TLSMemoryBIOProtocol) -> Connection:
ctx = SSL.Context(SSL.SSLv23_METHOD)
ctx.use_certificate_file(self._cert_file)
ctx.use_privatekey_file(get_test_key_file())
return Connection(ctx, None)


@implementer(IAddress)
class DummyAddress:
"""Dummy stand-in for an IAddress.

Placates mypy. Useful for tests that don't care about addresses, but
about other HTTP machinery."""

pass


dummy_address = DummyAddress()
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
Loading