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

Commit 7ec0a14

Browse files
authored
Convert more cached return values to immutable types (#16356)
1 parent d7c89c5 commit 7ec0a14

File tree

11 files changed

+52
-36
lines changed

11 files changed

+52
-36
lines changed

changelog.d/16356.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve type hints.

synapse/api/filtering.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from synapse.api.errors import SynapseError
3838
from synapse.api.presence import UserPresenceState
3939
from synapse.events import EventBase, relation_from_event
40-
from synapse.types import JsonDict, RoomID, UserID
40+
from synapse.types import JsonDict, JsonMapping, RoomID, UserID
4141

4242
if TYPE_CHECKING:
4343
from synapse.server import HomeServer
@@ -191,7 +191,7 @@ def check_valid_filter(self, user_filter_json: JsonDict) -> None:
191191

192192

193193
class FilterCollection:
194-
def __init__(self, hs: "HomeServer", filter_json: JsonDict):
194+
def __init__(self, hs: "HomeServer", filter_json: JsonMapping):
195195
self._filter_json = filter_json
196196

197197
room_filter_json = self._filter_json.get("room", {})
@@ -219,7 +219,7 @@ def __init__(self, hs: "HomeServer", filter_json: JsonDict):
219219
def __repr__(self) -> str:
220220
return "<FilterCollection %s>" % (json.dumps(self._filter_json),)
221221

222-
def get_filter_json(self) -> JsonDict:
222+
def get_filter_json(self) -> JsonMapping:
223223
return self._filter_json
224224

225225
def timeline_limit(self) -> int:
@@ -313,7 +313,7 @@ def blocks_all_room_timeline(self) -> bool:
313313

314314

315315
class Filter:
316-
def __init__(self, hs: "HomeServer", filter_json: JsonDict):
316+
def __init__(self, hs: "HomeServer", filter_json: JsonMapping):
317317
self._hs = hs
318318
self._store = hs.get_datastores().main
319319
self.filter_json = filter_json

synapse/federation/federation_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
from synapse.http.client import is_unknown_endpoint
6565
from synapse.http.types import QueryParams
6666
from synapse.logging.opentracing import SynapseTags, log_kv, set_tag, tag_args, trace
67-
from synapse.types import JsonDict, UserID, get_domain_from_id
67+
from synapse.types import JsonDict, StrCollection, UserID, get_domain_from_id
6868
from synapse.util.async_helpers import concurrently_execute
6969
from synapse.util.caches.expiringcache import ExpiringCache
7070
from synapse.util.retryutils import NotRetryingDestination
@@ -1704,7 +1704,7 @@ async def send_request(
17041704
async def timestamp_to_event(
17051705
self,
17061706
*,
1707-
destinations: List[str],
1707+
destinations: StrCollection,
17081708
room_id: str,
17091709
timestamp: int,
17101710
direction: Direction,

synapse/handlers/federation_event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ async def _resync_device(self, sender: str) -> None:
15381538
logger.exception("Failed to resync device for %s", sender)
15391539

15401540
async def backfill_event_id(
1541-
self, destinations: List[str], room_id: str, event_id: str
1541+
self, destinations: StrCollection, room_id: str, event_id: str
15421542
) -> PulledPduInfo:
15431543
"""Backfill a single event and persist it as a non-outlier which means
15441544
we also pull in all of the state and auth events necessary for it.

synapse/handlers/relations.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@
1313
# limitations under the License.
1414
import enum
1515
import logging
16-
from typing import TYPE_CHECKING, Collection, Dict, FrozenSet, Iterable, List, Optional
16+
from typing import (
17+
TYPE_CHECKING,
18+
Collection,
19+
Dict,
20+
FrozenSet,
21+
Iterable,
22+
List,
23+
Mapping,
24+
Optional,
25+
Sequence,
26+
)
1727

1828
import attr
1929

@@ -245,7 +255,7 @@ async def redact_events_related_to(
245255

246256
async def get_references_for_events(
247257
self, event_ids: Collection[str], ignored_users: FrozenSet[str] = frozenset()
248-
) -> Dict[str, List[_RelatedEvent]]:
258+
) -> Mapping[str, Sequence[_RelatedEvent]]:
249259
"""Get a list of references to the given events.
250260
251261
Args:

synapse/rest/client/filter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from synapse.http.server import HttpServer
2020
from synapse.http.servlet import RestServlet, parse_json_object_from_request
2121
from synapse.http.site import SynapseRequest
22-
from synapse.types import JsonDict, UserID
22+
from synapse.types import JsonDict, JsonMapping, UserID
2323

2424
from ._base import client_patterns, set_timeline_upper_limit
2525

@@ -41,7 +41,7 @@ def __init__(self, hs: "HomeServer"):
4141

4242
async def on_GET(
4343
self, request: SynapseRequest, user_id: str, filter_id: str
44-
) -> Tuple[int, JsonDict]:
44+
) -> Tuple[int, JsonMapping]:
4545
target_user = UserID.from_string(user_id)
4646
requester = await self.auth.get_user_by_req(request)
4747

synapse/storage/controllers/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ async def get_current_hosts_in_room(self, room_id: str) -> AbstractSet[str]:
582582

583583
@trace
584584
@tag_args
585-
async def get_current_hosts_in_room_ordered(self, room_id: str) -> List[str]:
585+
async def get_current_hosts_in_room_ordered(self, room_id: str) -> Tuple[str, ...]:
586586
"""Get current hosts in room based on current state.
587587
588588
Blocks until we have full state for the given room. This only happens for rooms

synapse/storage/databases/main/filtering.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
LoggingTransaction,
2626
)
2727
from synapse.storage.engines import PostgresEngine
28-
from synapse.types import JsonDict, UserID
28+
from synapse.types import JsonDict, JsonMapping, UserID
2929
from synapse.util.caches.descriptors import cached
3030

3131
if TYPE_CHECKING:
@@ -145,7 +145,7 @@ def _final_batch(txn: LoggingTransaction, lower_bound_id: str) -> None:
145145
@cached(num_args=2)
146146
async def get_user_filter(
147147
self, user_id: UserID, filter_id: Union[int, str]
148-
) -> JsonDict:
148+
) -> JsonMapping:
149149
# filter_id is BIGINT UNSIGNED, so if it isn't a number, fail
150150
# with a coherent error message rather than 500 M_UNKNOWN.
151151
try:

synapse/storage/databases/main/relations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ async def get_references_for_event(self, event_id: str) -> List[JsonDict]:
465465
@cachedList(cached_method_name="get_references_for_event", list_name="event_ids")
466466
async def get_references_for_events(
467467
self, event_ids: Collection[str]
468-
) -> Mapping[str, Optional[List[_RelatedEvent]]]:
468+
) -> Mapping[str, Optional[Sequence[_RelatedEvent]]]:
469469
"""Get a list of references to the given events.
470470
471471
Args:
@@ -931,7 +931,7 @@ async def get_threads(
931931
room_id: str,
932932
limit: int = 5,
933933
from_token: Optional[ThreadsNextBatch] = None,
934-
) -> Tuple[List[str], Optional[ThreadsNextBatch]]:
934+
) -> Tuple[Sequence[str], Optional[ThreadsNextBatch]]:
935935
"""Get a list of thread IDs, ordered by topological ordering of their
936936
latest reply.
937937

synapse/storage/databases/main/roommember.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ def get_current_hosts_in_room_txn(txn: LoggingTransaction) -> Set[str]:
984984
)
985985

986986
@cached(iterable=True, max_entries=10000)
987-
async def get_current_hosts_in_room_ordered(self, room_id: str) -> List[str]:
987+
async def get_current_hosts_in_room_ordered(self, room_id: str) -> Tuple[str, ...]:
988988
"""
989989
Get current hosts in room based on current state.
990990
@@ -1013,12 +1013,14 @@ async def get_current_hosts_in_room_ordered(self, room_id: str) -> List[str]:
10131013
# `get_users_in_room` rather than funky SQL.
10141014

10151015
domains = await self.get_current_hosts_in_room(room_id)
1016-
return list(domains)
1016+
return tuple(domains)
10171017

10181018
# For PostgreSQL we can use a regex to pull out the domains from the
10191019
# joined users in `current_state_events` via regex.
10201020

1021-
def get_current_hosts_in_room_ordered_txn(txn: LoggingTransaction) -> List[str]:
1021+
def get_current_hosts_in_room_ordered_txn(
1022+
txn: LoggingTransaction,
1023+
) -> Tuple[str, ...]:
10221024
# Returns a list of servers currently joined in the room sorted by
10231025
# longest in the room first (aka. with the lowest depth). The
10241026
# heuristic of sorting by servers who have been in the room the
@@ -1043,7 +1045,7 @@ def get_current_hosts_in_room_ordered_txn(txn: LoggingTransaction) -> List[str]:
10431045
"""
10441046
txn.execute(sql, (room_id,))
10451047
# `server_domain` will be `NULL` for malformed MXIDs with no colons.
1046-
return [d for d, in txn if d is not None]
1048+
return tuple(d for d, in txn if d is not None)
10471049

10481050
return await self.db_pool.runInteraction(
10491051
"get_current_hosts_in_room_ordered", get_current_hosts_in_room_ordered_txn

0 commit comments

Comments
 (0)