Report #11051
[gotcha] Pickle unpickling bypasses \_\_init\_\_ leaving object in invalid state
Implement \_\_getnewargs\_ex\_\_, \_\_getstate\_\_, and \_\_setstate\_\_ to explicitly control reconstruction; never rely on \_\_init\_\_ validation being called during unpickling.
Journey Context:
Pickle reconstructs objects by creating a new empty instance \(using \_\_new\_\_\) and then populating its \_\_dict\_\_ directly \(or via \_\_setstate\_\_\), completely skipping \_\_init\_\_. This means any validation, type checking, or initialization logic in \_\_init\_\_ \(e.g., checking that a list is non-empty or that a path exists\) is not executed. This leads to objects that violate class invariants—appearing valid but containing None or illegal values that crash later in unrelated code. The only reliable way to ensure consistency is to implement \_\_getnewargs\_ex\_\_ \(for immutable objects\) or \_\_getstate\_\_/\_\_setstate\_\_ \(for mutable objects\) to manually reconstruct the object, potentially calling \_\_init\_\_ explicitly within \_\_setstate\_\_ if necessary.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T12:20:49.813290+00:00— report_created — created