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

Commit 3c3ba31

Browse files
authored
Add missing type hints for tests.events. (#14904)
1 parent 8bc5d14 commit 3c3ba31

File tree

6 files changed

+91
-64
lines changed

6 files changed

+91
-64
lines changed

changelog.d/14904.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add missing type hints.

mypy.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ exclude = (?x)
3535
|tests/api/test_auth.py
3636
|tests/app/test_openid_listener.py
3737
|tests/appservice/test_scheduler.py
38-
|tests/events/test_presence_router.py
39-
|tests/events/test_utils.py
4038
|tests/federation/test_federation_catch_up.py
4139
|tests/federation/test_federation_sender.py
4240
|tests/handlers/test_typing.py
@@ -86,6 +84,9 @@ disallow_untyped_defs = True
8684
[mypy-tests.crypto.*]
8785
disallow_untyped_defs = True
8886

87+
[mypy-tests.events.*]
88+
disallow_untyped_defs = True
89+
8990
[mypy-tests.federation.transport.test_client]
9091
disallow_untyped_defs = True
9192

synapse/events/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,10 +605,11 @@ def serialize_events(
605605

606606

607607
_PowerLevel = Union[str, int]
608+
PowerLevelsContent = Mapping[str, Union[_PowerLevel, Mapping[str, _PowerLevel]]]
608609

609610

610611
def copy_and_fixup_power_levels_contents(
611-
old_power_levels: Mapping[str, Union[_PowerLevel, Mapping[str, _PowerLevel]]]
612+
old_power_levels: PowerLevelsContent,
612613
) -> Dict[str, Union[int, Dict[str, int]]]:
613614
"""Copy the content of a power_levels event, unfreezing frozendicts along the way.
614615

tests/events/test_presence_router.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@
1616

1717
import attr
1818

19+
from twisted.test.proto_helpers import MemoryReactor
20+
1921
from synapse.api.constants import EduTypes
2022
from synapse.events.presence_router import PresenceRouter, load_legacy_presence_router
2123
from synapse.federation.units import Transaction
2224
from synapse.handlers.presence import UserPresenceState
2325
from synapse.module_api import ModuleApi
2426
from synapse.rest import admin
2527
from synapse.rest.client import login, presence, room
28+
from synapse.server import HomeServer
2629
from synapse.types import JsonDict, StreamToken, create_requester
30+
from synapse.util import Clock
2731

2832
from tests.handlers.test_sync import generate_sync_config
2933
from tests.test_utils import simple_async_mock
30-
from tests.unittest import FederatingHomeserverTestCase, TestCase, override_config
34+
from tests.unittest import FederatingHomeserverTestCase, override_config
3135

3236

3337
@attr.s
@@ -49,9 +53,7 @@ async def get_users_for_states(
4953
}
5054
return users_to_state
5155

52-
async def get_interested_users(
53-
self, user_id: str
54-
) -> Union[Set[str], PresenceRouter.ALL_USERS]:
56+
async def get_interested_users(self, user_id: str) -> Union[Set[str], str]:
5557
if user_id in self._config.users_who_should_receive_all_presence:
5658
return PresenceRouter.ALL_USERS
5759

@@ -71,9 +73,14 @@ def parse_config(config_dict: dict) -> PresenceRouterTestConfig:
7173
# Initialise a typed config object
7274
config = PresenceRouterTestConfig()
7375

74-
config.users_who_should_receive_all_presence = config_dict.get(
76+
users_who_should_receive_all_presence = config_dict.get(
7577
"users_who_should_receive_all_presence"
7678
)
79+
assert isinstance(users_who_should_receive_all_presence, list)
80+
81+
config.users_who_should_receive_all_presence = (
82+
users_who_should_receive_all_presence
83+
)
7784

7885
return config
7986

@@ -96,9 +103,7 @@ async def get_users_for_states(
96103
}
97104
return users_to_state
98105

99-
async def get_interested_users(
100-
self, user_id: str
101-
) -> Union[Set[str], PresenceRouter.ALL_USERS]:
106+
async def get_interested_users(self, user_id: str) -> Union[Set[str], str]:
102107
if user_id in self._config.users_who_should_receive_all_presence:
103108
return PresenceRouter.ALL_USERS
104109

@@ -118,9 +123,14 @@ def parse_config(config_dict: dict) -> PresenceRouterTestConfig:
118123
# Initialise a typed config object
119124
config = PresenceRouterTestConfig()
120125

121-
config.users_who_should_receive_all_presence = config_dict.get(
126+
users_who_should_receive_all_presence = config_dict.get(
122127
"users_who_should_receive_all_presence"
123128
)
129+
assert isinstance(users_who_should_receive_all_presence, list)
130+
131+
config.users_who_should_receive_all_presence = (
132+
users_who_should_receive_all_presence
133+
)
124134

125135
return config
126136

@@ -140,7 +150,7 @@ class PresenceRouterTestCase(FederatingHomeserverTestCase):
140150
presence.register_servlets,
141151
]
142152

143-
def make_homeserver(self, reactor, clock):
153+
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
144154
# Mock out the calls over federation.
145155
fed_transport_client = Mock(spec=["send_transaction"])
146156
fed_transport_client.send_transaction = simple_async_mock({})
@@ -153,7 +163,9 @@ def make_homeserver(self, reactor, clock):
153163

154164
return hs
155165

156-
def prepare(self, reactor, clock, homeserver):
166+
def prepare(
167+
self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
168+
) -> None:
157169
self.sync_handler = self.hs.get_sync_handler()
158170
self.module_api = homeserver.get_module_api()
159171

@@ -176,7 +188,7 @@ def default_config(self) -> JsonDict:
176188
},
177189
}
178190
)
179-
def test_receiving_all_presence_legacy(self):
191+
def test_receiving_all_presence_legacy(self) -> None:
180192
self.receiving_all_presence_test_body()
181193

182194
@override_config(
@@ -193,10 +205,10 @@ def test_receiving_all_presence_legacy(self):
193205
],
194206
}
195207
)
196-
def test_receiving_all_presence(self):
208+
def test_receiving_all_presence(self) -> None:
197209
self.receiving_all_presence_test_body()
198210

199-
def receiving_all_presence_test_body(self):
211+
def receiving_all_presence_test_body(self) -> None:
200212
"""Test that a user that does not share a room with another other can receive
201213
presence for them, due to presence routing.
202214
"""
@@ -302,7 +314,7 @@ def receiving_all_presence_test_body(self):
302314
},
303315
}
304316
)
305-
def test_send_local_online_presence_to_with_module_legacy(self):
317+
def test_send_local_online_presence_to_with_module_legacy(self) -> None:
306318
self.send_local_online_presence_to_with_module_test_body()
307319

308320
@override_config(
@@ -321,10 +333,10 @@ def test_send_local_online_presence_to_with_module_legacy(self):
321333
],
322334
}
323335
)
324-
def test_send_local_online_presence_to_with_module(self):
336+
def test_send_local_online_presence_to_with_module(self) -> None:
325337
self.send_local_online_presence_to_with_module_test_body()
326338

327-
def send_local_online_presence_to_with_module_test_body(self):
339+
def send_local_online_presence_to_with_module_test_body(self) -> None:
328340
"""Tests that send_local_presence_to_users sends local online presence to a set
329341
of specified local and remote users, with a custom PresenceRouter module enabled.
330342
"""
@@ -447,18 +459,18 @@ def send_local_online_presence_to_with_module_test_body(self):
447459
continue
448460

449461
# EDUs can contain multiple presence updates
450-
for presence_update in edu["content"]["push"]:
462+
for presence_edu in edu["content"]["push"]:
451463
# Check for presence updates that contain the user IDs we're after
452-
found_users.add(presence_update["user_id"])
464+
found_users.add(presence_edu["user_id"])
453465

454466
# Ensure that no offline states are being sent out
455-
self.assertNotEqual(presence_update["presence"], "offline")
467+
self.assertNotEqual(presence_edu["presence"], "offline")
456468

457469
self.assertEqual(found_users, expected_users)
458470

459471

460472
def send_presence_update(
461-
testcase: TestCase,
473+
testcase: FederatingHomeserverTestCase,
462474
user_id: str,
463475
access_token: str,
464476
presence_state: str,
@@ -479,7 +491,7 @@ def send_presence_update(
479491

480492

481493
def sync_presence(
482-
testcase: TestCase,
494+
testcase: FederatingHomeserverTestCase,
483495
user_id: str,
484496
since_token: Optional[StreamToken] = None,
485497
) -> Tuple[List[UserPresenceState], StreamToken]:
@@ -500,7 +512,7 @@ def sync_presence(
500512
requester = create_requester(user_id)
501513
sync_config = generate_sync_config(requester.user.to_string())
502514
sync_result = testcase.get_success(
503-
testcase.sync_handler.wait_for_sync_for_user(
515+
testcase.hs.get_sync_handler().wait_for_sync_for_user(
504516
requester, sync_config, since_token
505517
)
506518
)

tests/events/test_snapshot.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from twisted.test.proto_helpers import MemoryReactor
16+
17+
from synapse.events import EventBase
1518
from synapse.events.snapshot import EventContext
1619
from synapse.rest import admin
1720
from synapse.rest.client import login, room
21+
from synapse.server import HomeServer
22+
from synapse.util import Clock
1823

1924
from tests import unittest
2025
from tests.test_utils.event_injection import create_event
@@ -27,15 +32,15 @@ class TestEventContext(unittest.HomeserverTestCase):
2732
room.register_servlets,
2833
]
2934

30-
def prepare(self, reactor, clock, hs):
35+
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
3136
self.store = hs.get_datastores().main
3237
self._storage_controllers = hs.get_storage_controllers()
3338

3439
self.user_id = self.register_user("u1", "pass")
3540
self.user_tok = self.login("u1", "pass")
3641
self.room_id = self.helper.create_room_as(tok=self.user_tok)
3742

38-
def test_serialize_deserialize_msg(self):
43+
def test_serialize_deserialize_msg(self) -> None:
3944
"""Test that an EventContext for a message event is the same after
4045
serialize/deserialize.
4146
"""
@@ -51,7 +56,7 @@ def test_serialize_deserialize_msg(self):
5156

5257
self._check_serialize_deserialize(event, context)
5358

54-
def test_serialize_deserialize_state_no_prev(self):
59+
def test_serialize_deserialize_state_no_prev(self) -> None:
5560
"""Test that an EventContext for a state event (with not previous entry)
5661
is the same after serialize/deserialize.
5762
"""
@@ -67,7 +72,7 @@ def test_serialize_deserialize_state_no_prev(self):
6772

6873
self._check_serialize_deserialize(event, context)
6974

70-
def test_serialize_deserialize_state_prev(self):
75+
def test_serialize_deserialize_state_prev(self) -> None:
7176
"""Test that an EventContext for a state event (which replaces a
7277
previous entry) is the same after serialize/deserialize.
7378
"""
@@ -84,7 +89,9 @@ def test_serialize_deserialize_state_prev(self):
8489

8590
self._check_serialize_deserialize(event, context)
8691

87-
def _check_serialize_deserialize(self, event, context):
92+
def _check_serialize_deserialize(
93+
self, event: EventBase, context: EventContext
94+
) -> None:
8895
serialized = self.get_success(context.serialize(event, self.store))
8996

9097
d_context = EventContext.deserialize(self._storage_controllers, serialized)

0 commit comments

Comments
 (0)