Add additional type hints to the storage module. (#8980)

This commit is contained in:
Patrick Cloke 2020-12-30 08:09:53 -05:00 committed by GitHub
parent b8591899ab
commit 637282bb50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 224 additions and 148 deletions

View file

@ -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__)
@ -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:
@ -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)
@ -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 boundary 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)