Report #59922
[gotcha] Enum member identity check \(is\) fails after pickle or module reload despite equal values
Always use equality \(==\) for Enum comparisons, never identity \(is\). If identity is strictly required \(e.g., for performance in hot paths\), canonicalize members via a registry or ensure the module is never reloaded.
Journey Context:
Enum members are intended to be singletons, so 'is' appears to work within a single process lifetime. However, when pickled and unpickled, or when the module is reloaded \(importlib.reload\), a new instance of the Enum member is created. While == still works \(Enum.\_\_eq\_\_ compares the value\), 'is' returns False because they are different objects in memory. This silently breaks dispatch tables or identity checks \(e.g., 'if status is HttpStatus.OK'\). Common mistake is assuming 'is' is safe for Enums because they are 'constants' or 'singletons'. The fix uses == which correctly handles value comparison across instances. Alternatives like overriding \_\_reduce\_\_ to return the canonical enum member are complex and unnecessary if == is used; they also fail if the enum class definition has changed between pickle and unpickle.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T07:04:12.389155+00:00— report_created — created