From 96465b843a7f2078ba1aabb457694903c1603259 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Sat, 21 Jan 2023 09:04:39 +0000 Subject: [PATCH 1/6] Add a TestCaseProtocol class to help type unit test mixins mypy recommends this in order to properly type mixins. The mixin class should inherit from the protocol, and any class using the mixin must provide the properties that the protocol defines. --- tests/unittest.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/unittest.py b/tests/unittest.py index a120c2976ccd..e02ceda80753 100644 --- a/tests/unittest.py +++ b/tests/unittest.py @@ -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. From 065e9f4431739450ae7fe8acae1536638fdff425 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Sat, 21 Jan 2023 09:18:53 +0000 Subject: [PATCH 2/6] Have mixin inherit from HomeserverTestCaseProtocol This stops mypy complaining that KnockingStrippedStateEventHelperMixin doesn't have 'hs' or 'get_success' attributes --- tests/federation/transport/test_knocking.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/federation/transport/test_knocking.py b/tests/federation/transport/test_knocking.py index d21c11b716cd..14b06003e2b0 100644 --- a/tests/federation/transport/test_knocking.py +++ b/tests/federation/transport/test_knocking.py @@ -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): def send_example_state_events_to_room( self, hs: "HomeServer", @@ -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"] ) ) From 3bec53133637f9425f90f5f3a1f9d152bcd2eacb Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Sat, 21 Jan 2023 09:19:39 +0000 Subject: [PATCH 3/6] Remove tests/federation/transport/test_knocking.py from the mypy exclude list --- mypy.ini | 1 - 1 file changed, 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index 468bfe588ccc..660d3d535398 100644 --- a/mypy.ini +++ b/mypy.ini @@ -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 From c1322222ee0ab8a130cd9c804fb32e9b54ad5d84 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Sat, 21 Jan 2023 13:16:58 +0000 Subject: [PATCH 4/6] changelog --- changelog.d/14887.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/14887.misc diff --git a/changelog.d/14887.misc b/changelog.d/14887.misc new file mode 100644 index 000000000000..9f5384e60e78 --- /dev/null +++ b/changelog.d/14887.misc @@ -0,0 +1 @@ +Add missing type hints. \ No newline at end of file From 55428c4fad9e5380668979b9c1229147b8131907 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Sun, 22 Jan 2023 10:56:20 +0100 Subject: [PATCH 5/6] Remove HomeserverTestCaseProtocol and just inherit from HomeserverTestCase --- tests/federation/transport/test_knocking.py | 8 ++------ tests/rest/client/test_sync.py | 2 +- tests/unittest.py | 21 --------------------- 3 files changed, 3 insertions(+), 28 deletions(-) diff --git a/tests/federation/transport/test_knocking.py b/tests/federation/transport/test_knocking.py index 14b06003e2b0..ff589c0b6ca0 100644 --- a/tests/federation/transport/test_knocking.py +++ b/tests/federation/transport/test_knocking.py @@ -23,14 +23,10 @@ from synapse.types import RoomAlias from tests.test_utils import event_injection -from tests.unittest import ( - FederatingHomeserverTestCase, - HomeserverTestCaseProtocol, - TestCase, -) +from tests.unittest import FederatingHomeserverTestCase, HomeserverTestCase -class KnockingStrippedStateEventHelperMixin(TestCase, HomeserverTestCaseProtocol): +class KnockingStrippedStateEventHelperMixin(HomeserverTestCase): def send_example_state_events_to_room( self, hs: "HomeServer", diff --git a/tests/rest/client/test_sync.py b/tests/rest/client/test_sync.py index 0af643ecd97b..b636a5df0d6a 100644 --- a/tests/rest/client/test_sync.py +++ b/tests/rest/client/test_sync.py @@ -295,7 +295,7 @@ def test_sync_backwards_typing(self) -> None: class SyncKnockTestCase( - unittest.HomeserverTestCase, KnockingStrippedStateEventHelperMixin + KnockingStrippedStateEventHelperMixin, unittest.HomeserverTestCase ): servlets = [ synapse.rest.admin.register_servlets, diff --git a/tests/unittest.py b/tests/unittest.py index e02ceda80753..a120c2976ccd 100644 --- a/tests/unittest.py +++ b/tests/unittest.py @@ -223,27 +223,6 @@ 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. From 082b3b4bc970b6cb091703609c2e73e3bb5aae51 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Sun, 22 Jan 2023 11:04:52 +0100 Subject: [PATCH 6/6] Mess around until mypy doesn't emit errors --- tests/rest/client/test_sync.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/rest/client/test_sync.py b/tests/rest/client/test_sync.py index b636a5df0d6a..3b9f4a8ff6f1 100644 --- a/tests/rest/client/test_sync.py +++ b/tests/rest/client/test_sync.py @@ -294,9 +294,7 @@ def test_sync_backwards_typing(self) -> None: self.make_request("GET", sync_url % (access_token, next_batch)) -class SyncKnockTestCase( - KnockingStrippedStateEventHelperMixin, unittest.HomeserverTestCase -): +class SyncKnockTestCase(KnockingStrippedStateEventHelperMixin): servlets = [ synapse.rest.admin.register_servlets, login.register_servlets,