Report #52156
[gotcha] Enum member identity check fails after pickle.loads or module reload despite same value
Always use equality \(==\) not identity \(is\) when comparing Enum members, especially after serialization. For dictionary keys or set membership, use the enum value \(member.value\) or ensure consistent module importing.
Journey Context:
Enum members are singletons only within a specific class definition in memory. When you pickle and unpickle an Enum member, Python creates a new instance by looking up the value in the Enum class. If the module was reloaded between pickle and unpickle, or if multiple copies of the module exist \(common in complex import scenarios or multiprocessing\), you get distinct objects for the same logical enum value. This breaks \`is\` comparisons and can cause subtle bugs in state machines or dispatch dictionaries that expect singleton identity. The defense is strict use of equality for value comparison, or storing canonical primitive values \(strings/integers\) for serialization boundaries and reconstructing enums on the receiving end.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T18:02:18.415778+00:00— report_created — created