From c8b20d145540a5e002b27b0efeec7150ffc547f3 Mon Sep 17 00:00:00 2001 From: Harishankar Kumar Date: Fri, 27 Jan 2023 23:30:10 +0530 Subject: [PATCH 1/6] change collection[str] to StrCollection Signed-off-by: Harishankar Kumar --- synapse/event_auth.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/synapse/event_auth.py b/synapse/event_auth.py index c4a7b16413c5..9c109440ddc0 100644 --- a/synapse/event_auth.py +++ b/synapse/event_auth.py @@ -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 @@ -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 @@ -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, From 05c396a5b9d0fcc9b615ae3e56fbb26ae97ca5f7 Mon Sep 17 00:00:00 2001 From: Harishankar Kumar Date: Fri, 27 Jan 2023 23:36:15 +0530 Subject: [PATCH 2/6] add changelog @14929 Signed-off-by: Harishankar Kumar --- changelog.d/14929.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/14929.misc diff --git a/changelog.d/14929.misc b/changelog.d/14929.misc new file mode 100644 index 000000000000..974da8523c50 --- /dev/null +++ b/changelog.d/14929.misc @@ -0,0 +1 @@ +change collection[str] to StrCollection in event_auth.py \ No newline at end of file From 473461db7895d7596a4f2818091f80930603f140 Mon Sep 17 00:00:00 2001 From: Harishankar Kumar Date: Sat, 28 Jan 2023 12:16:56 +0530 Subject: [PATCH 3/6] change to StrCollection in all files Signed-off-by: Harishankar Kumar --- changelog.d/14929.misc | 2 +- synapse/events/__init__.py | 6 +++--- synapse/storage/databases/main/events.py | 6 +++--- synapse/storage/databases/main/events_bg_updates.py | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/changelog.d/14929.misc b/changelog.d/14929.misc index 974da8523c50..2cc3614dfded 100644 --- a/changelog.d/14929.misc +++ b/changelog.d/14929.misc @@ -1 +1 @@ -change collection[str] to StrCollection in event_auth.py \ No newline at end of file +Use `StrCollection` to avoid potential bugs with `Collection[str]`. diff --git a/synapse/events/__init__.py b/synapse/events/__init__.py index 8aca9a3ab9e9..91118a8d847f 100644 --- a/synapse/events/__init__.py +++ b/synapse/events/__init__.py @@ -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 @@ -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. @@ -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. diff --git a/synapse/storage/databases/main/events.py b/synapse/storage/databases/main/events.py index 0f097a2927c1..d13f1c5453be 100644 --- a/synapse/storage/databases/main/events.py +++ b/synapse/storage/databases/main/events.py @@ -52,7 +52,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 @@ -571,7 +571,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. @@ -865,7 +865,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]]: diff --git a/synapse/storage/databases/main/events_bg_updates.py b/synapse/storage/databases/main/events_bg_updates.py index b9d3c36d602c..9a2a520d4d98 100644 --- a/synapse/storage/databases/main/events_bg_updates.py +++ b/synapse/storage/databases/main/events_bg_updates.py @@ -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 @@ -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( From 71647080613319ac91d8f0ca1f19326e96adc39e Mon Sep 17 00:00:00 2001 From: Harishankar Kumar Date: Sun, 29 Jan 2023 15:53:12 +0530 Subject: [PATCH 4/6] remove unused imports, lint Signed-off-by: Harishankar Kumar --- synapse/storage/databases/main/events.py | 1 - synapse/storage/databases/main/events_bg_updates.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/synapse/storage/databases/main/events.py b/synapse/storage/databases/main/events.py index d13f1c5453be..26ff6aa92c13 100644 --- a/synapse/storage/databases/main/events.py +++ b/synapse/storage/databases/main/events.py @@ -26,7 +26,6 @@ Iterable, List, Optional, - Sequence, Set, Tuple, ) diff --git a/synapse/storage/databases/main/events_bg_updates.py b/synapse/storage/databases/main/events_bg_updates.py index 9a2a520d4d98..584536111daa 100644 --- a/synapse/storage/databases/main/events_bg_updates.py +++ b/synapse/storage/databases/main/events_bg_updates.py @@ -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 From 1ae86cf950d4d498e37e48ae94c34884717dbd55 Mon Sep 17 00:00:00 2001 From: Harishankar Kumar <31770598+hari01584@users.noreply.github.com> Date: Wed, 1 Feb 2023 22:28:59 +0530 Subject: [PATCH 5/6] Update synapse/storage/databases/main/events_bg_updates.py Co-authored-by: Patrick Cloke --- synapse/storage/databases/main/events_bg_updates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/storage/databases/main/events_bg_updates.py b/synapse/storage/databases/main/events_bg_updates.py index 584536111daa..e057132cbfec 100644 --- a/synapse/storage/databases/main/events_bg_updates.py +++ b/synapse/storage/databases/main/events_bg_updates.py @@ -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, StrCollection], event_to_auth_chain), + event_to_auth_chain, ) return _CalculateChainCover( From dc9d54334502e29abab66354e031fe4567a563d8 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 3 Feb 2023 10:08:52 -0500 Subject: [PATCH 6/6] Add back the cast. --- synapse/storage/databases/main/events_bg_updates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/storage/databases/main/events_bg_updates.py b/synapse/storage/databases/main/events_bg_updates.py index e057132cbfec..584536111daa 100644 --- a/synapse/storage/databases/main/events_bg_updates.py +++ b/synapse/storage/databases/main/events_bg_updates.py @@ -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, - event_to_auth_chain, + cast(Dict[str, StrCollection], event_to_auth_chain), ) return _CalculateChainCover(