X Tutup
The Wayback Machine - https://web.archive.org/web/20250608120639/https://github.com/python/cpython/issues/100596
Skip to content

[unittest.mock] Autospecced decorator-transformed methods #100596

Closed as not planned
@Dreamsorcerer

Description

@Dreamsorcerer

If a decorator transforms a method, the autospecced mock will be wrong.

e.g. Something like:

def acquire(
    func: Callable[Concatenate[_Client, Connection, _P], Awaitable[_T]]
) -> Callable[Concatenate[_Client, _P], Awaitable[_T]]:

    @functools.wraps(func)
    async def wrapper(self: _Client, *args: _P.args, **kwargs: _P.kwargs) -> _T:
        conn = await self._pool.acquire()
        return await func(self, conn, *args, **kwargs)

    return wrapper

Will result in the mock requiring the first connection parameter when called, but the real method will already have that parameter provided by the decorator.

I assume autospec is currently only looking at the wraps information when inspecting the signature, but maybe it could make use of the type annotations to provide a more accurate mock.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup