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

Add final type hint to tests.unittest. #15072

Merged
merged 13 commits into from
Feb 14, 2023
1 change: 1 addition & 0 deletions changelog.d/15072.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve type hints.
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ disallow_untyped_defs = False
[mypy-synapse.storage.database]
disallow_untyped_defs = False

[mypy-tests.unittest]
disallow_untyped_defs = False

[mypy-tests.util.caches.test_descriptors]
disallow_untyped_defs = False

Expand Down
8 changes: 6 additions & 2 deletions tests/app/test_openid_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ def test_openid_listener(self, names: List[str], expectation: str) -> None:
}

# Listen with the config
self.hs._listen_http(parse_listener_def(0, config))
hs = self.hs
assert isinstance(hs, GenericWorkerServer)
hs._listen_http(parse_listener_def(0, config))

# Grab the resource from the site that was told to listen
site = self.reactor.tcpServers[0][1]
Expand Down Expand Up @@ -115,7 +117,9 @@ def test_openid_listener(self, names: List[str], expectation: str) -> None:
}

# Listen with the config
self.hs._listener_http(self.hs.config, parse_listener_def(0, config))
hs = self.hs
assert isinstance(hs, SynapseHomeServer)
hs._listener_http(self.hs.config, parse_listener_def(0, config))

# Grab the resource from the site that was told to listen
site = self.reactor.tcpServers[0][1]
Expand Down
22 changes: 11 additions & 11 deletions tests/crypto/test_keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def test_verify_json_for_server(self) -> None:
key1 = signedjson.key.generate_signing_key("1")
r = self.hs.get_datastores().main.store_server_verify_keys(
"server9",
time.time() * 1000,
int(time.time() * 1000),
[("server9", get_key_id(key1), FetchKeyResult(get_verify_key(key1), 1000))],
)
self.get_success(r)
Expand Down Expand Up @@ -287,7 +287,7 @@ def test_verify_json_for_server_with_null_valid_until_ms(self) -> None:
key1 = signedjson.key.generate_signing_key("1")
r = self.hs.get_datastores().main.store_server_verify_keys(
"server9",
time.time() * 1000,
int(time.time() * 1000),
# None is not a valid value in FetchKeyResult, but we're abusing this
# API to insert null values into the database. The nulls get converted
# to 0 when fetched in KeyStore.get_server_verify_keys.
Expand Down Expand Up @@ -466,9 +466,9 @@ async def get_json(destination: str, path: str, **kwargs: Any) -> JsonDict:
key_json = self.get_success(
self.hs.get_datastores().main.get_server_keys_json([lookup_triplet])
)
res = key_json[lookup_triplet]
self.assertEqual(len(res), 1)
res = res[0]
res_keys = key_json[lookup_triplet]
self.assertEqual(len(res_keys), 1)
res = res_keys[0]
self.assertEqual(res["key_id"], testverifykey_id)
self.assertEqual(res["from_server"], SERVER_NAME)
self.assertEqual(res["ts_added_ms"], self.reactor.seconds() * 1000)
Expand Down Expand Up @@ -584,9 +584,9 @@ def test_get_keys_from_perspectives(self) -> None:
key_json = self.get_success(
self.hs.get_datastores().main.get_server_keys_json([lookup_triplet])
)
res = key_json[lookup_triplet]
self.assertEqual(len(res), 1)
res = res[0]
res_keys = key_json[lookup_triplet]
self.assertEqual(len(res_keys), 1)
res = res_keys[0]
self.assertEqual(res["key_id"], testverifykey_id)
self.assertEqual(res["from_server"], self.mock_perspective_server.server_name)
self.assertEqual(res["ts_added_ms"], self.reactor.seconds() * 1000)
Expand Down Expand Up @@ -705,9 +705,9 @@ def test_get_perspectives_own_key(self) -> None:
key_json = self.get_success(
self.hs.get_datastores().main.get_server_keys_json([lookup_triplet])
)
res = key_json[lookup_triplet]
self.assertEqual(len(res), 1)
res = res[0]
res_keys = key_json[lookup_triplet]
self.assertEqual(len(res_keys), 1)
res = res_keys[0]
self.assertEqual(res["key_id"], testverifykey_id)
self.assertEqual(res["from_server"], self.mock_perspective_server.server_name)
self.assertEqual(res["ts_added_ms"], self.reactor.seconds() * 1000)
Expand Down
12 changes: 5 additions & 7 deletions tests/events/test_presence_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ class PresenceRouterTestCase(FederatingHomeserverTestCase):

def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
# Mock out the calls over federation.
fed_transport_client = Mock(spec=["send_transaction"])
fed_transport_client.send_transaction = simple_async_mock({})
self.fed_transport_client = Mock(spec=["send_transaction"])
self.fed_transport_client.send_transaction = simple_async_mock({})

hs = self.setup_test_homeserver(
federation_transport_client=fed_transport_client,
federation_transport_client=self.fed_transport_client,
)

load_legacy_presence_router(hs)
Expand Down Expand Up @@ -422,7 +422,7 @@ def send_local_online_presence_to_with_module_test_body(self) -> None:
#
# Thus we reset the mock, and try sending all online local user
# presence again
self.hs.get_federation_transport_client().send_transaction.reset_mock()
self.fed_transport_client.send_transaction.reset_mock()

# Broadcast local user online presence
self.get_success(
Expand All @@ -447,9 +447,7 @@ def send_local_online_presence_to_with_module_test_body(self) -> None:
}
found_users = set()

calls = (
self.hs.get_federation_transport_client().send_transaction.call_args_list
)
calls = self.fed_transport_client.send_transaction.call_args_list
for call in calls:
call_args = call[0]
federation_transaction: Transaction = call_args[0]
Expand Down
35 changes: 20 additions & 15 deletions tests/federation/test_complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from synapse.api.errors import Codes, SynapseError
from synapse.rest import admin
from synapse.rest.client import login, room
from synapse.types import JsonDict, UserID
from synapse.types import JsonDict, UserID, create_requester

from tests import unittest
from tests.test_utils import make_awaitable
Expand Down Expand Up @@ -56,7 +56,11 @@ def test_complexity_simple(self) -> None:

# Artificially raise the complexity
store = self.hs.get_datastores().main
store.get_current_state_event_counts = lambda x: make_awaitable(500 * 1.23)

async def get_current_state_event_counts(room_id: str) -> int:
return int(500 * 1.23)

store.get_current_state_event_counts = get_current_state_event_counts # type: ignore[assignment]

# Get the room complexity again -- make sure it's our artificial value
channel = self.make_signed_federation_request(
Expand All @@ -75,12 +79,12 @@ def test_join_too_large(self) -> None:

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
handler.federation_handler.do_invite_join = Mock(
handler.federation_handler.do_invite_join = Mock( # type: ignore[assignment]
return_value=make_awaitable(("", 1))
)

d = handler._remote_join(
None,
create_requester(u1),
["other.example.com"],
"roomid",
UserID.from_string(u1),
Expand All @@ -106,12 +110,12 @@ def test_join_too_large_admin(self) -> None:

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
handler.federation_handler.do_invite_join = Mock(
handler.federation_handler.do_invite_join = Mock( # type: ignore[assignment]
return_value=make_awaitable(("", 1))
)

d = handler._remote_join(
None,
create_requester(u1),
["other.example.com"],
"roomid",
UserID.from_string(u1),
Expand Down Expand Up @@ -144,17 +148,18 @@ def test_join_too_large_once_joined(self) -> None:

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(return_value=make_awaitable(None))
handler.federation_handler.do_invite_join = Mock(
handler.federation_handler.do_invite_join = Mock( # type: ignore[assignment]
return_value=make_awaitable(("", 1))
)

# Artificially raise the complexity
self.hs.get_datastores().main.get_current_state_event_counts = (
lambda x: make_awaitable(600)
)
async def get_current_state_event_counts(room_id: str) -> int:
return 600

self.hs.get_datastores().main.get_current_state_event_counts = get_current_state_event_counts # type: ignore[assignment]

d = handler._remote_join(
None,
create_requester(u1),
["other.example.com"],
room_1,
UserID.from_string(u1),
Expand Down Expand Up @@ -200,12 +205,12 @@ def test_join_too_large_no_admin(self) -> None:

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
handler.federation_handler.do_invite_join = Mock(
handler.federation_handler.do_invite_join = Mock( # type: ignore[assignment]
return_value=make_awaitable(("", 1))
)

d = handler._remote_join(
None,
create_requester(u1),
["other.example.com"],
"roomid",
UserID.from_string(u1),
Expand All @@ -230,12 +235,12 @@ def test_join_too_large_admin(self) -> None:

# Mock out some things, because we don't want to test the whole join
fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
handler.federation_handler.do_invite_join = Mock(
handler.federation_handler.do_invite_join = Mock( # type: ignore[assignment]
return_value=make_awaitable(("", 1))
)

d = handler._remote_join(
None,
create_requester(u1),
["other.example.com"],
"roomid",
UserID.from_string(u1),
Expand Down
32 changes: 22 additions & 10 deletions tests/federation/test_federation_catch_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

from synapse.api.constants import EventTypes
from synapse.events import EventBase
from synapse.federation.sender import PerDestinationQueue, TransactionManager
from synapse.federation.sender import (
FederationSender,
PerDestinationQueue,
TransactionManager,
)
from synapse.federation.units import Edu, Transaction
from synapse.rest import admin
from synapse.rest.client import login, room
Expand Down Expand Up @@ -33,8 +37,9 @@ class FederationCatchUpTestCases(FederatingHomeserverTestCase):
]

def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
self.federation_transport_client = Mock(spec=["send_transaction"])
return self.setup_test_homeserver(
federation_transport_client=Mock(spec=["send_transaction"]),
federation_transport_client=self.federation_transport_client,
)

def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
Expand All @@ -52,10 +57,14 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.pdus: List[JsonDict] = []
self.failed_pdus: List[JsonDict] = []
self.is_online = True
self.hs.get_federation_transport_client().send_transaction.side_effect = (
self.federation_transport_client.send_transaction.side_effect = (
self.record_transaction
)

federation_sender = hs.get_federation_sender()
assert isinstance(federation_sender, FederationSender)
self.federation_sender = federation_sender

def default_config(self) -> JsonDict:
config = super().default_config()
config["federation_sender_instances"] = None
Expand Down Expand Up @@ -229,11 +238,11 @@ def test_catch_up_from_blank_state(self) -> None:
# let's delete the federation transmission queue
# (this pretends we are starting up fresh.)
self.assertFalse(
self.hs.get_federation_sender()
._per_destination_queues["host2"]
.transmission_loop_running
self.federation_sender._per_destination_queues[
"host2"
].transmission_loop_running
)
del self.hs.get_federation_sender()._per_destination_queues["host2"]
del self.federation_sender._per_destination_queues["host2"]

# let's also clear any backoffs
self.get_success(
Expand Down Expand Up @@ -322,6 +331,7 @@ def test_catch_up_loop(self) -> None:
# also fetch event 5 so we know its last_successful_stream_ordering later
event_5 = self.get_success(self.hs.get_datastores().main.get_event(event_id_5))

assert event_2.internal_metadata.stream_ordering is not None
self.get_success(
self.hs.get_datastores().main.set_destination_last_successful_stream_ordering(
"host2", event_2.internal_metadata.stream_ordering
Expand Down Expand Up @@ -425,15 +435,16 @@ def test_catch_up_on_synapse_startup(self) -> None:
def wake_destination_track(destination: str) -> None:
woken.append(destination)

self.hs.get_federation_sender().wake_destination = wake_destination_track
self.federation_sender.wake_destination = wake_destination_track # type: ignore[assignment]

# cancel the pre-existing timer for _wake_destinations_needing_catchup
# this is because we are calling it manually rather than waiting for it
# to be called automatically
self.hs.get_federation_sender()._catchup_after_startup_timer.cancel()
assert self.federation_sender._catchup_after_startup_timer is not None
self.federation_sender._catchup_after_startup_timer.cancel()

self.get_success(
self.hs.get_federation_sender()._wake_destinations_needing_catchup(), by=5.0
self.federation_sender._wake_destinations_needing_catchup(), by=5.0
)

# ASSERT (_wake_destinations_needing_catchup):
Expand Down Expand Up @@ -475,6 +486,7 @@ def test_not_latest_event(self) -> None:
)
)

assert event_1.internal_metadata.stream_ordering is not None
self.get_success(
self.hs.get_datastores().main.set_destination_last_successful_stream_ordering(
"host2", event_1.internal_metadata.stream_ordering
Expand Down
4 changes: 2 additions & 2 deletions tests/federation/test_federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_get_pdu_event_from_cache_is_pristine(self) -> None:
RoomVersions.V9,
)
)
self.assertIsNotNone(pulled_pdu_info2)
assert pulled_pdu_info2 is not None
remote_pdu2 = pulled_pdu_info2.pdu

# Sanity check that we are working against the same event
Expand Down Expand Up @@ -226,7 +226,7 @@ def _get_pdu_once(self) -> EventBase:
RoomVersions.V9,
)
)
self.assertIsNotNone(pulled_pdu_info)
assert pulled_pdu_info is not None
remote_pdu = pulled_pdu_info.pdu

# check the right call got made to the agent
Expand Down
Loading