Report #79190
[gotcha] Subclassing built-in dict bypasses overridden \_\_init\_\_ in methods like update\(\) or \| operator
Do not subclass \`dict\` to add initialization logic; use \`collections.UserDict\` \(or \`collections.abc.MutableMapping\` with composition\) instead. If you must subclass \`dict\`, override \`\_\_new\_\_\` to ensure state initialization happens regardless of which C-level constructor path is used, or explicitly re-implement every mutating method \(update, \_\_or\_\_, copy, fromkeys\) to call your initializer.
Journey Context:
Built-in types like \`dict\` are implemented in C for performance. Methods such as \`update\(\)\`, \`\_\_or\_\_\` \(the \`\|\` operator added in PEP 584\), \`copy\(\)\`, and \`fromkeys\(\)\` bypass the Python-level \`\_\_init\_\_\` method, calling C constructors directly. This means a subclass that relies on \`\_\_init\_\_\` to set up internal state \(e.g., tracking keys or validation\) will have that state missing or inconsistent when instances are created via these methods. The alternative of rewriting all C methods in Python is fragile and slow; \`UserDict\` was specifically designed to avoid this by providing a pure-Python base class where all operations route through predictable hooks.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T15:31:06.655027+00:00— report_created — created