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

Commit ed24f87

Browse files
author
David Robertson
committed
Easy annotations in s.storage.database
1 parent cb1f07f commit ed24f87

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

synapse/storage/database.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
List,
3232
Optional,
3333
Tuple,
34+
Type,
3435
TypeVar,
3536
cast,
3637
overload,
@@ -41,6 +42,7 @@
4142
from typing_extensions import Concatenate, Literal, ParamSpec
4243

4344
from twisted.enterprise import adbapi
45+
from twisted.internet.interfaces import IReactorCore
4446

4547
from synapse.api.errors import StoreError
4648
from synapse.config.database import DatabaseConnectionConfig
@@ -92,7 +94,9 @@
9294

9395

9496
def make_pool(
95-
reactor, db_config: DatabaseConnectionConfig, engine: BaseDatabaseEngine
97+
reactor: IReactorCore,
98+
db_config: DatabaseConnectionConfig,
99+
engine: BaseDatabaseEngine,
96100
) -> adbapi.ConnectionPool:
97101
"""Get the connection pool for the database."""
98102

@@ -101,7 +105,7 @@ def make_pool(
101105
db_args = dict(db_config.config.get("args", {}))
102106
db_args.setdefault("cp_reconnect", True)
103107

104-
def _on_new_connection(conn):
108+
def _on_new_connection(conn: Connection) -> None:
105109
# Ensure we have a logging context so we can correctly track queries,
106110
# etc.
107111
with LoggingContext("db.on_new_connection"):
@@ -157,7 +161,11 @@ class LoggingDatabaseConnection:
157161
default_txn_name: str
158162

159163
def cursor(
160-
self, *, txn_name=None, after_callbacks=None, exception_callbacks=None
164+
self,
165+
*,
166+
txn_name: Optional[str] = None,
167+
after_callbacks: Optional[List["_CallbackListEntry"]] = None,
168+
exception_callbacks: Optional[List["_CallbackListEntry"]] = None,
161169
) -> "LoggingTransaction":
162170
if not txn_name:
163171
txn_name = self.default_txn_name
@@ -183,11 +191,16 @@ def __enter__(self) -> "LoggingDatabaseConnection":
183191
self.conn.__enter__()
184192
return self
185193

186-
def __exit__(self, exc_type, exc_value, traceback) -> Optional[bool]:
194+
def __exit__(
195+
self,
196+
exc_type: Optional[Type[BaseException]],
197+
exc_value: Optional[BaseException],
198+
traceback: Optional[types.TracebackType],
199+
) -> Optional[bool]:
187200
return self.conn.__exit__(exc_type, exc_value, traceback)
188201

189202
# Proxy through any unknown lookups to the DB conn class.
190-
def __getattr__(self, name):
203+
def __getattr__(self, name: str) -> Any:
191204
return getattr(self.conn, name)
192205

193206

@@ -391,17 +404,22 @@ def close(self) -> None:
391404
def __enter__(self) -> "LoggingTransaction":
392405
return self
393406

394-
def __exit__(self, exc_type, exc_value, traceback):
407+
def __exit__(
408+
self,
409+
exc_type: Optional[Type[BaseException]],
410+
exc_value: Optional[BaseException],
411+
traceback: Optional[types.TracebackType],
412+
) -> None:
395413
self.close()
396414

397415

398416
class PerformanceCounters:
399-
def __init__(self):
400-
self.current_counters = {}
401-
self.previous_counters = {}
417+
def __init__(self) -> None:
418+
self.current_counters: Dict[str, Tuple[int, float]] = {}
419+
self.previous_counters: Dict[str, Tuple[int, float]] = {}
402420

403421
def update(self, key: str, duration_secs: float) -> None:
404-
count, cum_time = self.current_counters.get(key, (0, 0))
422+
count, cum_time = self.current_counters.get(key, (0, 0.0))
405423
count += 1
406424
cum_time += duration_secs
407425
self.current_counters[key] = (count, cum_time)
@@ -527,7 +545,7 @@ async def _check_safe_to_upsert(self) -> None:
527545
def start_profiling(self) -> None:
528546
self._previous_loop_ts = monotonic_time()
529547

530-
def loop():
548+
def loop() -> None:
531549
curr = self._current_txn_total_time
532550
prev = self._previous_txn_total_time
533551
self._previous_txn_total_time = curr
@@ -1186,7 +1204,7 @@ def simple_upsert_txn_emulated(
11861204
if lock:
11871205
self.engine.lock_table(txn, table)
11881206

1189-
def _getwhere(key):
1207+
def _getwhere(key: str) -> str:
11901208
# If the value we're passing in is None (aka NULL), we need to use
11911209
# IS, not =, as NULL = NULL equals NULL (False).
11921210
if keyvalues[key] is None:
@@ -2258,7 +2276,7 @@ async def simple_search_list(
22582276
term: Optional[str],
22592277
col: str,
22602278
retcols: Collection[str],
2261-
desc="simple_search_list",
2279+
desc: str = "simple_search_list",
22622280
) -> Optional[List[Dict[str, Any]]]:
22632281
"""Executes a SELECT query on the named table, which may return zero or
22642282
more rows, returning the result as a list of dicts.

0 commit comments

Comments
 (0)