mirror of
https://github.com/element-hq/synapse.git
synced 2026-09-17 10:14:59 +00:00
Use type hinting generics in standard collections (#19046)
aka PEP 585, added in Python 3.9 - https://peps.python.org/pep-0585/ - https://docs.astral.sh/ruff/rules/non-pep585-annotation/
This commit is contained in:
+16
-20
@@ -21,13 +21,9 @@
|
||||
import itertools
|
||||
from typing import (
|
||||
Collection,
|
||||
Dict,
|
||||
Iterable,
|
||||
List,
|
||||
Mapping,
|
||||
Optional,
|
||||
Set,
|
||||
Tuple,
|
||||
TypeVar,
|
||||
)
|
||||
|
||||
@@ -94,7 +90,7 @@ class FakeEvent:
|
||||
self.content = content
|
||||
self.room_id = ROOM_ID
|
||||
|
||||
def to_event(self, auth_events: List[str], prev_events: List[str]) -> EventBase:
|
||||
def to_event(self, auth_events: list[str], prev_events: list[str]) -> EventBase:
|
||||
"""Given the auth_events and prev_events, convert to a Frozen Event
|
||||
|
||||
Args:
|
||||
@@ -461,9 +457,9 @@ class StateTestCase(unittest.TestCase):
|
||||
|
||||
def do_check(
|
||||
self,
|
||||
events: List[FakeEvent],
|
||||
edges: List[List[str]],
|
||||
expected_state_ids: List[str],
|
||||
events: list[FakeEvent],
|
||||
edges: list[list[str]],
|
||||
expected_state_ids: list[str],
|
||||
) -> None:
|
||||
"""Take a list of events and edges and calculate the state of the
|
||||
graph at END, and asserts it matches `expected_state_ids`
|
||||
@@ -476,9 +472,9 @@ class StateTestCase(unittest.TestCase):
|
||||
the keys that haven't changed since START).
|
||||
"""
|
||||
# We want to sort the events into topological order for processing.
|
||||
graph: Dict[str, Set[str]] = {}
|
||||
graph: dict[str, set[str]] = {}
|
||||
|
||||
fake_event_map: Dict[str, FakeEvent] = {}
|
||||
fake_event_map: dict[str, FakeEvent] = {}
|
||||
|
||||
for ev in itertools.chain(INITIAL_EVENTS, events):
|
||||
graph[ev.node_id] = set()
|
||||
@@ -491,8 +487,8 @@ class StateTestCase(unittest.TestCase):
|
||||
for a, b in pairwise(edge_list):
|
||||
graph[a].add(b)
|
||||
|
||||
event_map: Dict[str, EventBase] = {}
|
||||
state_at_event: Dict[str, StateMap[str]] = {}
|
||||
event_map: dict[str, EventBase] = {}
|
||||
state_at_event: dict[str, StateMap[str]] = {}
|
||||
|
||||
# We copy the map as the sort consumes the graph
|
||||
graph_copy = {k: set(v) for k, v in graph.items()}
|
||||
@@ -568,7 +564,7 @@ class StateTestCase(unittest.TestCase):
|
||||
|
||||
class LexicographicalTestCase(unittest.TestCase):
|
||||
def test_simple(self) -> None:
|
||||
graph: Dict[str, Set[str]] = {
|
||||
graph: dict[str, set[str]] = {
|
||||
"l": {"o"},
|
||||
"m": {"n", "o"},
|
||||
"n": {"o"},
|
||||
@@ -1020,7 +1016,7 @@ class AuthChainDifferenceTestCase(unittest.TestCase):
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
def pairwise(iterable: Iterable[T]) -> Iterable[Tuple[T, T]]:
|
||||
def pairwise(iterable: Iterable[T]) -> Iterable[tuple[T, T]]:
|
||||
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
|
||||
a, b = itertools.tee(iterable)
|
||||
next(b, None)
|
||||
@@ -1029,11 +1025,11 @@ def pairwise(iterable: Iterable[T]) -> Iterable[Tuple[T, T]]:
|
||||
|
||||
@attr.s
|
||||
class TestStateResolutionStore:
|
||||
event_map: Dict[str, EventBase] = attr.ib()
|
||||
event_map: dict[str, EventBase] = attr.ib()
|
||||
|
||||
def get_events(
|
||||
self, event_ids: Collection[str], allow_rejected: bool = False
|
||||
) -> "defer.Deferred[Dict[str, EventBase]]":
|
||||
) -> "defer.Deferred[dict[str, EventBase]]":
|
||||
"""Get events from the database
|
||||
|
||||
Args:
|
||||
@@ -1048,7 +1044,7 @@ class TestStateResolutionStore:
|
||||
{eid: self.event_map[eid] for eid in event_ids if eid in self.event_map}
|
||||
)
|
||||
|
||||
def _get_auth_chain(self, event_ids: Iterable[str]) -> List[str]:
|
||||
def _get_auth_chain(self, event_ids: Iterable[str]) -> list[str]:
|
||||
"""Gets the full auth chain for a set of events (including rejected
|
||||
events).
|
||||
|
||||
@@ -1085,9 +1081,9 @@ class TestStateResolutionStore:
|
||||
def get_auth_chain_difference(
|
||||
self,
|
||||
room_id: str,
|
||||
auth_sets: List[Set[str]],
|
||||
conflicted_state: Optional[Set[str]],
|
||||
additional_backwards_reachable_conflicted_events: Optional[Set[str]],
|
||||
auth_sets: list[set[str]],
|
||||
conflicted_state: Optional[set[str]],
|
||||
additional_backwards_reachable_conflicted_events: Optional[set[str]],
|
||||
) -> "defer.Deferred[StateDifference]":
|
||||
chains = [frozenset(self._get_auth_chain(a)) for a in auth_sets]
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
#
|
||||
#
|
||||
import itertools
|
||||
from typing import Dict, List, Optional, Sequence, Set
|
||||
from typing import Optional, Sequence
|
||||
|
||||
from twisted.internet import defer
|
||||
from twisted.test.proto_helpers import MemoryReactor
|
||||
@@ -357,11 +357,11 @@ class StateResV21TestCase(unittest.HomeserverTestCase):
|
||||
self,
|
||||
room_id: str,
|
||||
state_maps: Sequence[StateMap[str]],
|
||||
event_map: Optional[Dict[str, EventBase]],
|
||||
event_map: Optional[dict[str, EventBase]],
|
||||
state_res_store: StateResolutionStoreInterface,
|
||||
) -> Set[str]:
|
||||
) -> set[str]:
|
||||
_, conflicted_state = _seperate(state_maps)
|
||||
conflicted_set: Optional[Set[str]] = set(
|
||||
conflicted_set: Optional[set[str]] = set(
|
||||
itertools.chain.from_iterable(conflicted_state.values())
|
||||
)
|
||||
if event_map is None:
|
||||
@@ -377,7 +377,7 @@ class StateResV21TestCase(unittest.HomeserverTestCase):
|
||||
def get_resolution_and_verify_expected(
|
||||
self,
|
||||
state_maps: Sequence[StateMap[str]],
|
||||
events: List[EventBase],
|
||||
events: list[EventBase],
|
||||
expected: StateMap[str],
|
||||
) -> None:
|
||||
room_id = events[0].room_id
|
||||
@@ -475,9 +475,9 @@ class StateResV21TestCase(unittest.HomeserverTestCase):
|
||||
event_type: str,
|
||||
state_key: Optional[str],
|
||||
sender: str,
|
||||
content: Dict,
|
||||
auth_events: List[str],
|
||||
prev_events: Optional[List[str]] = None,
|
||||
content: dict,
|
||||
auth_events: list[str],
|
||||
prev_events: Optional[list[str]] = None,
|
||||
room_id: Optional[str] = None,
|
||||
) -> EventBase:
|
||||
"""Short-hand for event_from_pdu_json for fields we typically care about.
|
||||
|
||||
Reference in New Issue
Block a user