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

Add remaining type hints to synapse.events. #11098

Merged
merged 20 commits into from
Nov 2, 2021
Merged
Changes from 1 commit
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
33 changes: 17 additions & 16 deletions synapse/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
T = TypeVar("T")


# DictProperty (and DefaultDictPropery) require the classes they're used with to
clokep marked this conversation as resolved.
Show resolved Hide resolved
# have a _dict property to pull properties from.
_DictPropertyInstance = Union["_EventInternalMetadata", "EventBase"]


class DictProperty(Generic[T]):
"""An object property which delegates to the `_dict` within its parent object."""

Expand All @@ -65,22 +70,22 @@ def __init__(self, key: str):
def __get__(
self,
instance: Literal[None],
owner: Optional[Type[Union["_EventInternalMetadata", "EventBase"]]] = None,
owner: Optional[Type[_DictPropertyInstance]] = None,
) -> "DictProperty":
...

@overload
def __get__(
self,
instance: Union["_EventInternalMetadata", "EventBase"],
owner: Optional[Type[Union["_EventInternalMetadata", "EventBase"]]] = None,
instance: _DictPropertyInstance,
owner: Optional[Type[_DictPropertyInstance]] = None,
) -> T:
...

def __get__(
self,
instance: Optional[Union["_EventInternalMetadata", "EventBase"]],
owner: Optional[Type[Union["_EventInternalMetadata", "EventBase"]]] = None,
instance: Optional[_DictPropertyInstance],
owner: Optional[Type[_DictPropertyInstance]] = None,
) -> Union[T, "DictProperty"]:
# if the property is accessed as a class property rather than an instance
# property, return the property itself rather than the value
Expand All @@ -101,14 +106,10 @@ def __get__(
"'%s' has no '%s' property" % (type(instance), self.key)
) from e1.__context__

def __set__(
self, instance: Union["_EventInternalMetadata", "EventBase"], v: T
) -> None:
def __set__(self, instance: _DictPropertyInstance, v: T) -> None:
instance._dict[self.key] = v

def __delete__(
self, instance: Union["_EventInternalMetadata", "EventBase"]
) -> None:
def __delete__(self, instance: _DictPropertyInstance) -> None:
try:
del instance._dict[self.key]
except KeyError as e1:
Expand All @@ -134,22 +135,22 @@ def __init__(self, key: str, default: T):
def __get__(
self,
instance: Literal[None],
owner: Optional[Type[Union["_EventInternalMetadata", "EventBase"]]] = None,
owner: Optional[Type[_DictPropertyInstance]] = None,
) -> "DefaultDictProperty":
...

@overload
def __get__(
self,
instance: Union["_EventInternalMetadata", "EventBase"],
owner: Optional[Type[Union["_EventInternalMetadata", "EventBase"]]] = None,
instance: _DictPropertyInstance,
owner: Optional[Type[_DictPropertyInstance]] = None,
) -> T:
...

def __get__(
self,
instance: Optional[Union["_EventInternalMetadata", "EventBase"]],
owner: Optional[Type[Union["_EventInternalMetadata", "EventBase"]]] = None,
instance: Optional[_DictPropertyInstance],
owner: Optional[Type[_DictPropertyInstance]] = None,
) -> Union[T, "DefaultDictProperty"]:
if instance is None:
return self
Expand Down