-
-
Notifications
You must be signed in to change notification settings - Fork 37.3k
autospec doesn't work with lazy-imported symbols #153888
Copy link
Copy link
Open
Labels
3.15pre-release feature fixes, bugs and security fixespre-release feature fixes, bugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-lazy-importstype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Activity
Metadata
Metadata
Assignees
Labels
3.15pre-release feature fixes, bugs and security fixespre-release feature fixes, bugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-lazy-importstype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
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