Added
mock.AsyncMock(autospec=X)now gets the same autospec support asMock/MagicMock. Previously autospec was treated as an arbitrary config kwarg onAsyncMock(silently becoming a child mock named.autospec), so async classes /methods got no autospec support at all.- An explicit
spec=[...]/spec_set=[...]list alongsideautospec=is now handled as a whitelist instead of being replaced. This makes it possible to allow instance attributes only ever assigned in__init__(invisible to autospec'sdir()-based discovery) alongside autospec's own signature checking. This is something with no clean upstream equivalent oncespec_set=Trueis in effect.
Fixes
- Async
mock.patchtargets now enforce call signatures correctly, with no spurious warnings. An async def patch target makescreate_autospecreturn anAsyncMock, whose signature check only runs inside the awaited coroutine body; the fixture's synchronous check was discarding it, so on Python 3.13+ enforcement was silently dropped entirely. - Patching a builtin on a module (
open,print, ...) no longer raisesAttributeErrorunder the defaultautospec=True. Autospecs are now skipped for builtins, mirroring stdlib's behaviour. Autospeccing builtins offers little benefit as there are only a few cases in which that is needed; the most common case would beopen, butmock.mock_open()can be used for that instead. - Autospec now propagates recursively into nested non-callable attributes. A class-level attribute that was itself a spec-able object (e.g.
Outer.inner = Inner()) previously got a plain, unspecced child mock, so calls likemock.Mock(autospec=Outer)().inner.method(...)went completely unchecked.
Docs
- README now documents
mock.AsyncMockautospec support and thespec=/spec_set=whitelist pattern, with runnable examples. - Removed a stale known-limitation entry ("patching a builtin on a module raises AttributeError") that no longer applies.
Full Changelog: 0.2.0...0.3.0