From b66539e9acae90a08baf7a63672db3e2bc7ff5b7 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Thu, 11 Nov 2021 19:56:01 +0000 Subject: [PATCH 1/2] Annotations for state_deltas.py I was sad that I couldn't do better for `_curr_state_delta_stream_cache`. At least it's explicitly called out in a comment with #TODO. --- mypy.ini | 4 +++- synapse/storage/databases/main/state_deltas.py | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/mypy.ini b/mypy.ini index a06c7fc66b68..9a3bbfab4c6f 100644 --- a/mypy.ini +++ b/mypy.ini @@ -51,7 +51,6 @@ exclude = (?x) |synapse/storage/databases/main/search.py |synapse/storage/databases/main/signatures.py |synapse/storage/databases/main/state.py - |synapse/storage/databases/main/state_deltas.py |synapse/storage/databases/main/stats.py |synapse/storage/databases/main/transactions.py |synapse/storage/databases/main/user_directory.py @@ -183,6 +182,9 @@ disallow_untyped_defs = True [mypy-synapse.storage.databases.main.client_ips] disallow_untyped_defs = True +[mypy-synapse.storage.databases.main.state_deltas] +disallow_untyped_defs = True + [mypy-synapse.storage.util.*] disallow_untyped_defs = True diff --git a/synapse/storage/databases/main/state_deltas.py b/synapse/storage/databases/main/state_deltas.py index a89747d74103..7f3624b12872 100644 --- a/synapse/storage/databases/main/state_deltas.py +++ b/synapse/storage/databases/main/state_deltas.py @@ -16,11 +16,17 @@ from typing import Any, Dict, List, Tuple from synapse.storage._base import SQLBaseStore +from synapse.storage.database import LoggingTransaction +from synapse.util.caches.stream_change_cache import StreamChangeCache logger = logging.getLogger(__name__) class StateDeltasStore(SQLBaseStore): + # This class must be mixed in with a child class which provides the following + # attribute. TODO: can we get static analysis to enforce this? + _curr_state_delta_stream_cache: StreamChangeCache + async def get_current_state_deltas( self, prev_stream_id: int, max_stream_id: int ) -> Tuple[int, List[Dict[str, Any]]]: @@ -60,7 +66,9 @@ async def get_current_state_deltas( # max_stream_id. return max_stream_id, [] - def get_current_state_deltas_txn(txn): + def get_current_state_deltas_txn( + txn: LoggingTransaction, + ) -> Tuple[int, List[Dict[str, Any]]]: # First we calculate the max stream id that will give us less than # N results. # We arbitrarily limit to 100 stream_id entries to ensure we don't @@ -106,7 +114,9 @@ def get_current_state_deltas_txn(txn): "get_current_state_deltas", get_current_state_deltas_txn ) - def _get_max_stream_id_in_current_state_deltas_txn(self, txn): + def _get_max_stream_id_in_current_state_deltas_txn( + self, txn: LoggingTransaction + ) -> int: return self.db_pool.simple_select_one_onecol_txn( txn, table="current_state_delta_stream", @@ -114,7 +124,7 @@ def _get_max_stream_id_in_current_state_deltas_txn(self, txn): retcol="COALESCE(MAX(stream_id), -1)", ) - async def get_max_stream_id_in_current_state_deltas(self): + async def get_max_stream_id_in_current_state_deltas(self) -> int: return await self.db_pool.runInteraction( "get_max_stream_id_in_current_state_deltas", self._get_max_stream_id_in_current_state_deltas_txn, From 90af664d357ef5c96ddea4ec181afa7e45f0ed1b Mon Sep 17 00:00:00 2001 From: David Robertson Date: Thu, 11 Nov 2021 19:58:40 +0000 Subject: [PATCH 2/2] Changelog --- changelog.d/11316.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/11316.misc diff --git a/changelog.d/11316.misc b/changelog.d/11316.misc new file mode 100644 index 000000000000..86594a332db9 --- /dev/null +++ b/changelog.d/11316.misc @@ -0,0 +1 @@ +Add type hints to storage classes.