Report #5471
[gotcha] Subclassing dict/list and C-level methods ignoring Python overrides
Do not subclass \`dict\` or \`list\` to customize item storage logic \(e.g., validation, logging\). Instead, subclass \`collections.UserDict\` or \`collections.UserList\`, or use \`collections.abc.MutableMapping\` with composition \(wrap a dict\). If you must subclass built-ins, explicitly override \`\_\_init\_\_\`, \`update\`, and \`setdefault\`, but accept that C-paths \(e.g., unpickling\) may bypass Python code.
Journey Context:
CPython optimizes built-in \`dict.\_\_setitem\_\_\` and \`list.append\` in C. When you subclass \`dict\` and override \`\_\_setitem\_\_\` to add validation, methods like \`dict.update\(\)\`, \`dict.\_\_init\_\_\(\)\`, \`dict.setdefault\(\)\`, or unpickling may bypass your Python \`\_\_setitem\_\_\` and call the C implementation directly. This leads to silent validation bypasses \(e.g., invalid data entering a 'validated' dict\). \`collections.UserDict\` avoids this by not being a built-in type; all methods route through Python-level \`\_\_setitem\_\_\`, ensuring overrides are respected.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T21:20:01.024989+00:00— report_created — created