#pyo3 #methods #automatic #derive #dunder #python #str

pyo3_special_method_derive

Automatically derive Python dunder methods for your Rust code

14 unstable releases (3 breaking)

0.4.2 Jul 20, 2024
0.4.1 Jul 19, 2024
0.3.1 Jul 19, 2024
0.3.0 Jun 25, 2024
0.1.3 Jun 17, 2024

#376 in Rust patterns

Download history 797/week @ 2024-07-14 690/week @ 2024-07-21 243/week @ 2024-07-28 276/week @ 2024-08-04 533/week @ 2024-08-11 385/week @ 2024-08-18 1095/week @ 2024-08-25 54/week @ 2024-09-01 926/week @ 2024-09-08 466/week @ 2024-09-15 776/week @ 2024-09-22 344/week @ 2024-09-29 1149/week @ 2024-10-06 381/week @ 2024-10-13 112/week @ 2024-10-20 381/week @ 2024-10-27

2,023 downloads per month

MIT license

16KB
326 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
~77K SLoC