From 199649089cbf9982022cda0a0032e458ecb6ae11 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 8 Mar 2023 14:47:20 -0500 Subject: [PATCH 1/4] Add missing types. --- mypy.ini | 3 --- synapse/storage/database.py | 17 +++++++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/mypy.ini b/mypy.ini index 572734f8e7cf..9e6e4635d475 100644 --- a/mypy.ini +++ b/mypy.ini @@ -48,9 +48,6 @@ warn_unused_ignores = False [mypy-synapse.util.caches.treecache] disallow_untyped_defs = False -[mypy-synapse.storage.database] -disallow_untyped_defs = False - [mypy-tests.util.caches.test_descriptors] disallow_untyped_defs = False diff --git a/synapse/storage/database.py b/synapse/storage/database.py index 5efe31aa19bc..7ce09af21b1b 100644 --- a/synapse/storage/database.py +++ b/synapse/storage/database.py @@ -34,6 +34,7 @@ Tuple, Type, TypeVar, + Union, cast, overload, ) @@ -100,6 +101,14 @@ } +class _PoolConnection(Connection): + """ + A Connection from twisted.enterprise.adbapi.Connection. + """ + def reconnect(self) -> None: + ... + + def make_pool( reactor: IReactorCore, db_config: DatabaseConnectionConfig, @@ -892,7 +901,7 @@ async def _runInteraction() -> R: async def runWithConnection( self, - func: Callable[..., R], + func: Callable[Concatenate[LoggingDatabaseConnection, P], R], *args: Any, db_autocommit: bool = False, isolation_level: Optional[int] = None, @@ -926,7 +935,7 @@ async def runWithConnection( start_time = monotonic_time() - def inner_func(conn, *args, **kwargs): + def inner_func(conn: _PoolConnection, *args: P.args, **kwargs: P.kwargs) -> R: # We shouldn't be in a transaction. If we are then something # somewhere hasn't committed after doing work. (This is likely only # possible during startup, as `run*` will ensure changes are @@ -1019,7 +1028,7 @@ async def execute( decoder: Optional[Callable[[Cursor], R]], query: str, *args: Any, - ) -> R: + ) -> Union[List[Tuple[Any, ...]], R]: """Runs a single query for a result set. Args: @@ -1032,7 +1041,7 @@ async def execute( The result of decoder(results) """ - def interaction(txn): + def interaction(txn: LoggingTransaction) -> Union[List[Tuple[Any, ...]], R]: txn.execute(query, args) if decoder: return decoder(txn) From 701296b98c109cac5dc02114dbbdd5744549ed0b Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 8 Mar 2023 15:03:39 -0500 Subject: [PATCH 2/4] Add an ignore. --- synapse/storage/database.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/synapse/storage/database.py b/synapse/storage/database.py index 7ce09af21b1b..f44b485fed55 100644 --- a/synapse/storage/database.py +++ b/synapse/storage/database.py @@ -865,7 +865,8 @@ async def _runInteraction() -> R: try: with opentracing.start_active_span(f"db.{desc}"): result = await self.runWithConnection( - self.new_transaction, + # mypy seems to have an issue with this, maybe a bug? + self.new_transaction, # type: ignore[arg-type] desc, after_callbacks, async_after_callbacks, From edaac0952f2744e9cc5ba574b5c28d6fbfdc7a6e Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 8 Mar 2023 15:23:01 -0500 Subject: [PATCH 3/4] Newsfragment --- changelog.d/15230.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/15230.misc diff --git a/changelog.d/15230.misc b/changelog.d/15230.misc new file mode 100644 index 000000000000..93ceaeafc9b9 --- /dev/null +++ b/changelog.d/15230.misc @@ -0,0 +1 @@ +Improve type hints. From 6a01a1926afe6a1153fcf5862f029048a5e1e04a Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 8 Mar 2023 15:44:20 -0500 Subject: [PATCH 4/4] Lint --- synapse/storage/database.py | 1 + 1 file changed, 1 insertion(+) diff --git a/synapse/storage/database.py b/synapse/storage/database.py index f44b485fed55..fec4ae5b970d 100644 --- a/synapse/storage/database.py +++ b/synapse/storage/database.py @@ -105,6 +105,7 @@ class _PoolConnection(Connection): """ A Connection from twisted.enterprise.adbapi.Connection. """ + def reconnect(self) -> None: ...