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

Commit

Permalink
Add missing type hints to synapse.storage.database. (#15230)
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep authored Mar 9, 2023
1 parent e7c3832 commit 3d060ea
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog.d/15230.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve type hints.
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 16 additions & 5 deletions synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
Tuple,
Type,
TypeVar,
Union,
cast,
overload,
)
Expand Down Expand Up @@ -100,6 +101,15 @@
}


class _PoolConnection(Connection):
"""
A Connection from twisted.enterprise.adbapi.Connection.
"""

def reconnect(self) -> None:
...


def make_pool(
reactor: IReactorCore,
db_config: DatabaseConnectionConfig,
Expand Down Expand Up @@ -856,7 +866,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,
Expand Down Expand Up @@ -892,7 +903,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,
Expand Down Expand Up @@ -926,7 +937,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
Expand Down Expand Up @@ -1019,7 +1030,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:
Expand All @@ -1032,7 +1043,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)
Expand Down

0 comments on commit 3d060ea

Please sign in to comment.