pyo3-bindings
PyO3 conventions for exposing a Rust core to Python: pyclass/pymethods, PyErr/PyResult error mapping, GIL release, properties, maturin builds, and pytest. Load when generating or reviewing PyO3 Python
Files1
View on GitHubSKILL.mdinternal/builtins/bindings/pyo3/skills/pyo3-bindings/SKILL.md
GitHub- Use
#[pyclass]and#[pymethods]for Python-visible types. Use#[new]for constructors. - Map Rust
Result<T, E>to Python exceptions viaPyErr. UsePyResult<T>as return type. - Use
pyo3::typesfor conversions. Prefer&stroverStringin parameters, returnString. - Prefer
Bound<'py, T>overPy<T>for ergonomic, lifetime-checked references (PyO3 0.22+). - Release the GIL with
py.allow_threads()for CPU-intensive Rust code. Never hold GIL during I/O. - Use
#[getter]and#[setter]for properties. Implement__repr__and__str__for debugging. - Build with
maturin developfor local testing,maturin build --releasefor distribution. - Test bindings from Python using
pytest. Test both success and error paths. - Keep Python wrappers thin — business logic lives in Rust, Python provides ergonomic API.
- Anti-patterns:
.unwrap()in pyclass methods, blocking GIL for long operations, leaking Python objects.