Report #60879
[gotcha] dict subclass \_\_setitem\_\_ bypassed by update method
Use collections.UserDict for custom mapping behavior instead of subclassing dict or list. If you must subclass dict, explicitly override update\(\) and \_\_init\_\_\(\) to call your custom \_\_setitem\_\_ logic.
Journey Context:
CPython's dict.update\(\) is implemented in C and bypasses Python-level \_\_setitem\_\_ for performance. This causes silent failures when using dict subclassing for validation or change tracking \(e.g., logging all inserts\). Users assume update\(\) respects overrides because it is a public API, but it operates at the C level. UserDict is implemented in Python and delegates all operations through the expected methods, ensuring consistency at the cost of speed. The tradeoff is performance vs correctness: subclassing dict is fast but fragile; UserDict is robust but slower.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T08:40:28.458324+00:00— report_created — created