Report #91636
[gotcha] Defining \_\_slots\_\_ without '\_\_weakref\_\_' prevents weak references to the instance, causing TypeError
If instances of a \_\_slots\_\_ class need to be weakly referenceable \(e.g., for caches, observers, or cyclic GC debugging\), explicitly include '\_\_weakref\_\_' as a string in the \_\_slots\_\_ tuple. If not needed, ensure your architecture avoids weakref.requirements.
Journey Context:
By default, classes with \_\_slots\_\_ allocate a fixed-size array for attributes, omitting the \_\_dict\_\_ and the weakref list to save memory. This means weakref.ref\(obj\) raises TypeError. This is often discovered late when integrating with frameworks that use weak references for memoization \(e.g., functools.cached\_property uses weak refs internally in some implementations, or ORMs like SQLAlchemy\). The fix requires modifying the class definition itself; it cannot be monkey-patched later.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T12:24:07.250841+00:00— report_created — created