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

Commit

Permalink
Change collection[str] to StrCollection in event_auth code (#14929)
Browse files Browse the repository at this point in the history
Signed-off-by: Harishankar Kumar <hari01584@gmail.com>
  • Loading branch information
hari01584 authored Feb 14, 2023
1 parent c0bf4c3 commit db2b105
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
1 change: 1 addition & 0 deletions changelog.d/14929.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use `StrCollection` to avoid potential bugs with `Collection[str]`.
23 changes: 9 additions & 14 deletions synapse/event_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,7 @@
import collections.abc
import logging
import typing
from typing import (
Any,
Collection,
Dict,
Iterable,
List,
Mapping,
Optional,
Set,
Tuple,
Union,
)
from typing import Any, Dict, Iterable, List, Mapping, Optional, Set, Tuple, Union

from canonicaljson import encode_canonical_json
from signedjson.key import decode_verify_key_bytes
Expand Down Expand Up @@ -56,7 +45,13 @@
RoomVersions,
)
from synapse.storage.databases.main.events_worker import EventRedactBehaviour
from synapse.types import MutableStateMap, StateMap, UserID, get_domain_from_id
from synapse.types import (
MutableStateMap,
StateMap,
StrCollection,
UserID,
get_domain_from_id,
)

if typing.TYPE_CHECKING:
# conditional imports to avoid import cycle
Expand All @@ -69,7 +64,7 @@
class _EventSourceStore(Protocol):
async def get_events(
self,
event_ids: Collection[str],
event_ids: StrCollection,
redact_behaviour: EventRedactBehaviour,
get_prev_content: bool = False,
allow_rejected: bool = False,
Expand Down
6 changes: 3 additions & 3 deletions synapse/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

from synapse.api.constants import RelationTypes
from synapse.api.room_versions import EventFormatVersions, RoomVersion, RoomVersions
from synapse.types import JsonDict, RoomStreamToken
from synapse.types import JsonDict, RoomStreamToken, StrCollection
from synapse.util.caches import intern_dict
from synapse.util.frozenutils import freeze
from synapse.util.stringutils import strtobool
Expand Down Expand Up @@ -413,7 +413,7 @@ def prev_event_ids(self) -> Sequence[str]:
"""
return [e for e, _ in self._dict["prev_events"]]

def auth_event_ids(self) -> Sequence[str]:
def auth_event_ids(self) -> StrCollection:
"""Returns the list of auth event IDs. The order matches the order
specified in the event, though there is no meaning to it.
Expand Down Expand Up @@ -558,7 +558,7 @@ def prev_event_ids(self) -> Sequence[str]:
"""
return self._dict["prev_events"]

def auth_event_ids(self) -> Sequence[str]:
def auth_event_ids(self) -> StrCollection:
"""Returns the list of auth event IDs. The order matches the order
specified in the event, though there is no meaning to it.
Expand Down
7 changes: 3 additions & 4 deletions synapse/storage/databases/main/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
Iterable,
List,
Optional,
Sequence,
Set,
Tuple,
)
Expand All @@ -51,7 +50,7 @@
from synapse.storage.engines import PostgresEngine
from synapse.storage.util.id_generators import AbstractStreamIdGenerator
from synapse.storage.util.sequence import SequenceGenerator
from synapse.types import JsonDict, StateMap, get_domain_from_id
from synapse.types import JsonDict, StateMap, StrCollection, get_domain_from_id
from synapse.util import json_encoder
from synapse.util.iterutils import batch_iter, sorted_topologically
from synapse.util.stringutils import non_null_str_or_none
Expand Down Expand Up @@ -552,7 +551,7 @@ def _add_chain_cover_index(
event_chain_id_gen: SequenceGenerator,
event_to_room_id: Dict[str, str],
event_to_types: Dict[str, Tuple[str, str]],
event_to_auth_chain: Dict[str, Sequence[str]],
event_to_auth_chain: Dict[str, StrCollection],
) -> None:
"""Calculate the chain cover index for the given events.
Expand Down Expand Up @@ -846,7 +845,7 @@ def _allocate_chain_ids(
event_chain_id_gen: SequenceGenerator,
event_to_room_id: Dict[str, str],
event_to_types: Dict[str, Tuple[str, str]],
event_to_auth_chain: Dict[str, Sequence[str]],
event_to_auth_chain: Dict[str, StrCollection],
events_to_calc_chain_id_for: Set[str],
chain_map: Dict[str, Tuple[int, int]],
) -> Dict[str, Tuple[int, int]]:
Expand Down
6 changes: 3 additions & 3 deletions synapse/storage/databases/main/events_bg_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import logging
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Set, Tuple, cast
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple, cast

import attr

Expand All @@ -29,7 +29,7 @@
)
from synapse.storage.databases.main.events import PersistEventsStore
from synapse.storage.types import Cursor
from synapse.types import JsonDict
from synapse.types import JsonDict, StrCollection

if TYPE_CHECKING:
from synapse.server import HomeServer
Expand Down Expand Up @@ -1061,7 +1061,7 @@ def _calculate_chain_cover_txn(
self.event_chain_id_gen, # type: ignore[attr-defined]
event_to_room_id,
event_to_types,
cast(Dict[str, Sequence[str]], event_to_auth_chain),
cast(Dict[str, StrCollection], event_to_auth_chain),
)

return _CalculateChainCover(
Expand Down

0 comments on commit db2b105

Please sign in to comment.