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

Commit

Permalink
type-hint HomeserverTestcase.setup_test_homeserver (#10961)
Browse files Browse the repository at this point in the history
* type-hint `HomeserverTestcase.setup_test_homeserver`

For better IDE completion. A small drive-by.
  • Loading branch information
David Robertson authored Oct 1, 2021
1 parent 7e44052 commit e46ac85
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelog.d/10961.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type-hint to `HomeserverTestcase.setup_test_homeserver`.
19 changes: 15 additions & 4 deletions tests/replication/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,16 @@ def prepare(self, reactor, clock, hs):
# databases objects are the same.
self.worker_hs.get_datastore().db_pool = hs.get_datastore().db_pool

# Normally we'd pass in the handler to `setup_test_homeserver`, which would
# eventually hit "Install @cache_in_self attributes" in tests/utils.py.
# Unfortunately our handler wants a reference to the homeserver. That leaves
# us with a chicken-and-egg problem.
# We can workaround this: create the homeserver first, create the handler
# and bodge it in after the fact. The bodging requires us to know the
# dirty details of how `cache_in_self` works. We politely ask mypy to
# ignore our dirty dealings.
self.test_handler = self._build_replication_data_handler()
self.worker_hs._replication_data_handler = self.test_handler
self.worker_hs._replication_data_handler = self.test_handler # type: ignore[attr-defined]

repl_handler = ReplicationCommandHandler(self.worker_hs)
self.client = ClientReplicationStreamProtocol(
Expand Down Expand Up @@ -315,12 +323,15 @@ def make_worker_hs(
)
)

# Copy the port into a new, non-Optional variable so mypy knows we're
# not going to reset `instance_loc` to `None` under its feet. See
# https://mypy.readthedocs.io/en/latest/common_issues.html#narrowing-and-inner-functions
port = instance_loc.port

self.reactor.add_tcp_client_callback(
self.reactor.lookups[instance_loc.host],
instance_loc.port,
lambda: self._handle_http_replication_attempt(
worker_hs, instance_loc.port
),
lambda: self._handle_http_replication_attempt(worker_hs, port),
)

store = worker_hs.get_datastore()
Expand Down
6 changes: 3 additions & 3 deletions tests/rest/client/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ class LoginRestServletTestCase(unittest.HomeserverTestCase):

def make_homeserver(self, reactor, clock):
self.hs = self.setup_test_homeserver()
self.hs.config.enable_registration = True
self.hs.config.registrations_require_3pid = []
self.hs.config.auto_join_rooms = []
self.hs.config.registration.enable_registration = True
self.hs.config.registration.registrations_require_3pid = []
self.hs.config.registration.auto_join_rooms = []
self.hs.config.captcha.enable_registration_captcha = False

return self.hs
Expand Down
4 changes: 2 additions & 2 deletions tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import logging
import secrets
import time
from typing import Callable, Dict, Iterable, Optional, Tuple, Type, TypeVar, Union
from typing import Any, Callable, Dict, Iterable, Optional, Tuple, Type, TypeVar, Union
from unittest.mock import Mock, patch

from canonicaljson import json
Expand Down Expand Up @@ -449,7 +449,7 @@ def make_request(
client_ip,
)

def setup_test_homeserver(self, *args, **kwargs):
def setup_test_homeserver(self, *args: Any, **kwargs: Any) -> HomeServer:
"""
Set up the test homeserver, meant to be called by the overridable
make_homeserver. It automatically passes through the test class's
Expand Down

0 comments on commit e46ac85

Please sign in to comment.