{"record":{"id":"4fa43388d32c8635","repo":"PyO3/pyo3","slug":"the-weakref-referencetype-instance-should-be-val","errorCode":null,"errorMessage":"The 'weakref.ReferenceType' instance should be valid (non-null and actually a weakref reference)","messagePattern":"The 'weakref\\.ReferenceType' instance should be valid \\(non-null and actually a weakref reference\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/types/weakref/reference.rs","lineNumber":179,"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, PyWeakrefReference> {\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.ReferenceType' 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::platform::prelude::*;\n    use crate::types::any::{PyAny, PyAnyMethods};\n    use crate::types::weakref::{PyWeakrefMethods, PyWeakrefReference};\n    use crate::{Bound, PyResult, Python};\n\n    #[cfg(all(not(Py_LIMITED_API), Py_3_10))]\n    const CLASS_NAME: &str = \"<class 'weakref.ReferenceType'>\";\n    #[cfg(all(not(Py_LIMITED_API), not(Py_3_10)))]\n    const CLASS_NAME: &str = \"<class 'weakref'>\";\n","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/PyO3/pyo3/blob/ac9b6899d348be4d54614d060dea53a645a12e36/src/types/weakref/reference.rs#L161-L197","documentation":"Guard panic in the upgrade() implementation for Bound<PyWeakrefReference>. PyWeakref_GetRef returned an error, so the underlying pointer is not a valid weakref.ReferenceType object — the safe API assumes this can never occur because the wrapper type certifies a genuine weakref reference.","triggerScenarios":"Thrown at src/types/weakref/reference.rs:179 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Create references with PyWeakrefReference::new / weakref.ref() instead of casting","Validate foreign pointers with PyWeakref_Check before wrapping them","Avoid reusing objects across interpreter shutdown/initialization boundaries"],"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"}