From 7c9fbc49f9d1eff0e5e08b63f8e23ab10704dcea Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Fri, 7 Aug 2026 03:26:09 +0300 Subject: [PATCH] Type the connection store against the config subset it uses The store only needs user/requester/conn_id from the sync config, and is shared between sliding sync and paginated sync (MSC4525), so type its methods with a Protocol both configs satisfy instead of SlidingSyncConfig (fixes the mypy failure on PaginatedSyncConfig at the get_and_clear_connection_positions call site, and drops the now-redundant ignore on record_new_state). --- synapse/handlers/sliding_sync/paginated.py | 2 +- synapse/handlers/sliding_sync/store.py | 24 ++++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/synapse/handlers/sliding_sync/paginated.py b/synapse/handlers/sliding_sync/paginated.py index 41ec6c11d8..b2ffb3ade7 100644 --- a/synapse/handlers/sliding_sync/paginated.py +++ b/synapse/handlers/sliding_sync/paginated.py @@ -397,7 +397,7 @@ class PaginatedSyncHandler(SlidingSyncHandler): new_connection_state.rooms.record_sent_rooms(relevant_rooms_to_send_map.keys()) connection_position = await self.connection_store.record_new_state( - sync_config=sync_config, # type: ignore[arg-type] + sync_config=sync_config, from_token=from_token, new_connection_state=new_connection_state, ) diff --git a/synapse/handlers/sliding_sync/store.py b/synapse/handlers/sliding_sync/store.py index 65febe58aa..7f27382c33 100644 --- a/synapse/handlers/sliding_sync/store.py +++ b/synapse/handlers/sliding_sync/store.py @@ -13,22 +13,38 @@ # import logging +from typing import Protocol import attr from synapse.logging.opentracing import trace from synapse.storage.databases.main import DataStore -from synapse.types import SlidingSyncStreamToken +from synapse.types import Requester, SlidingSyncStreamToken, UserID from synapse.types.handlers.sliding_sync import ( MutablePerConnectionState, PerConnectionState, - SlidingSyncConfig, ) from synapse.util.clock import Clock logger = logging.getLogger(__name__) +class SlidingSyncConnectionConfig(Protocol): + """The subset of a sync config the connection store needs: who the + connection belongs to and its client-chosen ID. Satisfied by both + `SlidingSyncConfig` and `PaginatedSyncConfig` (MSC4525), which share the + per-connection room-tracking machinery.""" + + @property + def user(self) -> UserID: ... + + @property + def requester(self) -> Requester: ... + + @property + def conn_id(self) -> str | None: ... + + @attr.s(auto_attribs=True) class SlidingSyncConnectionStore: """In-memory store of per-connection state, including what rooms we have @@ -63,7 +79,7 @@ class SlidingSyncConnectionStore: async def get_and_clear_connection_positions( self, - sync_config: SlidingSyncConfig, + sync_config: SlidingSyncConnectionConfig, from_token: SlidingSyncStreamToken | None, ) -> PerConnectionState: """Fetch the per-connection state for the token. @@ -90,7 +106,7 @@ class SlidingSyncConnectionStore: @trace async def record_new_state( self, - sync_config: SlidingSyncConfig, + sync_config: SlidingSyncConnectionConfig, from_token: SlidingSyncStreamToken | None, new_connection_state: MutablePerConnectionState, ) -> int: