pub trait PyRuntimeType {
// Required method
fn runtime_type_object(py: Python<'_>) -> PyResult<Bound<'_, PyAny>>;
}Expand description
Trait for Rust types that can be converted to Python type objects at runtime.
This trait is used by type_alias! to create runtime type aliases
that can be imported from Python. Unlike PyStubType which is used
for stub file generation, this trait is only needed when you want to register type aliases
at runtime.
§Implementing for Custom Types
For #[pyclass] types, use py.get_type::<Self>():
ⓘ
use pyo3::prelude::*;
use pyo3_stub_gen::runtime::PyRuntimeType;
#[pyclass]
struct MyClass;
impl PyRuntimeType for MyClass {
fn runtime_type_object(py: Python<'_>) -> PyResult<Bound<'_, PyAny>> {
Ok(py.get_type::<Self>().into_any())
}
}§Note
This trait is automatically implemented for common Rust types (primitives, collections, etc.)
and for types that use #[gen_stub_pyclass] derive macro.
Required Methods§
Sourcefn runtime_type_object(py: Python<'_>) -> PyResult<Bound<'_, PyAny>>
fn runtime_type_object(py: Python<'_>) -> PyResult<Bound<'_, PyAny>>
Returns the Python type object for this Rust type.
§Examples
i32→<class 'int'>String→<class 'str'>Option<T>→T | NoneVec<T>→list
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.