Report #7654
[gotcha] Subclassing dict/list: C-level methods bypass overridden Python methods like \_\_setitem\_\_
Use collections.UserDict/UserList instead of subclassing built-in dict/list, or explicitly override all relevant methods \(update, setdefault, etc.\) rather than relying on \_\_setitem\_\_.
Journey Context:
When you subclass dict and override \_\_setitem\_\_, you expect all insertion paths to respect your logic. However, CPython's built-in dict uses C-level implementations for methods like update\(\), setdefault\(\), and the constructor, which bypass Python-level \_\_setitem\_\_ overrides. This leads to invariant violations where direct assignment triggers your validation, but dict.update\(\) bypasses it. UserDict is implemented in Python and ensures all paths respect overrides, at a modest performance cost. Alternatively, you must override every mutating method individually, which is error-prone.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T03:19:59.133037+00:00— report_created — created