Autospeccing decorator-transformed methods

Starting a conversation here as suggested by Nikita in [unittest.mock] Autospecced decorator-transformed methods · Issue #100596 · python/cpython · GitHub

The issue is that autospeccing in unittest.mock doesn’t work as expected when given a function which has its signature transformed by a decorator. For example:

def acquire(
    func: Callable[Concatenate[Any, int, _P], Awaitable[_T]]
) -> Callable[Concatenate[Any, _P], Awaitable[_T]]:
    def wrapper(self, *args: _P.args, **kwargs: _P.kwargs) -> _T:
        return func(self, 0, *args, **kwargs)
    return wrapper

class A:
    def test(self, foo: int) -> None:

a = A()
a.test()  # Works

with patch("__main__.a", autospec=True):
    a.test()  # TypeError: missing a required argument: 'foo'

I’m wondering if functools.wraps() could be improved to modify the signature of the function, so autospec ends up working as the user expects.

Nikita suggested adding a new kw to specify the signature. But, if it’s possible to automatically infer the information from typing annotations, so it “just works”, that’d be even better.