Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typing for Transaction #493

Merged
merged 2 commits into from
Jun 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions databases/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ def _build_query(
return query


_CallableType = typing.TypeVar("_CallableType", bound=typing.Callable)


class Transaction:
def __init__(
self,
Expand Down Expand Up @@ -347,13 +350,13 @@ async def __aexit__(
else:
await self.commit()

def __await__(self) -> typing.Generator:
def __await__(self) -> typing.Generator[None, None, "Transaction"]:
"""
Called if using the low-level `transaction = await database.transaction()`
"""
return self.start().__await__()

def __call__(self, func: typing.Callable) -> typing.Callable:
def __call__(self, func: _CallableType) -> _CallableType:
"""
Called if using `@database.transaction()` as a decorator.
"""
Expand All @@ -363,7 +366,7 @@ async def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
async with self:
return await func(*args, **kwargs)

return wrapper
return wrapper # type: ignore

async def start(self) -> "Transaction":
self._connection = self._connection_callable()
Expand Down