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

Commit 3bd5d4c

Browse files
committed
Add some type annotations to the federation base & client classes (#6995)
* commit '12d425900': Add some type annotations to the federation base & client classes (#6995)
2 parents 4c75c20 + 12d4259 commit 3bd5d4c

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

changelog.d/6995.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add some type annotations to the federation base & client classes.

synapse/federation/federation_base.py

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
# limitations under the License.
1616
import logging
1717
from collections import namedtuple
18+
from typing import Iterable, List
1819

1920
import six
2021

2122
from twisted.internet import defer
22-
from twisted.internet.defer import DeferredList
23+
from twisted.internet.defer import Deferred, DeferredList
24+
from twisted.python.failure import Failure
2325

2426
from synapse.api.constants import MAX_DEPTH, EventTypes, Membership
2527
from synapse.api.errors import Codes, SynapseError
@@ -29,6 +31,7 @@
2931
RoomVersion,
3032
)
3133
from synapse.crypto.event_signing import check_event_content_hash
34+
from synapse.crypto.keyring import Keyring
3235
from synapse.events import EventBase, make_event_from_dict
3336
from synapse.events.utils import prune_event
3437
from synapse.http.servlet import assert_params_in_dict
@@ -56,7 +59,12 @@ def __init__(self, hs):
5659

5760
@defer.inlineCallbacks
5861
def _check_sigs_and_hash_and_fetch(
59-
self, origin, pdus, room_version, outlier=False, include_none=False
62+
self,
63+
origin: str,
64+
pdus: List[EventBase],
65+
room_version: str,
66+
outlier: bool = False,
67+
include_none: bool = False,
6068
):
6169
"""Takes a list of PDUs and checks the signatures and hashs of each
6270
one. If a PDU fails its signature check then we check if we have it in
@@ -69,11 +77,11 @@ def _check_sigs_and_hash_and_fetch(
6977
a new list.
7078
7179
Args:
72-
origin (str)
73-
pdu (list)
74-
room_version (str)
75-
outlier (bool): Whether the events are outliers or not
76-
include_none (str): Whether to include None in the returned list
80+
origin
81+
pdu
82+
room_version
83+
outlier: Whether the events are outliers or not
84+
include_none: Whether to include None in the returned list
7785
for events that have failed their checks
7886
7987
Returns:
@@ -82,7 +90,7 @@ def _check_sigs_and_hash_and_fetch(
8290
deferreds = self._check_sigs_and_hashes(room_version, pdus)
8391

8492
@defer.inlineCallbacks
85-
def handle_check_result(pdu, deferred):
93+
def handle_check_result(pdu: EventBase, deferred: Deferred):
8694
try:
8795
res = yield make_deferred_yieldable(deferred)
8896
except SynapseError:
@@ -96,8 +104,10 @@ def handle_check_result(pdu, deferred):
96104

97105
if not res and pdu.origin != origin:
98106
try:
107+
# This should not exist in the base implementation, until
108+
# this is fixed, ignore it for typing. See issue #6997.
99109
res = yield defer.ensureDeferred(
100-
self.get_pdu(
110+
self.get_pdu( # type: ignore
101111
destinations=[pdu.origin],
102112
event_id=pdu.event_id,
103113
room_version=room_version,
@@ -127,21 +137,23 @@ def handle_check_result(pdu, deferred):
127137
else:
128138
return [p for p in valid_pdus if p]
129139

130-
def _check_sigs_and_hash(self, room_version, pdu):
140+
def _check_sigs_and_hash(self, room_version: str, pdu: EventBase) -> Deferred:
131141
return make_deferred_yieldable(
132142
self._check_sigs_and_hashes(room_version, [pdu])[0]
133143
)
134144

135-
def _check_sigs_and_hashes(self, room_version, pdus):
145+
def _check_sigs_and_hashes(
146+
self, room_version: str, pdus: List[EventBase]
147+
) -> List[Deferred]:
136148
"""Checks that each of the received events is correctly signed by the
137149
sending server.
138150
139151
Args:
140-
room_version (str): The room version of the PDUs
141-
pdus (list[FrozenEvent]): the events to be checked
152+
room_version: The room version of the PDUs
153+
pdus: the events to be checked
142154
143155
Returns:
144-
list[Deferred]: for each input event, a deferred which:
156+
For each input event, a deferred which:
145157
* returns the original event if the checks pass
146158
* returns a redacted version of the event (if the signature
147159
matched but the hash did not)
@@ -152,7 +164,7 @@ def _check_sigs_and_hashes(self, room_version, pdus):
152164

153165
ctx = LoggingContext.current_context()
154166

155-
def callback(_, pdu):
167+
def callback(_, pdu: EventBase):
156168
with PreserveLoggingContext(ctx):
157169
if not check_event_content_hash(pdu):
158170
# let's try to distinguish between failures because the event was
@@ -189,7 +201,7 @@ def callback(_, pdu):
189201

190202
return pdu
191203

192-
def errback(failure, pdu):
204+
def errback(failure: Failure, pdu: EventBase):
193205
failure.trap(SynapseError)
194206
with PreserveLoggingContext(ctx):
195207
logger.warning(
@@ -215,16 +227,18 @@ class PduToCheckSig(
215227
pass
216228

217229

218-
def _check_sigs_on_pdus(keyring, room_version, pdus):
230+
def _check_sigs_on_pdus(
231+
keyring: Keyring, room_version: str, pdus: Iterable[EventBase]
232+
) -> List[Deferred]:
219233
"""Check that the given events are correctly signed
220234
221235
Args:
222-
keyring (synapse.crypto.Keyring): keyring object to do the checks
223-
room_version (str): the room version of the PDUs
224-
pdus (Collection[EventBase]): the events to be checked
236+
keyring: keyring object to do the checks
237+
room_version: the room version of the PDUs
238+
pdus: the events to be checked
225239
226240
Returns:
227-
List[Deferred]: a Deferred for each event in pdus, which will either succeed if
241+
A Deferred for each event in pdus, which will either succeed if
228242
the signatures are valid, or fail (with a SynapseError) if not.
229243
"""
230244

@@ -329,7 +343,7 @@ def event_err(e, pdu_to_check):
329343
return [_flatten_deferred_list(p.deferreds) for p in pdus_to_check]
330344

331345

332-
def _flatten_deferred_list(deferreds):
346+
def _flatten_deferred_list(deferreds: List[Deferred]) -> Deferred:
333347
"""Given a list of deferreds, either return the single deferred,
334348
combine into a DeferredList, or return an already resolved deferred.
335349
"""
@@ -341,7 +355,7 @@ def _flatten_deferred_list(deferreds):
341355
return defer.succeed(None)
342356

343357

344-
def _is_invite_via_3pid(event):
358+
def _is_invite_via_3pid(event: EventBase) -> bool:
345359
return (
346360
event.type == EventTypes.Member
347361
and event.membership == Membership.INVITE

synapse/federation/federation_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def claim_client_keys(self, destination, content, timeout):
187187

188188
async def backfill(
189189
self, dest: str, room_id: str, limit: int, extremities: Iterable[str]
190-
) -> List[EventBase]:
190+
) -> Optional[List[EventBase]]:
191191
"""Requests some more historic PDUs for the given room from the
192192
given destination server.
193193
@@ -199,9 +199,9 @@ async def backfill(
199199
"""
200200
logger.debug("backfill extrem=%s", extremities)
201201

202-
# If there are no extremeties then we've (probably) reached the start.
202+
# If there are no extremities then we've (probably) reached the start.
203203
if not extremities:
204-
return
204+
return None
205205

206206
transaction_data = await self.transport_layer.backfill(
207207
dest, room_id, extremities, limit
@@ -284,7 +284,7 @@ async def get_pdu(
284284
pdu_list = [
285285
event_from_pdu_json(p, room_version, outlier=outlier)
286286
for p in transaction_data["pdus"]
287-
]
287+
] # type: List[EventBase]
288288

289289
if pdu_list and pdu_list[0]:
290290
pdu = pdu_list[0]
@@ -615,7 +615,7 @@ async def send_request(destination) -> Dict[str, Any]:
615615
]
616616
if auth_chain_create_events != [create_event.event_id]:
617617
raise InvalidResponseError(
618-
"Unexpected create event(s) in auth chain"
618+
"Unexpected create event(s) in auth chain: %s"
619619
% (auth_chain_create_events,)
620620
)
621621

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ commands = mypy \
181181
synapse/appservice \
182182
synapse/config \
183183
synapse/events/spamcheck.py \
184+
synapse/federation/federation_base.py \
185+
synapse/federation/federation_client.py \
184186
synapse/federation/sender \
185187
synapse/federation/transport \
186188
synapse/handlers/presence.py \

0 commit comments

Comments
 (0)