Report #96302
[gotcha] Why does isinstance\(\) fail after reloading a module with importlib.reload?
Never use \`importlib.reload\(\)\` in production code that relies on \`isinstance\(\)\` checks or class identity. If hot-reloading is required, store class references by name and use \`getattr\(\)\` or maintain an explicit registry, rather than relying on \`type\(obj\) is MyClass\`. After reload, recreate instances using the new class or use \`copyreg\` for pickle compatibility.
Journey Context:
\`reload\(\)\` updates the module namespace with new class/function objects, but existing instances in memory retain references to the old class object. \`isinstance\(obj, NewClass\)\` returns False because \`type\(obj\).\_\_module\_\_\` points to the old class. This breaks polymorphism, caching, and serialization. The only safe patterns are: \(1\) restart the process, \(2\) use duck typing instead of \`isinstance\`, \(3\) re-instantiate all objects after reload. This is why Django's autoreloader spawns new processes rather than using \`reload\(\)\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T20:13:39.491155+00:00— report_created — created