From 8b7d1872c52ddca0f397c108bda3841cfe914559 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Thu, 30 Sep 2021 19:07:26 +0100 Subject: [PATCH 1/4] type-hint `HomeserverTestcase.setup_test_homeserver` For better IDE completion. A small drive-by. --- tests/replication/_base.py | 10 +++++++++- tests/rest/client/test_login.py | 6 +++--- tests/unittest.py | 4 ++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/tests/replication/_base.py b/tests/replication/_base.py index c7555c26dbdd..c886b7aae4b0 100644 --- a/tests/replication/_base.py +++ b/tests/replication/_base.py @@ -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. + # Unfortunatly our handler wants a reference to the homserver. That leaves + # us with a chicken-and-egg problem. + # We can workaroudn 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( diff --git a/tests/rest/client/test_login.py b/tests/rest/client/test_login.py index 371615a015f3..7fd92c94e09d 100644 --- a/tests/rest/client/test_login.py +++ b/tests/rest/client/test_login.py @@ -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 diff --git a/tests/unittest.py b/tests/unittest.py index 5f93ebf1479a..0807467e3943 100644 --- a/tests/unittest.py +++ b/tests/unittest.py @@ -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 @@ -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 From cbbd8e8d18c1ebd281f595fdc74d3f90f2b925c5 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Thu, 30 Sep 2021 21:34:40 +0100 Subject: [PATCH 2/4] Changelog --- changelog.d/10961.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/10961.misc diff --git a/changelog.d/10961.misc b/changelog.d/10961.misc new file mode 100644 index 000000000000..0e35813488dc --- /dev/null +++ b/changelog.d/10961.misc @@ -0,0 +1 @@ +Add type-hint to `HomeserverTestcase.setup_test_homeserver`. \ No newline at end of file From 7567617b0a3b88cb54263dd98e4d59ea810be8ac Mon Sep 17 00:00:00 2001 From: David Robertson Date: Fri, 1 Oct 2021 11:14:08 +0100 Subject: [PATCH 3/4] Fix a mypy quirk: defining closure after narrowing --- tests/replication/_base.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/replication/_base.py b/tests/replication/_base.py index c886b7aae4b0..2977fbc9aa84 100644 --- a/tests/replication/_base.py +++ b/tests/replication/_base.py @@ -323,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() From a4dbcd7835e2ef9b0d13cdc30c47ed82aa232280 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Fri, 1 Oct 2021 11:50:08 +0100 Subject: [PATCH 4/4] Fix typos, thanks Sean --- tests/replication/_base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/replication/_base.py b/tests/replication/_base.py index 2977fbc9aa84..b77c01646edf 100644 --- a/tests/replication/_base.py +++ b/tests/replication/_base.py @@ -70,11 +70,11 @@ 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 + # Normally we'd pass in the handler to `setup_test_homeserver`, which would # eventually hit "Install @cache_in_self attributes" in tests/utils.py. - # Unfortunatly our handler wants a reference to the homserver. That leaves + # Unfortunately our handler wants a reference to the homeserver. That leaves # us with a chicken-and-egg problem. - # We can workaroudn this: create the homeserver first, create the handler + # 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.