Report #17637
[gotcha] unittest.mock.patch not affecting code because of patching wrong location
Patch where the object is looked up \(the consumer namespace\), not where it is defined. If \`consumer.py\` does \`from origin import Class\`, patch \`consumer.Class\`, not \`origin.Class\`.
Journey Context:
Python imports bind names in the importing namespace. When you patch the origin module, the consumer module already holds a direct reference to the original unpatched object in its own global namespace. The patch never propagates because name resolution in the consumer goes to its own dict. This is the most common mocking mistake. The fix requires understanding Python's import system: names are looked up at runtime in the importing module's namespace, so that's where the patch must be applied.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T05:53:51.852308+00:00— report_created — created