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

Commit 1783156

Browse files
authored
Add some type hints to datastore (#12423)
* Add some type hints to datastore * newsfile * change `Collection` to `List` * refactor return type of `select_users_txn` * correct type hint in `stream.py` * Remove `Optional` in `select_users_txn` * remove not needed return type in `__init__` * Revert change in `get_stream_id_for_event_txn` * Remove import from `Literal`
1 parent 4e13743 commit 1783156

File tree

9 files changed

+123
-77
lines changed

9 files changed

+123
-77
lines changed

changelog.d/12423.misc

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

synapse/handlers/account_validity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ async def _send_renewal_emails(self) -> None:
180180
expiring_users = await self.store.get_users_expiring_soon()
181181

182182
if expiring_users:
183-
for user in expiring_users:
183+
for user_id, expiration_ts_ms in expiring_users:
184184
await self._send_renewal_email(
185-
user_id=user["user_id"], expiration_ts=user["expiration_ts_ms"]
185+
user_id=user_id, expiration_ts=expiration_ts_ms
186186
)
187187

188188
async def send_renewal_email_to_user(self, user_id: str) -> None:

synapse/storage/databases/main/appservice.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515
import logging
1616
import re
17-
from typing import TYPE_CHECKING, List, Optional, Pattern, Tuple
17+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Pattern, Tuple
1818

1919
from synapse.appservice import (
2020
ApplicationService,
@@ -26,7 +26,11 @@
2626
from synapse.config.appservice import load_appservices
2727
from synapse.events import EventBase
2828
from synapse.storage._base import db_to_json
29-
from synapse.storage.database import DatabasePool, LoggingDatabaseConnection
29+
from synapse.storage.database import (
30+
DatabasePool,
31+
LoggingDatabaseConnection,
32+
LoggingTransaction,
33+
)
3034
from synapse.storage.databases.main.events_worker import EventsWorkerStore
3135
from synapse.storage.databases.main.roommember import RoomMemberWorkerStore
3236
from synapse.storage.types import Cursor
@@ -92,7 +96,7 @@ def get_max_as_txn_id(txn: Cursor) -> int:
9296

9397
super().__init__(database, db_conn, hs)
9498

95-
def get_app_services(self):
99+
def get_app_services(self) -> List[ApplicationService]:
96100
return self.services_cache
97101

98102
def get_if_app_services_interested_in_user(self, user_id: str) -> bool:
@@ -256,7 +260,7 @@ async def create_appservice_txn(
256260
A new transaction.
257261
"""
258262

259-
def _create_appservice_txn(txn):
263+
def _create_appservice_txn(txn: LoggingTransaction) -> AppServiceTransaction:
260264
new_txn_id = self._as_txn_seq_gen.get_next_id_txn(txn)
261265

262266
# Insert new txn into txn table
@@ -291,7 +295,7 @@ async def complete_appservice_txn(
291295
service: The application service which was sent this transaction.
292296
"""
293297

294-
def _complete_appservice_txn(txn):
298+
def _complete_appservice_txn(txn: LoggingTransaction) -> None:
295299
# Set current txn_id for AS to 'txn_id'
296300
self.db_pool.simple_upsert_txn(
297301
txn,
@@ -322,7 +326,9 @@ async def get_oldest_unsent_txn(
322326
An AppServiceTransaction or None.
323327
"""
324328

325-
def _get_oldest_unsent_txn(txn):
329+
def _get_oldest_unsent_txn(
330+
txn: LoggingTransaction,
331+
) -> Optional[Dict[str, Any]]:
326332
# Monotonically increasing txn ids, so just select the smallest
327333
# one in the txns table (we delete them when they are sent)
328334
txn.execute(
@@ -364,7 +370,7 @@ def _get_oldest_unsent_txn(txn):
364370
)
365371

366372
async def set_appservice_last_pos(self, pos: int) -> None:
367-
def set_appservice_last_pos_txn(txn):
373+
def set_appservice_last_pos_txn(txn: LoggingTransaction) -> None:
368374
txn.execute(
369375
"UPDATE appservice_stream_position SET stream_ordering = ?", (pos,)
370376
)
@@ -378,7 +384,9 @@ async def get_new_events_for_appservice(
378384
) -> Tuple[int, List[EventBase]]:
379385
"""Get all new events for an appservice"""
380386

381-
def get_new_events_for_appservice_txn(txn):
387+
def get_new_events_for_appservice_txn(
388+
txn: LoggingTransaction,
389+
) -> Tuple[int, List[str]]:
382390
sql = (
383391
"SELECT e.stream_ordering, e.event_id"
384392
" FROM events AS e"
@@ -416,7 +424,7 @@ async def get_type_stream_id_for_appservice(
416424
% (type,)
417425
)
418426

419-
def get_type_stream_id_for_appservice_txn(txn):
427+
def get_type_stream_id_for_appservice_txn(txn: LoggingTransaction) -> int:
420428
stream_id_type = "%s_stream_id" % type
421429
txn.execute(
422430
# We do NOT want to escape `stream_id_type`.
@@ -444,7 +452,7 @@ async def set_appservice_stream_type_pos(
444452
% (stream_type,)
445453
)
446454

447-
def set_appservice_stream_type_pos_txn(txn):
455+
def set_appservice_stream_type_pos_txn(txn: LoggingTransaction) -> None:
448456
stream_id_type = "%s_stream_id" % stream_type
449457
txn.execute(
450458
"UPDATE application_services_state SET %s = ? WHERE as_id=?"

0 commit comments

Comments
 (0)