You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
autospec doesn't work with lazy-imported symbols #153888
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.
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.
The linked PR has fallen stale for the last 2 months. With the 3.15 final cut coming up, I'm putting up a new PR which also addresses the open comments left on the original PR. #157768
Activity
Resolve lazy imports before using them as a mock spec
brittanyrey commentedon Sep 18, 2026
The linked PR has fallen stale for the last 2 months. With the 3.15 final cut coming up, I'm putting up a new PR which also addresses the open comments left on the original PR. #157768
gh-153888: Resolve lazy imports before using them as a mock spec (GH-…
[3.15] gh-153888: Resolve lazy imports before using them as a mock sp…
test: fix flaky autospec of lazy-imported logger on 3.15 (#1209)
Resolve a lazy import before unittest.mock specs it