From 112266eafd457204a34a76fa51d7074d0809a1db Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 2 Sep 2020 17:52:38 +0100 Subject: Add StreamStore to mypy (#8232) --- synapse/storage/database.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'synapse/storage/database.py') diff --git a/synapse/storage/database.py b/synapse/storage/database.py index 7ab370efef..af8796ad92 100644 --- a/synapse/storage/database.py +++ b/synapse/storage/database.py @@ -604,6 +604,18 @@ class DatabasePool(object): results = [dict(zip(col_headers, row)) for row in cursor] return results + @overload + async def execute( + self, desc: str, decoder: Literal[None], query: str, *args: Any + ) -> List[Tuple[Any, ...]]: + ... + + @overload + async def execute( + self, desc: str, decoder: Callable[[Cursor], R], query: str, *args: Any + ) -> R: + ... + async def execute( self, desc: str, @@ -1088,6 +1100,28 @@ class DatabasePool(object): desc, self.simple_select_one_txn, table, keyvalues, retcols, allow_none ) + @overload + async def simple_select_one_onecol( + self, + table: str, + keyvalues: Dict[str, Any], + retcol: Iterable[str], + allow_none: Literal[False] = False, + desc: str = "simple_select_one_onecol", + ) -> Any: + ... + + @overload + async def simple_select_one_onecol( + self, + table: str, + keyvalues: Dict[str, Any], + retcol: Iterable[str], + allow_none: Literal[True] = True, + desc: str = "simple_select_one_onecol", + ) -> Optional[Any]: + ... + async def simple_select_one_onecol( self, table: str, -- cgit 1.5.1 From 912e024913bdb2b0360f5d2631a5345d3424d2e2 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 2 Sep 2020 13:11:02 -0400 Subject: Convert runInteraction to async/await (#8156) --- changelog.d/8156.misc | 1 + synapse/storage/database.py | 29 ++++++++++++++--------------- 2 files changed, 15 insertions(+), 15 deletions(-) create mode 100644 changelog.d/8156.misc (limited to 'synapse/storage/database.py') diff --git a/changelog.d/8156.misc b/changelog.d/8156.misc new file mode 100644 index 0000000000..dfe4c03171 --- /dev/null +++ b/changelog.d/8156.misc @@ -0,0 +1 @@ +Convert various parts of the codebase to async/await. diff --git a/synapse/storage/database.py b/synapse/storage/database.py index af8796ad92..8851710d47 100644 --- a/synapse/storage/database.py +++ b/synapse/storage/database.py @@ -28,6 +28,7 @@ from typing import ( Optional, Tuple, TypeVar, + cast, overload, ) @@ -35,7 +36,6 @@ from prometheus_client import Histogram from typing_extensions import Literal from twisted.enterprise import adbapi -from twisted.internet import defer from synapse.api.errors import StoreError from synapse.config.database import DatabaseConnectionConfig @@ -507,8 +507,9 @@ class DatabasePool(object): self._txn_perf_counters.update(desc, duration) sql_txn_timer.labels(desc).observe(duration) - @defer.inlineCallbacks - def runInteraction(self, desc: str, func: Callable, *args: Any, **kwargs: Any): + async def runInteraction( + self, desc: str, func: "Callable[..., R]", *args: Any, **kwargs: Any + ) -> R: """Starts a transaction on the database and runs a given function Arguments: @@ -521,7 +522,7 @@ class DatabasePool(object): kwargs: named args to pass to `func` Returns: - Deferred: The result of func + The result of func """ after_callbacks = [] # type: List[_CallbackListEntry] exception_callbacks = [] # type: List[_CallbackListEntry] @@ -530,16 +531,14 @@ class DatabasePool(object): logger.warning("Starting db txn '%s' from sentinel context", desc) try: - result = yield defer.ensureDeferred( - self.runWithConnection( - self.new_transaction, - desc, - after_callbacks, - exception_callbacks, - func, - *args, - **kwargs - ) + result = await self.runWithConnection( + self.new_transaction, + desc, + after_callbacks, + exception_callbacks, + func, + *args, + **kwargs ) for after_callback, after_args, after_kwargs in after_callbacks: @@ -549,7 +548,7 @@ class DatabasePool(object): after_callback(*after_args, **after_kwargs) raise - return result + return cast(R, result) async def runWithConnection( self, func: "Callable[..., R]", *args: Any, **kwargs: Any -- cgit 1.5.1 From c8758cb72fccc5594ce8da7ccd2256315a8aa27e Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 2 Sep 2020 15:03:12 -0400 Subject: Add an overload for simple_select_one_onecol_txn. (#8235) --- changelog.d/8235.misc | 1 + synapse/storage/database.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 changelog.d/8235.misc (limited to 'synapse/storage/database.py') diff --git a/changelog.d/8235.misc b/changelog.d/8235.misc new file mode 100644 index 0000000000..3a7a352c4f --- /dev/null +++ b/changelog.d/8235.misc @@ -0,0 +1 @@ +Add type hints to `StreamStore`. diff --git a/synapse/storage/database.py b/synapse/storage/database.py index 8851710d47..78ca6d8346 100644 --- a/synapse/storage/database.py +++ b/synapse/storage/database.py @@ -1149,6 +1149,30 @@ class DatabasePool(object): allow_none=allow_none, ) + @overload + @classmethod + def simple_select_one_onecol_txn( + cls, + txn: LoggingTransaction, + table: str, + keyvalues: Dict[str, Any], + retcol: Iterable[str], + allow_none: Literal[False] = False, + ) -> Any: + ... + + @overload + @classmethod + def simple_select_one_onecol_txn( + cls, + txn: LoggingTransaction, + table: str, + keyvalues: Dict[str, Any], + retcol: Iterable[str], + allow_none: Literal[True] = True, + ) -> Optional[Any]: + ... + @classmethod def simple_select_one_onecol_txn( cls, -- cgit 1.5.1