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

Commit 9d21ecf

Browse files
authored
Add type hints to tests files. (#12256)
1 parent 0a59f97 commit 9d21ecf

File tree

6 files changed

+101
-88
lines changed

6 files changed

+101
-88
lines changed

changelog.d/12256.misc

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

mypy.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ exclude = (?x)
8282
|tests/server.py
8383
|tests/server_notices/test_resource_limits_server_notices.py
8484
|tests/state/test_v2.py
85-
|tests/storage/test_background_update.py
8685
|tests/storage/test_base.py
87-
|tests/storage/test_id_generators.py
8886
|tests/storage/test_roommember.py
8987
|tests/test_metrics.py
9088
|tests/test_phone_home.py

tests/handlers/test_typing.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
from unittest.mock import ANY, Mock, call
1919

2020
from twisted.internet import defer
21+
from twisted.test.proto_helpers import MemoryReactor
2122
from twisted.web.resource import Resource
2223

2324
from synapse.api.errors import AuthError
2425
from synapse.federation.transport.server import TransportLayerServer
25-
from synapse.types import UserID, create_requester
26+
from synapse.server import HomeServer
27+
from synapse.types import JsonDict, UserID, create_requester
28+
from synapse.util import Clock
2629

2730
from tests import unittest
2831
from tests.test_utils import make_awaitable
@@ -42,7 +45,9 @@
4245
OTHER_ROOM_ID = "another-room"
4346

4447

45-
def _expect_edu_transaction(edu_type, content, origin="test"):
48+
def _expect_edu_transaction(
49+
edu_type: str, content: JsonDict, origin: str = "test"
50+
) -> JsonDict:
4651
return {
4752
"origin": origin,
4853
"origin_server_ts": 1000000,
@@ -51,12 +56,12 @@ def _expect_edu_transaction(edu_type, content, origin="test"):
5156
}
5257

5358

54-
def _make_edu_transaction_json(edu_type, content):
59+
def _make_edu_transaction_json(edu_type: str, content: JsonDict) -> bytes:
5560
return json.dumps(_expect_edu_transaction(edu_type, content)).encode("utf8")
5661

5762

5863
class TypingNotificationsTestCase(unittest.HomeserverTestCase):
59-
def make_homeserver(self, reactor, clock):
64+
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
6065
# we mock out the keyring so as to skip the authentication check on the
6166
# federation API call.
6267
mock_keyring = Mock(spec=["verify_json_for_server"])
@@ -83,7 +88,7 @@ def create_resource_dict(self) -> Dict[str, Resource]:
8388
d["/_matrix/federation"] = TransportLayerServer(self.hs)
8489
return d
8590

86-
def prepare(self, reactor, clock, hs):
91+
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
8792
mock_notifier = hs.get_notifier()
8893
self.on_new_event = mock_notifier.on_new_event
8994

@@ -111,24 +116,24 @@ def get_received_txn_response(*args):
111116

112117
self.room_members = []
113118

114-
async def check_user_in_room(room_id, user_id):
119+
async def check_user_in_room(room_id: str, user_id: str) -> None:
115120
if user_id not in [u.to_string() for u in self.room_members]:
116121
raise AuthError(401, "User is not in the room")
117122
return None
118123

119124
hs.get_auth().check_user_in_room = check_user_in_room
120125

121-
async def check_host_in_room(room_id, server_name):
126+
async def check_host_in_room(room_id: str, server_name: str) -> bool:
122127
return room_id == ROOM_ID
123128

124129
hs.get_event_auth_handler().check_host_in_room = check_host_in_room
125130

126-
def get_joined_hosts_for_room(room_id):
131+
def get_joined_hosts_for_room(room_id: str):
127132
return {member.domain for member in self.room_members}
128133

129134
self.datastore.get_joined_hosts_for_room = get_joined_hosts_for_room
130135

131-
async def get_users_in_room(room_id):
136+
async def get_users_in_room(room_id: str):
132137
return {str(u) for u in self.room_members}
133138

134139
self.datastore.get_users_in_room = get_users_in_room
@@ -153,7 +158,7 @@ async def get_users_in_room(room_id):
153158
lambda *args, **kwargs: make_awaitable(None)
154159
)
155160

156-
def test_started_typing_local(self):
161+
def test_started_typing_local(self) -> None:
157162
self.room_members = [U_APPLE, U_BANANA]
158163

159164
self.assertEqual(self.event_source.get_current_key(), 0)
@@ -187,7 +192,7 @@ def test_started_typing_local(self):
187192
)
188193

189194
@override_config({"send_federation": True})
190-
def test_started_typing_remote_send(self):
195+
def test_started_typing_remote_send(self) -> None:
191196
self.room_members = [U_APPLE, U_ONION]
192197

193198
self.get_success(
@@ -217,7 +222,7 @@ def test_started_typing_remote_send(self):
217222
try_trailing_slash_on_400=True,
218223
)
219224

220-
def test_started_typing_remote_recv(self):
225+
def test_started_typing_remote_recv(self) -> None:
221226
self.room_members = [U_APPLE, U_ONION]
222227

223228
self.assertEqual(self.event_source.get_current_key(), 0)
@@ -256,7 +261,7 @@ def test_started_typing_remote_recv(self):
256261
],
257262
)
258263

259-
def test_started_typing_remote_recv_not_in_room(self):
264+
def test_started_typing_remote_recv_not_in_room(self) -> None:
260265
self.room_members = [U_APPLE, U_ONION]
261266

262267
self.assertEqual(self.event_source.get_current_key(), 0)
@@ -292,7 +297,7 @@ def test_started_typing_remote_recv_not_in_room(self):
292297
self.assertEqual(events[1], 0)
293298

294299
@override_config({"send_federation": True})
295-
def test_stopped_typing(self):
300+
def test_stopped_typing(self) -> None:
296301
self.room_members = [U_APPLE, U_BANANA, U_ONION]
297302

298303
# Gut-wrenching
@@ -343,7 +348,7 @@ def test_stopped_typing(self):
343348
[{"type": "m.typing", "room_id": ROOM_ID, "content": {"user_ids": []}}],
344349
)
345350

346-
def test_typing_timeout(self):
351+
def test_typing_timeout(self) -> None:
347352
self.room_members = [U_APPLE, U_BANANA]
348353

349354
self.assertEqual(self.event_source.get_current_key(), 0)

tests/push/test_push_rule_evaluator.py

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

15-
from typing import Any, Dict
15+
from typing import Dict, Optional, Union
1616

1717
import frozendict
1818

1919
from synapse.api.room_versions import RoomVersions
2020
from synapse.events import FrozenEvent
2121
from synapse.push import push_rule_evaluator
2222
from synapse.push.push_rule_evaluator import PushRuleEvaluatorForEvent
23+
from synapse.types import JsonDict
2324

2425
from tests import unittest
2526

2627

2728
class PushRuleEvaluatorTestCase(unittest.TestCase):
28-
def _get_evaluator(self, content):
29+
def _get_evaluator(self, content: JsonDict) -> PushRuleEvaluatorForEvent:
2930
event = FrozenEvent(
3031
{
3132
"event_id": "$event_id",
@@ -39,12 +40,12 @@ def _get_evaluator(self, content):
3940
)
4041
room_member_count = 0
4142
sender_power_level = 0
42-
power_levels = {}
43+
power_levels: Dict[str, Union[int, Dict[str, int]]] = {}
4344
return PushRuleEvaluatorForEvent(
4445
event, room_member_count, sender_power_level, power_levels
4546
)
4647

47-
def test_display_name(self):
48+
def test_display_name(self) -> None:
4849
"""Check for a matching display name in the body of the event."""
4950
evaluator = self._get_evaluator({"body": "foo bar baz"})
5051

@@ -71,20 +72,20 @@ def test_display_name(self):
7172
self.assertTrue(evaluator.matches(condition, "@user:test", "foo bar"))
7273

7374
def _assert_matches(
74-
self, condition: Dict[str, Any], content: Dict[str, Any], msg=None
75+
self, condition: JsonDict, content: JsonDict, msg: Optional[str] = None
7576
) -> None:
7677
evaluator = self._get_evaluator(content)
7778
self.assertTrue(evaluator.matches(condition, "@user:test", "display_name"), msg)
7879

7980
def _assert_not_matches(
80-
self, condition: Dict[str, Any], content: Dict[str, Any], msg=None
81+
self, condition: JsonDict, content: JsonDict, msg: Optional[str] = None
8182
) -> None:
8283
evaluator = self._get_evaluator(content)
8384
self.assertFalse(
8485
evaluator.matches(condition, "@user:test", "display_name"), msg
8586
)
8687

87-
def test_event_match_body(self):
88+
def test_event_match_body(self) -> None:
8889
"""Check that event_match conditions on content.body work as expected"""
8990

9091
# if the key is `content.body`, the pattern matches substrings.
@@ -165,7 +166,7 @@ def test_event_match_body(self):
165166
r"? after \ should match any character",
166167
)
167168

168-
def test_event_match_non_body(self):
169+
def test_event_match_non_body(self) -> None:
169170
"""Check that event_match conditions on other keys work as expected"""
170171

171172
# if the key is anything other than 'content.body', the pattern must match the
@@ -241,7 +242,7 @@ def test_event_match_non_body(self):
241242
"pattern should not match before a newline",
242243
)
243244

244-
def test_no_body(self):
245+
def test_no_body(self) -> None:
245246
"""Not having a body shouldn't break the evaluator."""
246247
evaluator = self._get_evaluator({})
247248

@@ -250,7 +251,7 @@ def test_no_body(self):
250251
}
251252
self.assertFalse(evaluator.matches(condition, "@user:test", "foo"))
252253

253-
def test_invalid_body(self):
254+
def test_invalid_body(self) -> None:
254255
"""A non-string body should not break the evaluator."""
255256
condition = {
256257
"kind": "contains_display_name",
@@ -260,7 +261,7 @@ def test_invalid_body(self):
260261
evaluator = self._get_evaluator({"body": body})
261262
self.assertFalse(evaluator.matches(condition, "@user:test", "foo"))
262263

263-
def test_tweaks_for_actions(self):
264+
def test_tweaks_for_actions(self) -> None:
264265
"""
265266
This tests the behaviour of tweaks_for_actions.
266267
"""

0 commit comments

Comments
 (0)