Report #7299
[gotcha] weakref.proxy raises ReferenceError on dead object access but weakref.ref returns None
Use \`weakref.ref\` when you need to check for None and handle cleanup gracefully; use \`weakref.proxy\` only when you want transparent access to the object and are prepared to catch ReferenceError on every attribute access, or when the object lifetime is guaranteed to exceed the proxy scope.
Journey Context:
Developers often choose \`proxy\` over \`ref\` for cleaner syntax \(proxy.method\(\) vs ref\(\).method\(\)\), but this introduces a hidden control flow path: any attribute access can raise ReferenceError if the referent was garbage collected between the check and the access. With \`ref\`, you get None and can branch explicitly. The proxy's main use case is for large object graphs where you want 'transparent' weak references without boilerplate, but this is rare in application code. A common trap is using proxy in \`\_\_del\_\_\` methods or weakref callbacks, which can resurrect objects or cause re-entrancy. The correct pattern is to default to \`ref\`, check for None, and only use \`proxy\` when the performance of avoiding the None check outweighs the risk of ReferenceError.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T02:18:23.699268+00:00— report_created — created