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

Type hints for tests.appservice #14990

Merged
merged 7 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/14990.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve type hints.
4 changes: 3 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ exclude = (?x)
|synapse/storage/databases/main/cache.py
|synapse/storage/schema/

|tests/appservice/test_scheduler.py
|tests/federation/test_federation_catch_up.py
|tests/federation/test_federation_sender.py
|tests/http/federation/test_matrix_federation_agent.py
Expand Down Expand Up @@ -78,6 +77,9 @@ disallow_untyped_defs = True
[mypy-tests.app.*]
disallow_untyped_defs = True

[mypy-tests.appservice.*]
disallow_untyped_defs = True

[mypy-tests.config.*]
disallow_untyped_defs = True

Expand Down
4 changes: 2 additions & 2 deletions synapse/appservice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import logging
import re
from enum import Enum
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Pattern
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Pattern, Sequence

import attr
from netaddr import IPSet
Expand Down Expand Up @@ -377,7 +377,7 @@ def __init__(
self,
service: ApplicationService,
id: int,
events: List[EventBase],
events: Sequence[EventBase],
ephemeral: List[JsonDict],
to_device_messages: List[JsonDict],
one_time_keys_count: TransactionOneTimeKeysCount,
Expand Down
14 changes: 12 additions & 2 deletions synapse/appservice/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@
# limitations under the License.
import logging
import urllib.parse
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Mapping, Optional, Tuple
from typing import (
TYPE_CHECKING,
Any,
Dict,
Iterable,
List,
Mapping,
Optional,
Sequence,
Tuple,
)

from prometheus_client import Counter
from typing_extensions import TypeGuard
Expand Down Expand Up @@ -259,7 +269,7 @@ async def _get() -> Optional[JsonDict]:
async def push_bulk(
self,
service: "ApplicationService",
events: List[EventBase],
events: Sequence[EventBase],
ephemeral: List[JsonDict],
to_device_messages: List[JsonDict],
one_time_keys_count: TransactionOneTimeKeysCount,
Expand Down
3 changes: 2 additions & 1 deletion synapse/appservice/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
Iterable,
List,
Optional,
Sequence,
Set,
Tuple,
)
Expand Down Expand Up @@ -364,7 +365,7 @@ def __init__(self, clock: Clock, store: DataStore, as_api: ApplicationServiceApi
async def send(
self,
service: ApplicationService,
events: List[EventBase],
events: Sequence[EventBase],
ephemeral: Optional[List[JsonDict]] = None,
to_device_messages: Optional[List[JsonDict]] = None,
one_time_keys_count: Optional[TransactionOneTimeKeysCount] = None,
Expand Down
14 changes: 12 additions & 2 deletions synapse/storage/databases/main/appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@
# limitations under the License.
import logging
import re
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Pattern, Tuple, cast
from typing import (
TYPE_CHECKING,
Any,
Dict,
List,
Optional,
Pattern,
Sequence,
Tuple,
cast,
)

from synapse.appservice import (
ApplicationService,
Expand Down Expand Up @@ -257,7 +267,7 @@ async def set_appservice_state(
async def create_appservice_txn(
self,
service: ApplicationService,
events: List[EventBase],
events: Sequence[EventBase],
ephemeral: List[JsonDict],
to_device_messages: List[JsonDict],
one_time_keys_count: TransactionOneTimeKeysCount,
Expand Down
4 changes: 2 additions & 2 deletions tests/appservice/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


class ApplicationServiceApiTestCase(unittest.HomeserverTestCase):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.api = hs.get_application_service_api()
self.service = ApplicationService(
id="unique_identifier",
Expand All @@ -39,7 +39,7 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer):
hs_token=TOKEN,
)

def test_query_3pe_authenticates_token(self):
def test_query_3pe_authenticates_token(self) -> None:
"""
Tests that 3pe queries to the appservice are authenticated
with the appservice's token.
Expand Down
55 changes: 38 additions & 17 deletions tests/appservice/test_appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import re
from typing import Generator
from unittest.mock import Mock

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


class ApplicationServiceTestCase(unittest.TestCase):
def setUp(self):
def setUp(self) -> None:
self.service = ApplicationService(
id="unique_identifier",
sender="@as:test",
Expand All @@ -46,7 +47,9 @@ def setUp(self):
self.store.get_local_users_in_room = simple_async_mock([])

@defer.inlineCallbacks
def test_regex_user_id_prefix_match(self):
def test_regex_user_id_prefix_match(
self,
) -> Generator["defer.Deferred[object]", object, None]:
self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
self.event.sender = "@irc_foobar:matrix.org"
self.assertTrue(
Expand All @@ -60,7 +63,9 @@ def test_regex_user_id_prefix_match(self):
)

@defer.inlineCallbacks
def test_regex_user_id_prefix_no_match(self):
def test_regex_user_id_prefix_no_match(
self,
) -> Generator["defer.Deferred[object]", object, None]:
self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
self.event.sender = "@someone_else:matrix.org"
self.assertFalse(
Expand All @@ -74,7 +79,9 @@ def test_regex_user_id_prefix_no_match(self):
)

@defer.inlineCallbacks
def test_regex_room_member_is_checked(self):
def test_regex_room_member_is_checked(
self,
) -> Generator["defer.Deferred[object]", object, None]:
self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
self.event.sender = "@someone_else:matrix.org"
self.event.type = "m.room.member"
Expand All @@ -90,7 +97,9 @@ def test_regex_room_member_is_checked(self):
)

@defer.inlineCallbacks
def test_regex_room_id_match(self):
def test_regex_room_id_match(
self,
) -> Generator["defer.Deferred[object]", object, None]:
self.service.namespaces[ApplicationService.NS_ROOMS].append(
_regex("!some_prefix.*some_suffix:matrix.org")
)
Expand All @@ -106,7 +115,9 @@ def test_regex_room_id_match(self):
)

@defer.inlineCallbacks
def test_regex_room_id_no_match(self):
def test_regex_room_id_no_match(
self,
) -> Generator["defer.Deferred[object]", object, None]:
self.service.namespaces[ApplicationService.NS_ROOMS].append(
_regex("!some_prefix.*some_suffix:matrix.org")
)
Expand All @@ -122,7 +133,9 @@ def test_regex_room_id_no_match(self):
)

@defer.inlineCallbacks
def test_regex_alias_match(self):
def test_regex_alias_match(
self,
) -> Generator["defer.Deferred[object]", object, None]:
self.service.namespaces[ApplicationService.NS_ALIASES].append(
_regex("#irc_.*:matrix.org")
)
Expand All @@ -140,44 +153,46 @@ def test_regex_alias_match(self):
)
)

def test_non_exclusive_alias(self):
def test_non_exclusive_alias(self) -> None:
self.service.namespaces[ApplicationService.NS_ALIASES].append(
_regex("#irc_.*:matrix.org", exclusive=False)
)
self.assertFalse(self.service.is_exclusive_alias("#irc_foobar:matrix.org"))

def test_non_exclusive_room(self):
def test_non_exclusive_room(self) -> None:
self.service.namespaces[ApplicationService.NS_ROOMS].append(
_regex("!irc_.*:matrix.org", exclusive=False)
)
self.assertFalse(self.service.is_exclusive_room("!irc_foobar:matrix.org"))

def test_non_exclusive_user(self):
def test_non_exclusive_user(self) -> None:
self.service.namespaces[ApplicationService.NS_USERS].append(
_regex("@irc_.*:matrix.org", exclusive=False)
)
self.assertFalse(self.service.is_exclusive_user("@irc_foobar:matrix.org"))

def test_exclusive_alias(self):
def test_exclusive_alias(self) -> None:
self.service.namespaces[ApplicationService.NS_ALIASES].append(
_regex("#irc_.*:matrix.org", exclusive=True)
)
self.assertTrue(self.service.is_exclusive_alias("#irc_foobar:matrix.org"))

def test_exclusive_user(self):
def test_exclusive_user(self) -> None:
self.service.namespaces[ApplicationService.NS_USERS].append(
_regex("@irc_.*:matrix.org", exclusive=True)
)
self.assertTrue(self.service.is_exclusive_user("@irc_foobar:matrix.org"))

def test_exclusive_room(self):
def test_exclusive_room(self) -> None:
self.service.namespaces[ApplicationService.NS_ROOMS].append(
_regex("!irc_.*:matrix.org", exclusive=True)
)
self.assertTrue(self.service.is_exclusive_room("!irc_foobar:matrix.org"))

@defer.inlineCallbacks
def test_regex_alias_no_match(self):
def test_regex_alias_no_match(
self,
) -> Generator["defer.Deferred[object]", object, None]:
self.service.namespaces[ApplicationService.NS_ALIASES].append(
_regex("#irc_.*:matrix.org")
)
Expand All @@ -196,7 +211,9 @@ def test_regex_alias_no_match(self):
)

@defer.inlineCallbacks
def test_regex_multiple_matches(self):
def test_regex_multiple_matches(
self,
) -> Generator["defer.Deferred[object]", object, None]:
self.service.namespaces[ApplicationService.NS_ALIASES].append(
_regex("#irc_.*:matrix.org")
)
Expand All @@ -215,7 +232,9 @@ def test_regex_multiple_matches(self):
)

@defer.inlineCallbacks
def test_interested_in_self(self):
def test_interested_in_self(
self,
) -> Generator["defer.Deferred[object]", object, None]:
# make sure invites get through
self.service.sender = "@appservice:name"
self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
Expand All @@ -233,7 +252,9 @@ def test_interested_in_self(self):
)

@defer.inlineCallbacks
def test_member_list_match(self):
def test_member_list_match(
self,
) -> Generator["defer.Deferred[object]", object, None]:
self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
# Note that @irc_fo:here is the AS user.
self.store.get_local_users_in_room = simple_async_mock(
Expand Down
Loading