0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-12-30 03:14:10 +01:00

Fix typing annotations in synapse/federation (#7382)

We're pretty close to having mypy working for `synapse.federation`, so let's
finish the job.
This commit is contained in:
Richard van der Hoff 2020-05-05 14:27:13 +01:00 committed by GitHub
parent d5aa7d93ed
commit 16b1a34e80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 21 deletions

View file

@ -1 +1 @@
Add typing information to federation server code. Add typing annotations in `synapse.federation`.

1
changelog.d/7382.misc Normal file
View file

@ -0,0 +1 @@
Add typing annotations in `synapse.federation`.

View file

@ -31,7 +31,7 @@ Events are replicated via a separate events stream.
import logging import logging
from collections import namedtuple from collections import namedtuple
from typing import List, Tuple from typing import Dict, List, Tuple, Type
from six import iteritems from six import iteritems
@ -57,25 +57,35 @@ class FederationRemoteSendQueue(object):
self.notifier = hs.get_notifier() self.notifier = hs.get_notifier()
self.is_mine_id = hs.is_mine_id self.is_mine_id = hs.is_mine_id
self.presence_map = {} # Pending presence map user_id -> UserPresenceState # Pending presence map user_id -> UserPresenceState
self.presence_changed = SortedDict() # Stream position -> list[user_id] self.presence_map = {} # type: Dict[str, UserPresenceState]
# Stream position -> list[user_id]
self.presence_changed = SortedDict() # type: SortedDict[int, List[str]]
# Stores the destinations we need to explicitly send presence to about a # Stores the destinations we need to explicitly send presence to about a
# given user. # given user.
# Stream position -> (user_id, destinations) # Stream position -> (user_id, destinations)
self.presence_destinations = SortedDict() self.presence_destinations = (
SortedDict()
) # type: SortedDict[int, Tuple[str, List[str]]]
self.keyed_edu = {} # (destination, key) -> EDU # (destination, key) -> EDU
self.keyed_edu_changed = SortedDict() # stream position -> (destination, key) self.keyed_edu = {} # type: Dict[Tuple[str, tuple], Edu]
self.edus = SortedDict() # stream position -> Edu # stream position -> (destination, key)
self.keyed_edu_changed = (
SortedDict()
) # type: SortedDict[int, Tuple[str, tuple]]
self.edus = SortedDict() # type: SortedDict[int, Edu]
# stream ID for the next entry into presence_changed/keyed_edu_changed/edus. # stream ID for the next entry into presence_changed/keyed_edu_changed/edus.
self.pos = 1 self.pos = 1
# map from stream ID to the time that stream entry was generated, so that we # map from stream ID to the time that stream entry was generated, so that we
# can clear out entries after a while # can clear out entries after a while
self.pos_time = SortedDict() self.pos_time = SortedDict() # type: SortedDict[int, int]
# EVERYTHING IS SAD. In particular, python only makes new scopes when # EVERYTHING IS SAD. In particular, python only makes new scopes when
# we make a new function, so we need to make a new function so the inner # we make a new function, so we need to make a new function so the inner
@ -163,8 +173,10 @@ class FederationRemoteSendQueue(object):
for edu_key in self.keyed_edu_changed.values(): for edu_key in self.keyed_edu_changed.values():
live_keys.add(edu_key) live_keys.add(edu_key)
to_del = [edu_key for edu_key in self.keyed_edu if edu_key not in live_keys] keys_to_del = [
for edu_key in to_del: edu_key for edu_key in self.keyed_edu if edu_key not in live_keys
]
for edu_key in keys_to_del:
del self.keyed_edu[edu_key] del self.keyed_edu[edu_key]
# Delete things out of edu map # Delete things out of edu map
@ -349,7 +361,7 @@ class BaseFederationRow(object):
Specifies how to identify, serialize and deserialize the different types. Specifies how to identify, serialize and deserialize the different types.
""" """
TypeId = None # Unique string that ids the type. Must be overriden in sub classes. TypeId = "" # Unique string that ids the type. Must be overriden in sub classes.
@staticmethod @staticmethod
def from_data(data): def from_data(data):
@ -462,10 +474,14 @@ class EduRow(BaseFederationRow, namedtuple("EduRow", ("edu",))): # Edu
buff.edus.setdefault(self.edu.destination, []).append(self.edu) buff.edus.setdefault(self.edu.destination, []).append(self.edu)
TypeToRow = { _rowtypes = (
Row.TypeId: Row PresenceRow,
for Row in (PresenceRow, PresenceDestinationsRow, KeyedEduRow, EduRow,) PresenceDestinationsRow,
} KeyedEduRow,
EduRow,
) # type: Tuple[Type[BaseFederationRow], ...]
TypeToRow = {Row.TypeId: Row for Row in _rowtypes}
ParsedFederationStreamData = namedtuple( ParsedFederationStreamData = namedtuple(

View file

@ -181,11 +181,7 @@ commands = mypy \
synapse/appservice \ synapse/appservice \
synapse/config \ synapse/config \
synapse/events/spamcheck.py \ synapse/events/spamcheck.py \
synapse/federation/federation_base.py \ synapse/federation \
synapse/federation/federation_client.py \
synapse/federation/federation_server.py \
synapse/federation/sender \
synapse/federation/transport \
synapse/handlers/auth.py \ synapse/handlers/auth.py \
synapse/handlers/cas_handler.py \ synapse/handlers/cas_handler.py \
synapse/handlers/directory.py \ synapse/handlers/directory.py \