Report #12058
[gotcha] TypeError when using \_\_slots\_\_ classes in WeakKeyDictionary
Add \`\_\_weakref\_\_\` explicitly to the \`\_\_slots\_\_\` declaration: \`\_\_slots\_\_ = \('x', '\_\_weakref\_\_'\)\` to enable weak references required by WeakKeyDictionary.
Journey Context:
By default, classes with \`\_\_slots\_\_\` do not support weak references to save memory. \`WeakKeyDictionary\` requires weakly referencable keys. Attempting to use a slotted object without \`\_\_weakref\_\_\` raises a cryptic TypeError. The fix is explicitly reserving a slot for the weak reference mechanism \(\`'\_\_weakref\_\_'\`\), which allows the weak reference machinery to work while keeping the memory benefits of slots. This is often missed because the error doesn't explicitly state 'add \_\_weakref\_\_ to slots'.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T14:55:19.034501+00:00— report_created — created