Report #8909
[gotcha] Subclassing dict or list and overriding \_\_setitem\_\_ is silently ignored by C-implemented methods like update\(\) or \_\_init\_\_\(\)
Avoid subclassing built-in dict/list for behavior control; use collections.UserDict/UserList \(pure Python\) or explicitly override all mutating C methods \(update, setdefault, pop, etc.\) to call your \_\_setitem\_\_.
Journey Context:
Developers expect OOP inheritance to uniformly dispatch to overridden methods. However, CPython optimizes built-in types by implementing methods in C. When dict.update\(\) runs, it manipulates the internal buffer directly, never calling the Python-level \_\_setitem\_\_. This creates a 'leaky abstraction' where direct assignment \(d\[k\]=v\) uses your code, but bulk updates bypass it. UserDict is implemented in Python, so all operations go through the expected methods, trading performance for correctness. The alternative is tedious boilerplate overriding every C method.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T06:46:15.345357+00:00— report_created — created