Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Improve typing #492

@sobolevn

Description

@sobolevn

Hi! I've noticed several places

Transaction.__call__

Right now it is defined as:

    def __call__(self, func: typing.Callable) -> typing.Callable:
        """
        Called if using `@database.transaction()` as a decorator.
        """

        @functools.wraps(func)
        async def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
            async with self:
                return await func(*args, **kwargs)

        return wrapper

databases/databases/core.py

Lines 356 to 366 in fbea46d

def __call__(self, func: typing.Callable) -> typing.Callable:
"""
Called if using `@database.transaction()` as a decorator.
"""
@functools.wraps(func)
async def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
async with self:
return await func(*args, **kwargs)
return wrapper

Which make all wrapped function to have (*Any, **Any) -> Any type. This is not ideal.
I propose to use TypeVar('_CallableType', bound=Callable) to save the exact type.

After:

from databases import Database

db = Database('url')

async def test(a: int) -> int:
    ...

reveal_type(db.transaction()(test))
# note: Revealed type is "def (a: builtins.int) -> typing.Coroutine[Any, Any, builtins.int]"

Notice, that signature is preserved.

Transaction.__await__

Right now there's a small problem with it:

from databases import Database

db = Database('url')

async def test() -> None:
    reveal_type(await db.transaction())
    # note: Revealed type is "Any"
    # But, should be the same as the next line:
    reveal_type(await db.transaction().start())
    # note: Revealed type is "databases.core.Transaction"

I propose to use the same type for await db.transaction()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions