Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
4 changes: 4 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ files =
synapse/storage/database.py,
synapse/storage/engines,
synapse/storage/persist_events.py,
synapse/storage/push_rule.py,
synapse/storage/relations.py,
synapse/storage/roommember.py,
synapse/storage/state.py,
synapse/storage/types.py,
synapse/storage/util,
synapse/streams,
synapse/types.py,
Expand Down
44 changes: 23 additions & 21 deletions synapse/storage/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
# limitations under the License.

import logging
from typing import Any, Dict, List, Optional, Tuple

import attr

from synapse.api.errors import SynapseError
from synapse.types import JsonDict

logger = logging.getLogger(__name__)

Expand All @@ -27,18 +29,18 @@ class PaginationChunk:
"""Returned by relation pagination APIs.

Attributes:
chunk (list): The rows returned by pagination
next_batch (Any|None): Token to fetch next set of results with, if
chunk: The rows returned by pagination
next_batch: Token to fetch next set of results with, if
None then there are no more results.
prev_batch (Any|None): Token to fetch previous set of results with, if
prev_batch: Token to fetch previous set of results with, if
None then there are no previous results.
"""

chunk = attr.ib()
next_batch = attr.ib(default=None)
prev_batch = attr.ib(default=None)
chunk = attr.ib(type=List[JsonDict])
next_batch = attr.ib(type=Optional[Any], default=None)
prev_batch = attr.ib(type=Optional[Any], default=None)

def to_dict(self):
def to_dict(self) -> Dict[str, Any]:
d = {"chunk": self.chunk}

if self.next_batch:
Expand All @@ -59,25 +61,25 @@ class RelationPaginationToken:
boundaries of the chunk as pagination tokens.

Attributes:
topological (int): The topological ordering of the boundary event
stream (int): The stream ordering of the boundary event.
topological: The topological ordering of the boundary event
stream: The stream ordering of the boundary event.
"""

topological = attr.ib()
stream = attr.ib()
topological = attr.ib(type=int)
stream = attr.ib(type=int)

@staticmethod
def from_string(string):
def from_string(string: str) -> "RelationPaginationToken":
try:
t, s = string.split("-")
return RelationPaginationToken(int(t), int(s))
except ValueError:
raise SynapseError(400, "Invalid token")

def to_string(self):
def to_string(self) -> str:
return "%d-%d" % (self.topological, self.stream)

def as_tuple(self):
def as_tuple(self) -> Tuple[Any, ...]:
return attr.astuple(self)


Expand All @@ -89,23 +91,23 @@ class AggregationPaginationToken:
aggregation groups, we can just use them as our pagination token.

Attributes:
count (int): The count of relations in the boundar group.
stream (int): The MAX stream ordering in the boundary group.
count: The count of relations in the boundar group.
stream: The MAX stream ordering in the boundary group.
"""

count = attr.ib()
stream = attr.ib()
count = attr.ib(type=int)
stream = attr.ib(type=int)

@staticmethod
def from_string(string):
def from_string(string: str) -> "AggregationPaginationToken":
try:
c, s = string.split("-")
return AggregationPaginationToken(int(c), int(s))
except ValueError:
raise SynapseError(400, "Invalid token")

def to_string(self):
def to_string(self) -> str:
return "%d-%d" % (self.count, self.stream)

def as_tuple(self):
def as_tuple(self) -> Tuple[Any, ...]:
return attr.astuple(self)