From 90683a5805cb4877b3a2e72964ee5cc73c645f9e Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 24 Nov 2020 21:57:24 +0000 Subject: [PATCH 1/4] Add typing and remove unnecessary arguments from Replication class methods --- synapse/replication/http/membership.py | 43 +++++++++++++------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/synapse/replication/http/membership.py b/synapse/replication/http/membership.py index f0c37eaf5e1b..195458c6deea 100644 --- a/synapse/replication/http/membership.py +++ b/synapse/replication/http/membership.py @@ -12,9 +12,10 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - import logging -from typing import TYPE_CHECKING, Optional +from typing import TYPE_CHECKING, List, Optional + +from twisted.web.http import Request from synapse.http.servlet import parse_json_object_from_request from synapse.replication.http._base import ReplicationEndpoint @@ -52,16 +53,14 @@ def __init__(self, hs): self.clock = hs.get_clock() @staticmethod - async def _serialize_payload( - requester, room_id, user_id, remote_room_hosts, content + async def _serialize_payload( # type: ignore + requester: Requester, remote_room_hosts: List[str], content: JsonDict, ): """ Args: - requester(Requester) - room_id (str) - user_id (str) - remote_room_hosts (list[str]): Servers to try and join via - content(dict): The event content to use for the join event + requester: the user making the request according to the access token + remote_room_hosts: Servers to try and join via + content: The event content to use for the join event """ return { "requester": requester.serialize(), @@ -69,7 +68,9 @@ async def _serialize_payload( "content": content, } - async def _handle_request(self, request, room_id, user_id): + async def _handle_request( # type: ignore + self, request: Request, room_id: str, user_id: str + ): content = parse_json_object_from_request(request) remote_room_hosts = content["remote_room_hosts"] @@ -114,14 +115,10 @@ def __init__(self, hs: "HomeServer"): @staticmethod async def _serialize_payload( # type: ignore - invite_event_id: str, - txn_id: Optional[str], - requester: Requester, - content: JsonDict, + txn_id: Optional[str], requester: Requester, content: JsonDict, ): """ Args: - invite_event_id: ID of the invite to be rejected txn_id: optional transaction ID supplied by the client requester: user making the rejection request, according to the access token content: additional content to include in the rejection event. @@ -133,7 +130,9 @@ async def _serialize_payload( # type: ignore "content": content, } - async def _handle_request(self, request, invite_event_id): + async def _handle_request( # type: ignore + self, request: Request, invite_event_id: str + ): content = parse_json_object_from_request(request) txn_id = content["txn_id"] @@ -174,18 +173,20 @@ def __init__(self, hs): self.distributor = hs.get_distributor() @staticmethod - async def _serialize_payload(room_id, user_id, change): + async def _serialize_payload( # type: ignore + change: str, + ): """ Args: - room_id (str) - user_id (str) - change (str): "left" + change: "left" """ assert change == "left" return {} - def _handle_request(self, request, room_id, user_id, change): + def _handle_request( # type: ignore + self, request: Request, room_id: str, user_id: str, change: str, + ): logger.info("user membership change: %s in %s", user_id, room_id) user = UserID.from_string(user_id) From 175294e60346f9c1c5b5b87972bc65536d650d0e Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 24 Nov 2020 22:05:44 +0000 Subject: [PATCH 2/4] Changelog --- changelog.d/8809.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/8809.misc diff --git a/changelog.d/8809.misc b/changelog.d/8809.misc new file mode 100644 index 000000000000..bbf83cf18d6a --- /dev/null +++ b/changelog.d/8809.misc @@ -0,0 +1 @@ +Remove unnecessary function arguments and add typing to several membership replication classes. \ No newline at end of file From ad13f9f03c633d8caf4cac007b103e53f3a6726f Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 26 Nov 2020 15:45:19 +0000 Subject: [PATCH 3/4] Add arguments to _serialize_payload back. Add missing return types --- synapse/replication/http/membership.py | 49 ++++++++++++++++++-------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/synapse/replication/http/membership.py b/synapse/replication/http/membership.py index 195458c6deea..fa2bbd2972ba 100644 --- a/synapse/replication/http/membership.py +++ b/synapse/replication/http/membership.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import logging -from typing import TYPE_CHECKING, List, Optional +from typing import TYPE_CHECKING, List, Optional, Tuple from twisted.web.http import Request @@ -54,13 +54,22 @@ def __init__(self, hs): @staticmethod async def _serialize_payload( # type: ignore - requester: Requester, remote_room_hosts: List[str], content: JsonDict, - ): + requester: Requester, + room_id: str, + user_id: str, + remote_room_hosts: List[str], + content: JsonDict, + ) -> JsonDict: """ Args: - requester: the user making the request according to the access token + requester: The user making the request according to the access token + room_id: The ID of the room. + user_id: The ID of the user. remote_room_hosts: Servers to try and join via content: The event content to use for the join event + + Returns: + A dict representing the payload of the request. """ return { "requester": requester.serialize(), @@ -70,7 +79,7 @@ async def _serialize_payload( # type: ignore async def _handle_request( # type: ignore self, request: Request, room_id: str, user_id: str - ): + ) -> Tuple[int, JsonDict]: content = parse_json_object_from_request(request) remote_room_hosts = content["remote_room_hosts"] @@ -115,14 +124,21 @@ def __init__(self, hs: "HomeServer"): @staticmethod async def _serialize_payload( # type: ignore - txn_id: Optional[str], requester: Requester, content: JsonDict, - ): + invite_event_id: str, + txn_id: Optional[str], + requester: Requester, + content: JsonDict, + ) -> JsonDict: """ Args: - txn_id: optional transaction ID supplied by the client - requester: user making the rejection request, according to the access token - content: additional content to include in the rejection event. + invite_event_id: The ID of the invite to be rejected. + txn_id: Optional transaction ID supplied by the client + requester: User making the rejection request, according to the access token + content: Additional content to include in the rejection event. Normally an empty dict. + + Returns: + A dict representing the payload of the request. """ return { "txn_id": txn_id, @@ -132,7 +148,7 @@ async def _serialize_payload( # type: ignore async def _handle_request( # type: ignore self, request: Request, invite_event_id: str - ): + ) -> Tuple[int, JsonDict]: content = parse_json_object_from_request(request) txn_id = content["txn_id"] @@ -174,11 +190,16 @@ def __init__(self, hs): @staticmethod async def _serialize_payload( # type: ignore - change: str, - ): + room_id: str, user_id: str, change: str, + ) -> JsonDict: """ Args: + room_id: The ID of the room. + user_id: The ID of the user. change: "left" + + Returns: + A dict representing the payload of the request. """ assert change == "left" @@ -186,7 +207,7 @@ async def _serialize_payload( # type: ignore def _handle_request( # type: ignore self, request: Request, room_id: str, user_id: str, change: str, - ): + ) -> Tuple[int, JsonDict]: logger.info("user membership change: %s in %s", user_id, room_id) user = UserID.from_string(user_id) From 377673db2ac4b4af6a237252dc39cf464cfb3a30 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 26 Nov 2020 15:47:16 +0000 Subject: [PATCH 4/4] Drop some commas --- synapse/replication/http/membership.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/replication/http/membership.py b/synapse/replication/http/membership.py index fa2bbd2972ba..84e002f93448 100644 --- a/synapse/replication/http/membership.py +++ b/synapse/replication/http/membership.py @@ -190,7 +190,7 @@ def __init__(self, hs): @staticmethod async def _serialize_payload( # type: ignore - room_id: str, user_id: str, change: str, + room_id: str, user_id: str, change: str ) -> JsonDict: """ Args: @@ -206,7 +206,7 @@ async def _serialize_payload( # type: ignore return {} def _handle_request( # type: ignore - self, request: Request, room_id: str, user_id: str, change: str, + self, request: Request, room_id: str, user_id: str, change: str ) -> Tuple[int, JsonDict]: logger.info("user membership change: %s in %s", user_id, room_id)