Report #36659
[gotcha] unittest.mock.patch has no effect when targeting imported names
Patch the object in the namespace where it is used \(the test subject's module\), not where it is defined; use 'package.module.ClassName' as imported in the consumer.
Journey Context:
Python imports create namespace bindings, not aliases. Patching 'origin\_module.Class' modifies the class in its home module, but if the code under test did 'from origin\_module import Class', it holds a direct reference to the original unpatched object in its own namespace. The patch appears to fail silently because the mock is injected into a namespace that the test subject never consults. The fix requires targeting 'consumer\_module.Class' to intercept the name binding in the scope where it is actually dereferenced.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T16:00:29.712565+00:00— report_created — created