{"record":{"id":"b9b83ac5bdf1de4a","repo":"PyO3/pyo3","slug":"the-weakref-proxytype-or-weakref-callableproxy","errorCode":null,"errorMessage":"The 'weakref.ProxyType' (or `weakref.CallableProxyType`) instance should be valid (non-null and actually a weakref reference)","messagePattern":"The 'weakref\\.ProxyType' \\(or `weakref\\.CallableProxyType`\\) instance should be valid \\(non-null and actually a weakref reference\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/types/weakref/proxy.rs","lineNumber":171,"sourceCode":"            }\n        }\n\n        let py = object.py();\n        inner(\n            object,\n            callback\n                .into_pyobject_or_pyerr(py)?\n                .into_any()\n                .as_borrowed(),\n        )\n    }\n}\n\nimpl<'py> PyWeakrefMethods<'py> for Bound<'py, PyWeakrefProxy> {\n    fn upgrade(&self) -> Option<Bound<'py, PyAny>> {\n        let mut obj: *mut ffi::PyObject = core::ptr::null_mut();\n        match unsafe { ffi::compat::PyWeakref_GetRef(self.as_ptr(), &mut obj) } {\n            core::ffi::c_int::MIN..=-1 => panic!(\"The 'weakref.ProxyType' (or `weakref.CallableProxyType`) instance should be valid (non-null and actually a weakref reference)\"),\n            0 => None,\n            1..=core::ffi::c_int::MAX => Some(unsafe { obj.assume_owned_unchecked(self.py()) }),\n        }\n    }\n}\n\n#[cfg(test)]\nmod tests {\n    use crate::exceptions::{PyAttributeError, PyReferenceError, PyTypeError};\n    use crate::platform::prelude::*;\n    use crate::types::any::{PyAny, PyAnyMethods};\n    use crate::types::weakref::{PyWeakrefMethods, PyWeakrefProxy};\n    use crate::{Bound, PyResult, Python};\n\n    #[cfg(all(Py_3_13, not(Py_LIMITED_API)))]\n    const DEADREF_FIX: Option<&str> = None;\n    #[cfg(all(not(Py_3_13), not(Py_LIMITED_API)))]\n    const DEADREF_FIX: Option<&str> = Some(\"NoneType\");","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/PyO3/pyo3/blob/ac9b6899d348be4d54614d060dea53a645a12e36/src/types/weakref/proxy.rs#L153-L189","documentation":"Guard panic in the upgrade() implementation for Bound<PyWeakrefProxy>. It fires when PyWeakref_GetRef reports an error, meaning the stored pointer is not a genuine weakref.proxy / weakref.CallableProxyType instance — a violation of the type invariant that the wrapper is supposed to guarantee, reachable only through unsafe construction or FFI misuse.","triggerScenarios":"Thrown at src/types/weakref/proxy.rs:171 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Construct proxies via PyWeakrefProxy::new / weakref.proxy() so the type invariant holds","When receiving pointers from FFI, validate with PyWeakref_Check before upgrading","Do not transmute or cast unrelated Bound<PyAny> values to Bound<PyWeakrefProxy>"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"ac9b6899d348be4d54614d060dea53a645a12e36","analyzedAt":"2026-09-05T09:20:35.319Z","contentChangedAt":"2026-09-05T09:20:35.319Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}