#automatic #pyo3 #python #derive #methods #attributes #repr

macro pyo3_special_method_derive_macro

Automatically derive Python dunder methods for your Rust code

3 releases

0.4.2 Jul 20, 2024
0.4.1 Jul 19, 2024
0.4.0 Jul 19, 2024

#1913 in Procedural macros

Download history 1/week @ 2024-07-29 3/week @ 2024-08-26 37/week @ 2024-09-16 452/week @ 2024-09-23 322/week @ 2024-09-30 1312/week @ 2024-10-07 223/week @ 2024-10-14 115/week @ 2024-10-21 383/week @ 2024-10-28 746/week @ 2024-11-04 1766/week @ 2024-11-11

3,011 downloads per month
Used in pyo3_special_method_deriv…

MIT license

49KB
959 lines

pyo3-special-method-derive

This crate enables you to automatically derive Python dunder methods for your Rust crate using PyO3.

Key features

  • The following methods may be automatically derived on structs and enums:
    • __str__
    • __repr__
    • __dir__
    • __getattr__
    • __dict__
  • Support for structs and enums (only unit and complex enums due to a PyO3 limitation)
  • Support for skipping variants or fields with the #[pyo3_smd(skip)] attribute
  • Automatically skip struct fields which are not pub
  • Support for skipping variants or fields for __str__ or __repr__ differently with the #[pyo3_smd_str(skip)] and #[pyo3_smd_str(repr)] attributes

Example

#[pyclass]
#[derive(Dir, Str, Repr)]
struct Person {
    pub name: String,
    occupation: String,
    #[pyo3_smd(skip)]
    pub phone_num: String,
}

PyO3 feature note

To use pyo3-special-method-derive, you should enable the multiple-pymethods feature on PyO3:

pyo3 = { version = "0.22", features = ["multiple-pymethods"] }

Dependencies

~3.5MB
~79K SLoC