mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-02 12:06:08 -04:00
Convert all namedtuples to attrs. (#11665)
To improve type hints throughout the code.
This commit is contained in:
parent
07a3b5daba
commit
cbd82d0b2d
22 changed files with 231 additions and 206 deletions
|
@ -15,7 +15,6 @@
|
|||
import abc
|
||||
import re
|
||||
import string
|
||||
from collections import namedtuple
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Any,
|
||||
|
@ -227,8 +226,7 @@ class DomainSpecificString(metaclass=abc.ABCMeta):
|
|||
localpart = attr.ib(type=str)
|
||||
domain = attr.ib(type=str)
|
||||
|
||||
# Because this class is a namedtuple of strings and booleans, it is deeply
|
||||
# immutable.
|
||||
# Because this is a frozen class, it is deeply immutable.
|
||||
def __copy__(self):
|
||||
return self
|
||||
|
||||
|
@ -708,16 +706,18 @@ class PersistedEventPosition:
|
|||
return RoomStreamToken(None, self.stream)
|
||||
|
||||
|
||||
class ThirdPartyInstanceID(
|
||||
namedtuple("ThirdPartyInstanceID", ("appservice_id", "network_id"))
|
||||
):
|
||||
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
||||
class ThirdPartyInstanceID:
|
||||
appservice_id: Optional[str]
|
||||
network_id: Optional[str]
|
||||
|
||||
# Deny iteration because it will bite you if you try to create a singleton
|
||||
# set by:
|
||||
# users = set(user)
|
||||
def __iter__(self):
|
||||
raise ValueError("Attempted to iterate a %s" % (type(self).__name__,))
|
||||
|
||||
# Because this class is a namedtuple of strings, it is deeply immutable.
|
||||
# Because this class is a frozen class, it is deeply immutable.
|
||||
def __copy__(self):
|
||||
return self
|
||||
|
||||
|
@ -725,22 +725,18 @@ class ThirdPartyInstanceID(
|
|||
return self
|
||||
|
||||
@classmethod
|
||||
def from_string(cls, s):
|
||||
def from_string(cls, s: str) -> "ThirdPartyInstanceID":
|
||||
bits = s.split("|", 2)
|
||||
if len(bits) != 2:
|
||||
raise SynapseError(400, "Invalid ID %r" % (s,))
|
||||
|
||||
return cls(appservice_id=bits[0], network_id=bits[1])
|
||||
|
||||
def to_string(self):
|
||||
def to_string(self) -> str:
|
||||
return "%s|%s" % (self.appservice_id, self.network_id)
|
||||
|
||||
__str__ = to_string
|
||||
|
||||
@classmethod
|
||||
def create(cls, appservice_id, network_id):
|
||||
return cls(appservice_id=appservice_id, network_id=network_id)
|
||||
|
||||
|
||||
@attr.s(slots=True)
|
||||
class ReadReceipt:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue