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

Use auto_attribs/native type hints for attrs classes. #11692

Merged
merged 20 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions synapse/api/room_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,41 @@ class RoomDisposition:
UNSTABLE = "unstable"


@attr.s(slots=True, frozen=True)
@attr.s(slots=True, frozen=True, auto_attribs=True)
class RoomVersion:
"""An object which describes the unique attributes of a room version."""

identifier = attr.ib(type=str) # the identifier for this version
disposition = attr.ib(type=str) # one of the RoomDispositions
event_format = attr.ib(type=int) # one of the EventFormatVersions
state_res = attr.ib(type=int) # one of the StateResolutionVersions
enforce_key_validity = attr.ib(type=bool)
identifier: str # the identifier for this version
disposition: str # one of the RoomDispositions
event_format: int # one of the EventFormatVersions
state_res: int # one of the StateResolutionVersions
enforce_key_validity: bool

# Before MSC2432, m.room.aliases had special auth rules and redaction rules
special_case_aliases_auth = attr.ib(type=bool)
special_case_aliases_auth: bool
# Strictly enforce canonicaljson, do not allow:
# * Integers outside the range of [-2 ^ 53 + 1, 2 ^ 53 - 1]
# * Floats
# * NaN, Infinity, -Infinity
strict_canonicaljson = attr.ib(type=bool)
strict_canonicaljson: bool
# MSC2209: Check 'notifications' key while verifying
# m.room.power_levels auth rules.
limit_notifications_power_levels = attr.ib(type=bool)
limit_notifications_power_levels: bool
# MSC2174/MSC2176: Apply updated redaction rules algorithm.
msc2176_redaction_rules = attr.ib(type=bool)
msc2176_redaction_rules: bool
# MSC3083: Support the 'restricted' join_rule.
msc3083_join_rules = attr.ib(type=bool)
msc3083_join_rules: bool
# MSC3375: Support for the proper redaction rules for MSC3083. This mustn't
# be enabled if MSC3083 is not.
msc3375_redaction_rules = attr.ib(type=bool)
msc3375_redaction_rules: bool
# MSC2403: Allows join_rules to be set to 'knock', changes auth rules to allow sending
# m.room.membership event with membership 'knock'.
msc2403_knocking = attr.ib(type=bool)
msc2403_knocking: bool
# MSC2716: Adds m.room.power_levels -> content.historical field to control
# whether "insertion", "chunk", "marker" events can be sent
msc2716_historical = attr.ib(type=bool)
msc2716_historical: bool
# MSC2716: Adds support for redacting "insertion", "chunk", and "marker" events
msc2716_redactions = attr.ib(type=bool)
msc2716_redactions: bool


class RoomVersions:
Expand Down
77 changes: 38 additions & 39 deletions synapse/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Any,
ClassVar,
Dict,
List,
Mapping,
Match,
MutableMapping,
Expand Down Expand Up @@ -80,7 +81,7 @@ class ISynapseReactor(
"""The interfaces necessary for Synapse to function."""


@attr.s(frozen=True, slots=True)
@attr.s(frozen=True, slots=True, auto_attribs=True)
class Requester:
"""
Represents the user making a request
Expand All @@ -98,13 +99,13 @@ class Requester:
"puppeting" the user.
"""

user = attr.ib(type="UserID")
access_token_id = attr.ib(type=Optional[int])
is_guest = attr.ib(type=bool)
shadow_banned = attr.ib(type=bool)
device_id = attr.ib(type=Optional[str])
app_service = attr.ib(type=Optional["ApplicationService"])
authenticated_entity = attr.ib(type=str)
user: "UserID"
access_token_id: Optional[int]
is_guest: bool
shadow_banned: bool
device_id: Optional[str]
app_service: Optional["ApplicationService"]
authenticated_entity: str

def serialize(self):
"""Converts self to a type that can be serialized as JSON, and then
Expand Down Expand Up @@ -211,7 +212,7 @@ def get_localpart_from_id(string: str) -> str:
DS = TypeVar("DS", bound="DomainSpecificString")


@attr.s(slots=True, frozen=True, repr=False)
@attr.s(slots=True, frozen=True, repr=False, auto_attribs=True)
class DomainSpecificString(metaclass=abc.ABCMeta):
"""Common base class among ID/name strings that have a local part and a
domain name, prefixed with a sigil.
Expand All @@ -224,10 +225,10 @@ class DomainSpecificString(metaclass=abc.ABCMeta):

SIGIL: ClassVar[str] = abc.abstractproperty() # type: ignore

localpart = attr.ib(type=str)
domain = attr.ib(type=str)
localpart: str
domain: str

# Because this is a frozen class, it is deeply immutable.
# Because this is a frozen class, it is deeplyx immutable.
clokep marked this conversation as resolved.
Show resolved Hide resolved
def __copy__(self):
return self

Expand Down Expand Up @@ -461,14 +462,12 @@ class RoomStreamToken:
attributes, must be hashable.
"""

topological = attr.ib(
type=Optional[int],
topological: Optional[int] = attr.ib(
validator=attr.validators.optional(attr.validators.instance_of(int)),
)
stream = attr.ib(type=int, validator=attr.validators.instance_of(int))
stream: int = attr.ib(validator=attr.validators.instance_of(int))

instance_map = attr.ib(
type="frozendict[str, int]",
instance_map: "frozendict[str, int]" = attr.ib(
factory=frozendict,
validator=attr.validators.deep_mapping(
key_validator=attr.validators.instance_of(str),
Expand All @@ -477,7 +476,7 @@ class RoomStreamToken:
),
)

def __attrs_post_init__(self):
def __attrs_post_init__(self) -> None:
"""Validates that both `topological` and `instance_map` aren't set."""

if self.instance_map and self.topological:
Expand Down Expand Up @@ -593,28 +592,28 @@ async def to_string(self, store: "DataStore") -> str:
return "s%d" % (self.stream,)


@attr.s(slots=True, frozen=True)
@attr.s(slots=True, frozen=True, auto_attribs=True)
class StreamToken:
"""A collection of positions within multiple streams.

For caching purposes, `StreamToken`s and by extension, all their attributes,
must be hashable.
"""

room_key = attr.ib(
type=RoomStreamToken, validator=attr.validators.instance_of(RoomStreamToken)
room_key: RoomStreamToken = attr.ib(
validator=attr.validators.instance_of(RoomStreamToken)
)
presence_key = attr.ib(type=int)
typing_key = attr.ib(type=int)
receipt_key = attr.ib(type=int)
account_data_key = attr.ib(type=int)
push_rules_key = attr.ib(type=int)
to_device_key = attr.ib(type=int)
device_list_key = attr.ib(type=int)
groups_key = attr.ib(type=int)
presence_key: int
typing_key: int
receipt_key: int
account_data_key: int
push_rules_key: int
to_device_key: int
device_list_key: int
groups_key: int

_SEPARATOR = "_"
START: "StreamToken"
START: ClassVar["StreamToken"]

@classmethod
async def from_string(cls, store: "DataStore", string: str) -> "StreamToken":
Expand Down Expand Up @@ -674,16 +673,16 @@ def copy_and_replace(self, key, new_value) -> "StreamToken":
StreamToken.START = StreamToken(RoomStreamToken(None, 0), 0, 0, 0, 0, 0, 0, 0, 0)


@attr.s(slots=True, frozen=True)
@attr.s(slots=True, frozen=True, auto_attribs=True)
class PersistedEventPosition:
"""Position of a newly persisted event with instance that persisted it.

This can be used to test whether the event is persisted before or after a
RoomStreamToken.
"""

instance_name = attr.ib(type=str)
stream = attr.ib(type=int)
instance_name: str
stream: int

def persisted_after(self, token: RoomStreamToken) -> bool:
return token.get_stream_pos_for_instance(self.instance_name) < self.stream
Expand Down Expand Up @@ -733,15 +732,15 @@ def to_string(self) -> str:
__str__ = to_string


@attr.s(slots=True)
@attr.s(slots=True, frozen=True, auto_attribs=True)
class ReadReceipt:
"""Information about a read-receipt"""

room_id = attr.ib()
receipt_type = attr.ib()
user_id = attr.ib()
event_ids = attr.ib()
data = attr.ib()
room_id: str
receipt_type: str
user_id: str
event_ids: List[str]
data: JsonDict


def get_verify_key_from_cross_signing_key(key_info):
Expand Down