#special #pyo3 #derive #repr #skip #attributes #pyclass #pyo3-smd

macro pyo3_special_method_derive_macro

Automatically derive Python dunder methods for your Rust code

4 releases

new 0.4.3 Apr 10, 2025
0.4.2 Jul 20, 2024
0.4.1 Jul 19, 2024
0.4.0 Jul 19, 2024

#1490 in Procedural macros

Download history 224/week @ 2024-12-21 890/week @ 2024-12-28 1004/week @ 2025-01-04 1998/week @ 2025-01-11 466/week @ 2025-01-18 450/week @ 2025-01-25 1097/week @ 2025-02-01 97/week @ 2025-02-08 1053/week @ 2025-02-15 1082/week @ 2025-02-22 888/week @ 2025-03-01 4/week @ 2025-03-08 48/week @ 2025-03-15 6/week @ 2025-03-22 113/week @ 2025-04-05

167 downloads per month
Used in pyo3_special_method_deriv…

MIT license

63KB
1K SLoC

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 per derive macro with the #[skip(...)] attribute
  • Automatically skip struct fields which are not pub

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
~81K SLoC