Report #42261
[gotcha] dict subclass \_\_missing\_\_ not triggered by .get\(\) or \`in\` operator
Override get\(\) explicitly to call \_\_getitem\_\_ if you need \_\_missing\_\_ logic, or use d\[key\] syntax directly. For existence checks, override \_\_contains\_\_ or use try/except KeyError instead of relying on \`in\` if \_\_missing\_\_ defines logical membership.
Journey Context:
Developers subclass dict and implement \_\_missing\_\_ to provide default values for missing keys, expecting it to be the universal fallback. However, \_\_missing\_\_ is strictly a hook for \_\_getitem\_\_ only. The dict.get\(\) method bypasses \_\_getitem\_\_ entirely, looking directly in the internal mapping, so it never triggers \_\_missing\_\_. Similarly, the \`in\` operator invokes \_\_contains\_\_, which checks the hash table directly without calling \_\_getitem\_\_. This leads to inconsistent APIs where d\[key\] returns a default value but key in d returns False and d.get\(key\) returns None, violating the principle of least astonishment.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T01:24:26.273829+00:00— report_created — created