Report #13659
[gotcha] Class with \_\_slots\_\_ does not support weak references without explicit \_\_weakref\_\_
Include '\_\_weakref\_\_' in the \_\_slots\_\_ tuple if weak references are needed
Journey Context:
When using \`\_\_slots\_\_\` to save memory, Python omits both \`\_\_dict\_\_\` and \`\_\_weakref\_\_\` slots by default. This causes \`weakref.ref\(\)\` and \`WeakKeyDictionary\` to raise TypeError with a confusing message about the object not supporting weak references. The error doesn't mention \`\_\_slots\_\_\`, leading developers to think weakref is broken or the object is somehow immutable. The tradeoff is memory: including \`\_\_weakref\_\_\` adds per-instance overhead. The fix must be explicit because omitting it is the default behavior to save memory.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T19:19:39.292087+00:00— report_created — created