Bug report
Bug description:
unittest.mock.patch(target, autospec=True) reads unresolved lazy-import bindings directly from target.__dict__ and autospecs the LazyImportType proxy instead of the imported symbol.
from unittest.mock import patch
lazy from json import dumps
with patch("__main__.dumps", autospec=True) as mock_dumps:
mock_dumps({})
On CPython main (9d231cbc937f as of now), this raises:
TypeError: 'NonCallableMagicMock' object is not callable
Resolving dumps before patching works. This also affects patch.object, spec=True, and spec_set=True.
A practical example is concurrent.futures.ThreadPoolExecutor, which now uses an explicit lazy import: existing tests such as this one patch it with autospec=True and encounter the same issue.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux, macOS, Windows
Linked PRs
Bug report
Bug description:
unittest.mock.patch(target, autospec=True)reads unresolved lazy-import bindings directly fromtarget.__dict__and autospecs theLazyImportTypeproxy instead of the imported symbol.On CPython main (
9d231cbc937fas of now), this raises:Resolving
dumpsbefore patching works. This also affectspatch.object,spec=True, andspec_set=True.A practical example is
concurrent.futures.ThreadPoolExecutor, which now uses an explicit lazy import: existing tests such as this one patch it withautospec=Trueand encounter the same issue.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux, macOS, Windows
Linked PRs