{"record":{"id":"ff1936280318954d","repo":"PyO3/pyo3","slug":"failed-to-create-type-object-for-type-name","errorCode":null,"errorMessage":"failed to create type object for `{type_name}`","messagePattern":"failed to create type object for `(.+?)`","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"src/impl_/pyclass/lazy_type_object.rs","lineNumber":260,"sourceCode":") -> PyResult<()> {\n    // We hold the GIL: the dictionary update can be considered atomic from\n    // the POV of other threads.\n    for (key, val) in items {\n        crate::err::error_on_minusone(py, unsafe {\n            ffi::PyObject_SetAttrString(type_object, key.as_ptr(), val.as_ptr())\n        })?;\n    }\n    Ok(())\n}\n\n// This is necessary for making static `LazyTypeObject`s\nunsafe impl<T> Sync for LazyTypeObject<T> {}\n\n/// Used in the macro-expanded implementation of `type_object_raw` for `#[pyclass]` types\n#[cold]\npub fn type_object_init_failed(py: Python<'_>, err: PyErr, type_name: &str) -> ! {\n    err.write_unraisable(py, None);\n    panic!(\"failed to create type object for `{type_name}`\")\n}\n\n/// The full macro-expanded implementation of `type_object_raw` for `#[pyclass]` types, kept\n/// out-of-line here to reduce the amount of macro-generated code.\n#[inline]\npub fn pyclass_type_object_raw<T: PyClass>(py: Python<'_>) -> *mut ffi::PyTypeObject {\n    use crate::types::PyTypeMethods;\n    T::lazy_type_object()\n        .get_or_try_init(py)\n        .unwrap_or_else(|e| type_object_init_failed(py, e, <T as PyClass>::NAME))\n        .as_type_ptr()\n}\n\n#[cold]\nfn wrap_in_runtime_error(py: Python<'_>, err: PyErr, message: String) -> PyErr {\n    let runtime_err = PyRuntimeError::new_err(message);\n    runtime_err.set_cause(py, Some(err));\n    runtime_err","sourceCodeStart":242,"sourceCodeEnd":278,"githubUrl":"https://github.com/PyO3/pyo3/blob/ac9b6899d348be4d54614d060dea53a645a12e36/src/impl_/pyclass/lazy_type_object.rs#L242-L278","documentation":"type_object_init_failed is called from pyclass_type_object_raw when creating a #[pyclass]'s type object fails (e.g. during lazy type object initialization). It writes the underlying PyErr as unraisable and then panics, since the type object can never be produced.","triggerScenarios":"First access to a #[pyclass] type object when PyType creation fails — usually because __new__/base class setup failed, module registration context is invalid, or the interpreter is in an error/initialization-failure state.","commonSituations":"Using a pyclass before/beyond a valid module init; interpreter shutdown while creating the type; invalid pyclass attributes (bad base, duplicate name) surfacing as PyErr at type creation.","solutions":["Inspect the unraisable PyErr printed to stderr just before the panic — it names the real cause","Ensure the pyclass is registered/initialized in a healthy interpreter context (correct module init, not during finalization)","Fix pyclass definition issues: conflicting names, invalid base classes, or misconfigured #[pyclass(...)] options"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// register classes only during module init, while the interpreter is healthy\n#[pymodule]\nfn mymod(m: &Bound<'_, PyModule>) -> PyResult<()> {\n    m.add_class::<MyClass>()?;\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":"// capture the unraisable cause before the abort by installing a hook in tests\nPython::with_gil(|py| unsafe {\n    ffi::PyErr_SetInterrupt /* or run under a test harness that reads unraisable output */;\n});","preventionTips":["Only create/access pyclass type objects during normal module initialization","Read the unraisable PyErr printed before the panic to find the root cause","Avoid duplicate class names and invalid base classes in #[pyclass] definitions"],"tags":["pyclass","type-object","initialization","panic"],"backgroundTag":"type-object-initialization-failed","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"}