PyNamedTupleFields

Derive Macro PyNamedTupleFields 

Source
#[derive(PyNamedTupleFields)]
{
    // Attributes available to this derive:
    #[pyderive]
}
Expand description

Derive macro generating a _fields fn/Python class attribute.

It assumes all fields are get (e.g. get_all).

  • It should place #[derive(PyNamedTupleFields)] before #[pyclass].

§Implementation Note

This is experimental. Behavior may change in future releases.

§Example

use pyo3::{prelude::*, py_run};
use pyderive::*;

// Place before `#[pyclass]`
#[derive(PyNamedTupleFields)]
#[pyclass(get_all)]
struct PyClass {
    string: String,
    integer: i64,
    float: f64,
    tuple: (String, i64, f64),
}

Python::attach(|py| -> PyResult<()> {
    let Class = py.get_type::<PyClass>();

    py_run!(py, Class, r#"assert Class._fields == ('string', 'integer', 'float', 'tuple')"#);

    Ok(())
});