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

Commit e8269ed

Browse files
author
David Robertson
authored
Type hints for tests.appservice (#14990)
* Accept a Sequence of events in synapse.appservice This avoids some casts/ignores in the tests I'm about to fixup. It seems that `List[Mock]` is not a subtype of `List[EventBase]`, but `Sequence[Mock]` is a subtype of `Sequence[EventBase]`. So presumably `Mock` is considered a subtype of anything, much like `Any`. * make tests.appservice.test_scheduler pass mypy * Extra hints in tests.appservice.test_scheduler * Extra hints in tests.appservice.test_api * Extra hints in tests.appservice.test_appservice * Disallow untyped defs * Changelog
1 parent 3e37ff1 commit e8269ed

File tree

9 files changed

+132
-59
lines changed

9 files changed

+132
-59
lines changed

changelog.d/14990.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve type hints.

mypy.ini

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ exclude = (?x)
3232
|synapse/storage/databases/main/cache.py
3333
|synapse/storage/schema/
3434

35-
|tests/appservice/test_scheduler.py
3635
|tests/federation/test_federation_catch_up.py
3736
|tests/federation/test_federation_sender.py
3837
|tests/http/federation/test_matrix_federation_agent.py
@@ -78,6 +77,9 @@ disallow_untyped_defs = True
7877
[mypy-tests.app.*]
7978
disallow_untyped_defs = True
8079

80+
[mypy-tests.appservice.*]
81+
disallow_untyped_defs = True
82+
8183
[mypy-tests.config.*]
8284
disallow_untyped_defs = True
8385

synapse/appservice/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import logging
1717
import re
1818
from enum import Enum
19-
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Pattern
19+
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Pattern, Sequence
2020

2121
import attr
2222
from netaddr import IPSet
@@ -377,7 +377,7 @@ def __init__(
377377
self,
378378
service: ApplicationService,
379379
id: int,
380-
events: List[EventBase],
380+
events: Sequence[EventBase],
381381
ephemeral: List[JsonDict],
382382
to_device_messages: List[JsonDict],
383383
one_time_keys_count: TransactionOneTimeKeysCount,

synapse/appservice/api.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@
1414
# limitations under the License.
1515
import logging
1616
import urllib.parse
17-
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Mapping, Optional, Tuple
17+
from typing import (
18+
TYPE_CHECKING,
19+
Any,
20+
Dict,
21+
Iterable,
22+
List,
23+
Mapping,
24+
Optional,
25+
Sequence,
26+
Tuple,
27+
)
1828

1929
from prometheus_client import Counter
2030
from typing_extensions import TypeGuard
@@ -259,7 +269,7 @@ async def _get() -> Optional[JsonDict]:
259269
async def push_bulk(
260270
self,
261271
service: "ApplicationService",
262-
events: List[EventBase],
272+
events: Sequence[EventBase],
263273
ephemeral: List[JsonDict],
264274
to_device_messages: List[JsonDict],
265275
one_time_keys_count: TransactionOneTimeKeysCount,

synapse/appservice/scheduler.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
Iterable,
5858
List,
5959
Optional,
60+
Sequence,
6061
Set,
6162
Tuple,
6263
)
@@ -364,7 +365,7 @@ def __init__(self, clock: Clock, store: DataStore, as_api: ApplicationServiceApi
364365
async def send(
365366
self,
366367
service: ApplicationService,
367-
events: List[EventBase],
368+
events: Sequence[EventBase],
368369
ephemeral: Optional[List[JsonDict]] = None,
369370
to_device_messages: Optional[List[JsonDict]] = None,
370371
one_time_keys_count: Optional[TransactionOneTimeKeysCount] = None,

synapse/storage/databases/main/appservice.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@
1414
# limitations under the License.
1515
import logging
1616
import re
17-
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Pattern, Tuple, cast
17+
from typing import (
18+
TYPE_CHECKING,
19+
Any,
20+
Dict,
21+
List,
22+
Optional,
23+
Pattern,
24+
Sequence,
25+
Tuple,
26+
cast,
27+
)
1828

1929
from synapse.appservice import (
2030
ApplicationService,
@@ -257,7 +267,7 @@ async def set_appservice_state(
257267
async def create_appservice_txn(
258268
self,
259269
service: ApplicationService,
260-
events: List[EventBase],
270+
events: Sequence[EventBase],
261271
ephemeral: List[JsonDict],
262272
to_device_messages: List[JsonDict],
263273
one_time_keys_count: TransactionOneTimeKeysCount,

tests/appservice/test_api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030

3131
class ApplicationServiceApiTestCase(unittest.HomeserverTestCase):
32-
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer):
32+
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
3333
self.api = hs.get_application_service_api()
3434
self.service = ApplicationService(
3535
id="unique_identifier",
@@ -39,7 +39,7 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer):
3939
hs_token=TOKEN,
4040
)
4141

42-
def test_query_3pe_authenticates_token(self):
42+
def test_query_3pe_authenticates_token(self) -> None:
4343
"""
4444
Tests that 3pe queries to the appservice are authenticated
4545
with the appservice's token.

tests/appservice/test_appservice.py

+38-17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import re
15+
from typing import Generator
1516
from unittest.mock import Mock
1617

1718
from twisted.internet import defer
@@ -27,7 +28,7 @@ def _regex(regex: str, exclusive: bool = True) -> Namespace:
2728

2829

2930
class ApplicationServiceTestCase(unittest.TestCase):
30-
def setUp(self):
31+
def setUp(self) -> None:
3132
self.service = ApplicationService(
3233
id="unique_identifier",
3334
sender="@as:test",
@@ -46,7 +47,9 @@ def setUp(self):
4647
self.store.get_local_users_in_room = simple_async_mock([])
4748

4849
@defer.inlineCallbacks
49-
def test_regex_user_id_prefix_match(self):
50+
def test_regex_user_id_prefix_match(
51+
self,
52+
) -> Generator["defer.Deferred[object]", object, None]:
5053
self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
5154
self.event.sender = "@irc_foobar:matrix.org"
5255
self.assertTrue(
@@ -60,7 +63,9 @@ def test_regex_user_id_prefix_match(self):
6063
)
6164

6265
@defer.inlineCallbacks
63-
def test_regex_user_id_prefix_no_match(self):
66+
def test_regex_user_id_prefix_no_match(
67+
self,
68+
) -> Generator["defer.Deferred[object]", object, None]:
6469
self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
6570
self.event.sender = "@someone_else:matrix.org"
6671
self.assertFalse(
@@ -74,7 +79,9 @@ def test_regex_user_id_prefix_no_match(self):
7479
)
7580

7681
@defer.inlineCallbacks
77-
def test_regex_room_member_is_checked(self):
82+
def test_regex_room_member_is_checked(
83+
self,
84+
) -> Generator["defer.Deferred[object]", object, None]:
7885
self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
7986
self.event.sender = "@someone_else:matrix.org"
8087
self.event.type = "m.room.member"
@@ -90,7 +97,9 @@ def test_regex_room_member_is_checked(self):
9097
)
9198

9299
@defer.inlineCallbacks
93-
def test_regex_room_id_match(self):
100+
def test_regex_room_id_match(
101+
self,
102+
) -> Generator["defer.Deferred[object]", object, None]:
94103
self.service.namespaces[ApplicationService.NS_ROOMS].append(
95104
_regex("!some_prefix.*some_suffix:matrix.org")
96105
)
@@ -106,7 +115,9 @@ def test_regex_room_id_match(self):
106115
)
107116

108117
@defer.inlineCallbacks
109-
def test_regex_room_id_no_match(self):
118+
def test_regex_room_id_no_match(
119+
self,
120+
) -> Generator["defer.Deferred[object]", object, None]:
110121
self.service.namespaces[ApplicationService.NS_ROOMS].append(
111122
_regex("!some_prefix.*some_suffix:matrix.org")
112123
)
@@ -122,7 +133,9 @@ def test_regex_room_id_no_match(self):
122133
)
123134

124135
@defer.inlineCallbacks
125-
def test_regex_alias_match(self):
136+
def test_regex_alias_match(
137+
self,
138+
) -> Generator["defer.Deferred[object]", object, None]:
126139
self.service.namespaces[ApplicationService.NS_ALIASES].append(
127140
_regex("#irc_.*:matrix.org")
128141
)
@@ -140,44 +153,46 @@ def test_regex_alias_match(self):
140153
)
141154
)
142155

143-
def test_non_exclusive_alias(self):
156+
def test_non_exclusive_alias(self) -> None:
144157
self.service.namespaces[ApplicationService.NS_ALIASES].append(
145158
_regex("#irc_.*:matrix.org", exclusive=False)
146159
)
147160
self.assertFalse(self.service.is_exclusive_alias("#irc_foobar:matrix.org"))
148161

149-
def test_non_exclusive_room(self):
162+
def test_non_exclusive_room(self) -> None:
150163
self.service.namespaces[ApplicationService.NS_ROOMS].append(
151164
_regex("!irc_.*:matrix.org", exclusive=False)
152165
)
153166
self.assertFalse(self.service.is_exclusive_room("!irc_foobar:matrix.org"))
154167

155-
def test_non_exclusive_user(self):
168+
def test_non_exclusive_user(self) -> None:
156169
self.service.namespaces[ApplicationService.NS_USERS].append(
157170
_regex("@irc_.*:matrix.org", exclusive=False)
158171
)
159172
self.assertFalse(self.service.is_exclusive_user("@irc_foobar:matrix.org"))
160173

161-
def test_exclusive_alias(self):
174+
def test_exclusive_alias(self) -> None:
162175
self.service.namespaces[ApplicationService.NS_ALIASES].append(
163176
_regex("#irc_.*:matrix.org", exclusive=True)
164177
)
165178
self.assertTrue(self.service.is_exclusive_alias("#irc_foobar:matrix.org"))
166179

167-
def test_exclusive_user(self):
180+
def test_exclusive_user(self) -> None:
168181
self.service.namespaces[ApplicationService.NS_USERS].append(
169182
_regex("@irc_.*:matrix.org", exclusive=True)
170183
)
171184
self.assertTrue(self.service.is_exclusive_user("@irc_foobar:matrix.org"))
172185

173-
def test_exclusive_room(self):
186+
def test_exclusive_room(self) -> None:
174187
self.service.namespaces[ApplicationService.NS_ROOMS].append(
175188
_regex("!irc_.*:matrix.org", exclusive=True)
176189
)
177190
self.assertTrue(self.service.is_exclusive_room("!irc_foobar:matrix.org"))
178191

179192
@defer.inlineCallbacks
180-
def test_regex_alias_no_match(self):
193+
def test_regex_alias_no_match(
194+
self,
195+
) -> Generator["defer.Deferred[object]", object, None]:
181196
self.service.namespaces[ApplicationService.NS_ALIASES].append(
182197
_regex("#irc_.*:matrix.org")
183198
)
@@ -196,7 +211,9 @@ def test_regex_alias_no_match(self):
196211
)
197212

198213
@defer.inlineCallbacks
199-
def test_regex_multiple_matches(self):
214+
def test_regex_multiple_matches(
215+
self,
216+
) -> Generator["defer.Deferred[object]", object, None]:
200217
self.service.namespaces[ApplicationService.NS_ALIASES].append(
201218
_regex("#irc_.*:matrix.org")
202219
)
@@ -215,7 +232,9 @@ def test_regex_multiple_matches(self):
215232
)
216233

217234
@defer.inlineCallbacks
218-
def test_interested_in_self(self):
235+
def test_interested_in_self(
236+
self,
237+
) -> Generator["defer.Deferred[object]", object, None]:
219238
# make sure invites get through
220239
self.service.sender = "@appservice:name"
221240
self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
@@ -233,7 +252,9 @@ def test_interested_in_self(self):
233252
)
234253

235254
@defer.inlineCallbacks
236-
def test_member_list_match(self):
255+
def test_member_list_match(
256+
self,
257+
) -> Generator["defer.Deferred[object]", object, None]:
237258
self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
238259
# Note that @irc_fo:here is the AS user.
239260
self.store.get_local_users_in_room = simple_async_mock(

0 commit comments

Comments
 (0)