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

Commit ba79fb4

Browse files
authored
Use StrCollection in place of Collection[str] in (most) handlers code. (#14922)
Due to the increased safety of StrCollection over Collection[str] and Sequence[str].
1 parent dc901a8 commit ba79fb4

File tree

13 files changed

+43
-58
lines changed

13 files changed

+43
-58
lines changed

changelog.d/14922.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use `StrCollection` to avoid potential bugs with `Collection[str]`.

synapse/handlers/account_data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515
import logging
1616
import random
17-
from typing import TYPE_CHECKING, Awaitable, Callable, Collection, List, Optional, Tuple
17+
from typing import TYPE_CHECKING, Awaitable, Callable, List, Optional, Tuple
1818

1919
from synapse.api.constants import AccountDataTypes
2020
from synapse.replication.http.account_data import (
@@ -26,7 +26,7 @@
2626
ReplicationRemoveUserAccountDataRestServlet,
2727
)
2828
from synapse.streams import EventSource
29-
from synapse.types import JsonDict, StreamKeyType, UserID
29+
from synapse.types import JsonDict, StrCollection, StreamKeyType, UserID
3030

3131
if TYPE_CHECKING:
3232
from synapse.server import HomeServer
@@ -322,7 +322,7 @@ async def get_new_events(
322322
user: UserID,
323323
from_key: int,
324324
limit: int,
325-
room_ids: Collection[str],
325+
room_ids: StrCollection,
326326
is_guest: bool,
327327
explicit_room_id: Optional[str] = None,
328328
) -> Tuple[List[JsonDict], int]:

synapse/handlers/device.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from typing import (
1919
TYPE_CHECKING,
2020
Any,
21-
Collection,
2221
Dict,
2322
Iterable,
2423
List,
@@ -45,6 +44,7 @@
4544
)
4645
from synapse.types import (
4746
JsonDict,
47+
StrCollection,
4848
StreamKeyType,
4949
StreamToken,
5050
UserID,
@@ -146,7 +146,7 @@ async def get_device(self, user_id: str, device_id: str) -> JsonDict:
146146

147147
@cancellable
148148
async def get_device_changes_in_shared_rooms(
149-
self, user_id: str, room_ids: Collection[str], from_token: StreamToken
149+
self, user_id: str, room_ids: StrCollection, from_token: StreamToken
150150
) -> Set[str]:
151151
"""Get the set of users whose devices have changed who share a room with
152152
the given user.
@@ -551,7 +551,7 @@ async def update_device(self, user_id: str, device_id: str, content: dict) -> No
551551
@trace
552552
@measure_func("notify_device_update")
553553
async def notify_device_update(
554-
self, user_id: str, device_ids: Collection[str]
554+
self, user_id: str, device_ids: StrCollection
555555
) -> None:
556556
"""Notify that a user's device(s) has changed. Pokes the notifier, and
557557
remote servers if the user is local.

synapse/handlers/event_auth.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import logging
15-
from typing import TYPE_CHECKING, Collection, List, Mapping, Optional, Union
15+
from typing import TYPE_CHECKING, List, Mapping, Optional, Union
1616

1717
from synapse import event_auth
1818
from synapse.api.constants import (
@@ -29,7 +29,7 @@
2929
)
3030
from synapse.events import EventBase
3131
from synapse.events.builder import EventBuilder
32-
from synapse.types import StateMap, get_domain_from_id
32+
from synapse.types import StateMap, StrCollection, get_domain_from_id
3333

3434
if TYPE_CHECKING:
3535
from synapse.server import HomeServer
@@ -290,7 +290,7 @@ async def has_restricted_join_rules(
290290

291291
async def get_rooms_that_allow_join(
292292
self, state_ids: StateMap[str]
293-
) -> Collection[str]:
293+
) -> StrCollection:
294294
"""
295295
Generate a list of rooms in which membership allows access to a room.
296296
@@ -331,7 +331,7 @@ async def get_rooms_that_allow_join(
331331

332332
return result
333333

334-
async def is_user_in_rooms(self, room_ids: Collection[str], user_id: str) -> bool:
334+
async def is_user_in_rooms(self, room_ids: StrCollection, user_id: str) -> bool:
335335
"""
336336
Check whether a user is a member of any of the provided rooms.
337337

synapse/handlers/federation.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,7 @@
2020
import logging
2121
from enum import Enum
2222
from http import HTTPStatus
23-
from typing import (
24-
TYPE_CHECKING,
25-
Collection,
26-
Dict,
27-
Iterable,
28-
List,
29-
Optional,
30-
Set,
31-
Tuple,
32-
Union,
33-
)
23+
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Set, Tuple, Union
3424

3525
import attr
3626
from prometheus_client import Histogram
@@ -70,7 +60,7 @@
7060
)
7161
from synapse.storage.databases.main.events import PartialStateConflictError
7262
from synapse.storage.databases.main.events_worker import EventRedactBehaviour
73-
from synapse.types import JsonDict, get_domain_from_id
63+
from synapse.types import JsonDict, StrCollection, get_domain_from_id
7464
from synapse.types.state import StateFilter
7565
from synapse.util.async_helpers import Linearizer
7666
from synapse.util.retryutils import NotRetryingDestination
@@ -179,7 +169,7 @@ def __init__(self, hs: "HomeServer"):
179169
# A dictionary mapping room IDs to (initial destination, other destinations)
180170
# tuples.
181171
self._partial_state_syncs_maybe_needing_restart: Dict[
182-
str, Tuple[Optional[str], Collection[str]]
172+
str, Tuple[Optional[str], StrCollection]
183173
] = {}
184174
# A lock guarding the partial state flag for rooms.
185175
# When the lock is held for a given room, no other concurrent code may
@@ -437,7 +427,7 @@ async def _maybe_backfill_inner(
437427
)
438428
)
439429

440-
async def try_backfill(domains: Collection[str]) -> bool:
430+
async def try_backfill(domains: StrCollection) -> bool:
441431
# TODO: Should we try multiple of these at a time?
442432

443433
# Number of contacted remote homeservers that have denied our backfill
@@ -1730,7 +1720,7 @@ async def _resume_partial_state_room_sync(self) -> None:
17301720
def _start_partial_state_room_sync(
17311721
self,
17321722
initial_destination: Optional[str],
1733-
other_destinations: Collection[str],
1723+
other_destinations: StrCollection,
17341724
room_id: str,
17351725
) -> None:
17361726
"""Starts the background process to resync the state of a partial state room,
@@ -1812,7 +1802,7 @@ async def _sync_partial_state_room_wrapper() -> None:
18121802
async def _sync_partial_state_room(
18131803
self,
18141804
initial_destination: Optional[str],
1815-
other_destinations: Collection[str],
1805+
other_destinations: StrCollection,
18161806
room_id: str,
18171807
) -> None:
18181808
"""Background process to resync the state of a partial-state room
@@ -1949,9 +1939,9 @@ async def _sync_partial_state_room(
19491939

19501940
def _prioritise_destinations_for_partial_state_resync(
19511941
initial_destination: Optional[str],
1952-
other_destinations: Collection[str],
1942+
other_destinations: StrCollection,
19531943
room_id: str,
1954-
) -> Collection[str]:
1944+
) -> StrCollection:
19551945
"""Work out the order in which we should ask servers to resync events.
19561946
19571947
If an `initial_destination` is given, it takes top priority. Otherwise

synapse/handlers/federation_event.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
PersistedEventPosition,
8181
RoomStreamToken,
8282
StateMap,
83+
StrCollection,
8384
UserID,
8485
get_domain_from_id,
8586
)
@@ -615,7 +616,7 @@ async def update_state_for_partial_state_event(
615616

616617
@trace
617618
async def backfill(
618-
self, dest: str, room_id: str, limit: int, extremities: Collection[str]
619+
self, dest: str, room_id: str, limit: int, extremities: StrCollection
619620
) -> None:
620621
"""Trigger a backfill request to `dest` for the given `room_id`
621622
@@ -1565,7 +1566,7 @@ async def backfill_event_id(
15651566
@trace
15661567
@tag_args
15671568
async def _get_events_and_persist(
1568-
self, destination: str, room_id: str, event_ids: Collection[str]
1569+
self, destination: str, room_id: str, event_ids: StrCollection
15691570
) -> None:
15701571
"""Fetch the given events from a server, and persist them as outliers.
15711572

synapse/handlers/pagination.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
import logging
16-
from typing import TYPE_CHECKING, Collection, Dict, List, Optional, Set
16+
from typing import TYPE_CHECKING, Dict, List, Optional, Set
1717

1818
import attr
1919

@@ -28,7 +28,7 @@
2828
from synapse.metrics.background_process_metrics import run_as_background_process
2929
from synapse.rest.admin._base import assert_user_is_admin
3030
from synapse.streams.config import PaginationConfig
31-
from synapse.types import JsonDict, Requester, StreamKeyType
31+
from synapse.types import JsonDict, Requester, StrCollection, StreamKeyType
3232
from synapse.types.state import StateFilter
3333
from synapse.util.async_helpers import ReadWriteLock
3434
from synapse.util.stringutils import random_string
@@ -391,7 +391,7 @@ def get_delete_status(self, delete_id: str) -> Optional[DeleteStatus]:
391391
"""
392392
return self._delete_by_id.get(delete_id)
393393

394-
def get_delete_ids_by_room(self, room_id: str) -> Optional[Collection[str]]:
394+
def get_delete_ids_by_room(self, room_id: str) -> Optional[StrCollection]:
395395
"""Get all active delete ids by room
396396
397397
Args:

synapse/handlers/room.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,7 @@
2020
import string
2121
from collections import OrderedDict
2222
from http import HTTPStatus
23-
from typing import (
24-
TYPE_CHECKING,
25-
Any,
26-
Awaitable,
27-
Collection,
28-
Dict,
29-
List,
30-
Optional,
31-
Tuple,
32-
)
23+
from typing import TYPE_CHECKING, Any, Awaitable, Dict, List, Optional, Tuple
3324

3425
import attr
3526
from typing_extensions import TypedDict
@@ -72,6 +63,7 @@
7263
RoomID,
7364
RoomStreamToken,
7465
StateMap,
66+
StrCollection,
7567
StreamKeyType,
7668
StreamToken,
7769
UserID,
@@ -1644,7 +1636,7 @@ async def get_new_events(
16441636
user: UserID,
16451637
from_key: RoomStreamToken,
16461638
limit: int,
1647-
room_ids: Collection[str],
1639+
room_ids: StrCollection,
16481640
is_guest: bool,
16491641
explicit_room_id: Optional[str] = None,
16501642
) -> Tuple[List[EventBase], RoomStreamToken]:

synapse/handlers/room_summary.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
)
3737
from synapse.api.ratelimiting import Ratelimiter
3838
from synapse.events import EventBase
39-
from synapse.types import JsonDict, Requester
39+
from synapse.types import JsonDict, Requester, StrCollection
4040
from synapse.util.caches.response_cache import ResponseCache
4141

4242
if TYPE_CHECKING:
@@ -870,7 +870,7 @@ class _RoomQueueEntry:
870870
# The room ID of this entry.
871871
room_id: str
872872
# The server to query if the room is not known locally.
873-
via: Sequence[str]
873+
via: StrCollection
874874
# The minimum number of hops necessary to get to this room (compared to the
875875
# originally requested room).
876876
depth: int = 0

synapse/handlers/search.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import itertools
1616
import logging
17-
from typing import TYPE_CHECKING, Collection, Dict, Iterable, List, Optional, Set, Tuple
17+
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Set, Tuple
1818

1919
import attr
2020
from unpaddedbase64 import decode_base64, encode_base64
@@ -23,7 +23,7 @@
2323
from synapse.api.errors import NotFoundError, SynapseError
2424
from synapse.api.filtering import Filter
2525
from synapse.events import EventBase
26-
from synapse.types import JsonDict, StreamKeyType, UserID
26+
from synapse.types import JsonDict, StrCollection, StreamKeyType, UserID
2727
from synapse.types.state import StateFilter
2828
from synapse.visibility import filter_events_for_client
2929

@@ -418,7 +418,7 @@ async def _search(
418418
async def _search_by_rank(
419419
self,
420420
user: UserID,
421-
room_ids: Collection[str],
421+
room_ids: StrCollection,
422422
search_term: str,
423423
keys: Iterable[str],
424424
search_filter: Filter,
@@ -491,7 +491,7 @@ async def _search_by_rank(
491491
async def _search_by_recent(
492492
self,
493493
user: UserID,
494-
room_ids: Collection[str],
494+
room_ids: StrCollection,
495495
search_term: str,
496496
keys: Iterable[str],
497497
search_filter: Filter,

0 commit comments

Comments
 (0)