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

Fix type hints in knocking tests #14887

Merged
merged 6 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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/14887.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add missing type hints.
1 change: 0 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ exclude = (?x)
|tests/events/test_utils.py
|tests/federation/test_federation_catch_up.py
|tests/federation/test_federation_sender.py
|tests/federation/transport/test_knocking.py
|tests/handlers/test_typing.py
|tests/http/federation/test_matrix_federation_agent.py
|tests/http/federation/test_srv_resolver.py
Expand Down
10 changes: 7 additions & 3 deletions tests/federation/transport/test_knocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
from synapse.types import RoomAlias

from tests.test_utils import event_injection
from tests.unittest import FederatingHomeserverTestCase, TestCase
from tests.unittest import (
FederatingHomeserverTestCase,
HomeserverTestCaseProtocol,
TestCase,
)


class KnockingStrippedStateEventHelperMixin(TestCase):
class KnockingStrippedStateEventHelperMixin(TestCase, HomeserverTestCaseProtocol):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can make this sub-class HomeserverTestCase instead and not need the protocol.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, indeed you can! Silly mypy docs.

def send_example_state_events_to_room(
self,
hs: "HomeServer",
Expand All @@ -49,7 +53,7 @@ def send_example_state_events_to_room(
# To set a canonical alias, we'll need to point an alias at the room first.
canonical_alias = "#fancy_alias:test"
self.get_success(
self.store.create_room_alias_association(
self.hs.get_datastores().main.create_room_alias_association(
RoomAlias.from_string(canonical_alias), room_id, ["test"]
)
)
Expand Down
21 changes: 21 additions & 0 deletions tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,27 @@ def logcontext_error(msg: str) -> NoReturn:
return patcher(target) # type: ignore[call-overload]


class HomeserverTestCaseProtocol(Protocol):
"""
A protocol type specifying that a class has access to various common
unit test helper methods defined by unittest.TestCase and the classes
that inherit from it. Add more methods or properties below as necessary.

Used for typing mixin classes. See this link for more info:
https://mypy.readthedocs.io/en/stable/protocols.html

TODO: HomeserverTestCase should inherit from this protocol, so that
mypy checks that HomeserverTestCase implements these properties/methods.
Unfortunately this causes mypy to find many type errors in our test
classes, which need to first be fixed.
"""

hs: HomeServer

def get_success(self, d: Awaitable[TV], by: float = 0.0) -> TV:
...


class HomeserverTestCase(TestCase):
"""
A base TestCase that reduces boilerplate for HomeServer-using test cases.
Expand Down