Skip to content

Hybrid Projects

Guide for documenting Rust-Python hybrid projects using PyO3 and maturin.

[project]
name = "myproject"
version_from = "cargo"
[output]
path = "docs/api"
template = "mkdocs-material"
[rust]
crates = ["."]
[python]
package = "mypackage"

plissken automatically detects PyO3 bindings:

/// A data container exposed to Python.
///
/// This struct is available in Python as `mypackage.Container`.
#[pyclass]
#[derive(Debug, Clone)]
pub struct Container {
#[pyo3(get, set)]
pub value: i32,
}

In the Rust documentation, you’ll see:

Python API: mypackage.Container → links to Python doc page

#[pymethods]
impl Container {
/// Create a new container.
#[new]
pub fn new(value: i32) -> Self {
Self { value }
}
/// Double the stored value.
pub fn double(&mut self) {
self.value *= 2;
}
}
/// Process data and return results.
///
/// This function is exposed to Python as `mypackage.process`.
#[pyfunction]
pub fn process(data: Vec<String>) -> PyResult<Vec<String>> {
Ok(data.into_iter().map(|s| s.to_uppercase()).collect())
}

The #[pymodule] function names the Python module, and says what is in it:

#[pymodule]
fn mypackage(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Container>()?;
m.add_function(wrap_pyfunction!(process, m)?)?;
events::register(m)?; // a helper adds more
let io = PyModule::new(m.py(), "io")?; // the submodule mypackage.io
io.add_class::<Reader>()?;
m.add_submodule(&io)?;
Ok(())
}

plissken follows it: the helpers that get the module, the submodules that it makes, and the #[pymodule] functions that it wraps (wrap_pymodule!). The Python module is the package when they have the same name, else a module of the package (mypackage._native).

A binding that no #[pymodule] adds cannot be imported, so it has no Python page. In a crate with no #[pymodule], a binding goes to the module of its Rust module: mycrate::handlers is mypackage.handlers.

The Python bindings appear in docs with links back to Rust:

class Container:
"""A data container.
Attributes:
value: The stored integer value.
"""
def __init__(self, value: int) -> None:
"""Create a new container."""
...
def double(self) -> None:
"""Double the stored value."""
...

In the Python documentation, you’ll see:

Rust Implementation: mycrate::Container → links to Rust doc page

Rust Python
mycrate::Container mypackage.Container

Links appear in both directions automatically.

Method-level cross-references include anchors:

  • Rust Container::double → Python Container.double
  • Python Container.double → Rust Container::double

Write doc comments in Rust:

/// Process input data.
///
/// # Arguments
///
/// * `data` - Input strings to process
///
/// # Returns
///
/// Processed strings in uppercase.
#[pyfunction]
pub fn process(data: Vec<String>) -> Vec<String> {
// ...
}

plissken will show this documentation on both the Rust and Python pages.

For complex Python types, create .pyi stub files:

# mypackage/__init__.pyi
from typing import List
class Container:
value: int
def __init__(self, value: int) -> None: ...
def double(self) -> None: ...
def process(data: List[str]) -> List[str]: ...

PyO3 allows renaming:

#[pyclass(name = "PyContainer")]
pub struct Container { ... }
#[pyfunction(name = "py_process")]
pub fn process(...) { ... }

plissken uses the Python-visible names for cross-references.

Typical hybrid project layout:

myproject/
├── Cargo.toml
├── pyproject.toml
├── plissken.toml
├── src/
│ └── lib.rs # Rust implementation
├── mypackage/
│ ├── __init__.py # Re-exports
│ └── py.typed # PEP 561 marker
└── docs/
└── api/ # Generated docs