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

Add missing type hints for tests.unittest. #13397

Merged
merged 11 commits into from
Jul 27, 2022
32 changes: 17 additions & 15 deletions tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
from synapse.rest import RegisterServletsFunc
from synapse.server import HomeServer
from synapse.storage.keys import FetchKeyResult
from synapse.types import JsonDict, UserID, create_requester
from synapse.types import JsonDict, Requester, UserID, create_requester
from synapse.util import Clock
from synapse.util.httpresourcetree import create_resource_tree

Expand Down Expand Up @@ -171,7 +171,7 @@ def tearDown(orig: Callable[[], R]) -> R:

return ret

def assertObjectHasAttributes(self, attrs, obj):
def assertObjectHasAttributes(self, attrs: Dict[str, object], obj: object) -> None:
"""Asserts that the given object has each of the attributes given, and
that the value of each matches according to assertEqual."""
for key in attrs.keys():
Expand Down Expand Up @@ -259,7 +259,7 @@ def __init__(self, methodName: str):
method = getattr(self, methodName)
self._extra_config = getattr(method, "_extra_config", None)

def setUp(self):
def setUp(self) -> None:
"""
Set up the TestCase by calling the homeserver constructor, optionally
hijacking the authentication system to return a fixed user, and then
Expand Down Expand Up @@ -310,15 +310,21 @@ def setUp(self):
)
)

async def get_user_by_access_token(token=None, allow_guest=False):
async def get_user_by_access_token(
token: Optional[str] = None, allow_guest: bool = False
) -> JsonDict:
assert self.helper.auth_user_id is not None
return {
"user": UserID.from_string(self.helper.auth_user_id),
"token_id": token_id,
"is_guest": False,
}

async def get_user_by_req(request, allow_guest=False):
async def get_user_by_req(
request: SynapseRequest,
allow_guest: bool = False,
allow_expired: bool = False,
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
) -> Requester:
assert self.helper.auth_user_id is not None
return create_requester(
UserID.from_string(self.helper.auth_user_id),
Expand All @@ -343,11 +349,11 @@ async def get_user_by_req(request, allow_guest=False):
if hasattr(self, "prepare"):
self.prepare(self.reactor, self.clock, self.hs)

def tearDown(self):
def tearDown(self) -> None:
# Reset to not use frozen dicts.
events.USE_FROZEN_DICTS = False

def wait_on_thread(self, deferred, timeout=10):
def wait_on_thread(self, deferred: Deferred, timeout: int = 10) -> None:
"""
Wait until a Deferred is done, where it's waiting on a real thread.
"""
Expand Down Expand Up @@ -378,7 +384,7 @@ def make_homeserver(self, reactor, clock):
clock (synapse.util.Clock): The Clock, associated with the reactor.

Returns:
A homeserver (synapse.server.HomeServer) suitable for testing.
A homeserver suitable for testing.

Function to be overridden in subclasses.
"""
Expand Down Expand Up @@ -412,7 +418,7 @@ def create_resource_dict(self) -> Dict[str, Resource]:
"/_synapse/admin": servlet_resource,
}

def default_config(self):
def default_config(self) -> JsonDict:
"""
Get a default HomeServer config dict.
"""
Expand Down Expand Up @@ -525,7 +531,7 @@ def setup_test_homeserver(self, *args: Any, **kwargs: Any) -> HomeServer:
config_obj.parse_config_dict(config, "", "")
kwargs["config"] = config_obj

async def run_bg_updates():
async def run_bg_updates() -> None:
with LoggingContext("run_bg_updates"):
self.get_success(stor.db_pool.updates.run_background_updates(False))

Expand All @@ -544,11 +550,7 @@ def pump(self, by: float = 0.0) -> None:
"""
self.reactor.pump([by] * 100)

def get_success(
self,
d: Awaitable[TV],
by: float = 0.0,
) -> TV:
def get_success(self, d: Awaitable[TV], by: float = 0.0) -> TV:
deferred: Deferred[TV] = ensureDeferred(d) # type: ignore[arg-type]
self.pump(by=by)
return self.successResultOf(deferred)
Expand Down