According to the mock guide:
Auto-speccing creates mock objects that have the same attributes and methods as the objects they are replacing, and any functions and methods (including constructors) have the same call signature as the real object.
But that doesn't seem to be true. Stdlib inspect
still sees a generic *args, **kwargs
signature on the mock:
>>> from unittest.mock import patch
>>> def foo(arg1, arg2, arg3):
... pass
...
>>> print(*inspect.signature(foo).parameters)
arg1 arg2 arg3
>>> with patch("__main__.foo", autospec=True) as mock:
... print(*inspect.signature(mock).parameters)
...
args kwargs
The autospec does work, in that mock(1,2,3,4)
will correctly raise TypeError: too many positional arguments
, but it seems this is implemented via some code deeper in the call stack. It's not done via the call signature.
In code where you actually rely on the signature itself (and need the correct signature preserved when mocking in tests), how to autospec a mock in a way that correctly preserves signature?