pyderive

Crates.iopyderive
lib.rspyderive
version
sourcesrc
created_at2024-06-29 14:21:06.363787
updated_at2024-12-13 00:40:23.866223
descriptionDerive macro of Python special methods and a class attributes for PyO3
homepage
repositoryhttps://github.com/paqira/pyderive
max_upload_size
id1287399
Cargo.toml error:TOML parse error at line 20, column 1 | 20 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
(paqira)

documentation

README

pyderive

Crates.io Version Crates.io MSRV GitHub Actions Workflow Status docs.rs Crates.io License

pyderive provides derive macros for Python spacial methods and a class attributes for PyO3.

// Enable `multiple-pymethods` feature of PyO3
use pyo3::prelude::*;
use pyderive::*;

// Place #[derive(PyNew, ...)] before #[pyclass]
#[derive(PyNew, PyMatchArgs, PyRepr, PyEq)]
#[pyclass(get_all)]
#[derive(PartialEq, Hash)]
struct MyClass {
    string: String,
    integer: i64,
    option: Option<String>
}
from rust_module import MyClass

# Derives __new__()
m = MyClass("a", 1, None)

# Derives __match_args__ (supports Pattern Matching by positional arguments)
match m:
    case MyClass(a, b, c):
        assert a == "a"
        assert b == 1
        assert c is None
    case _:
        raise AssertionError

# Derives __repr__(), calls Python repr() recursively
assert str(m) == "MyClass(string='a', integer=1, option=None)"
assert repr(m) == "MyClass(string='a', integer=1, option=None)"

# Derives __eq__() that depends on PartialEq trait
assert m == MyClass("a", 1, None)

This provides deriving following special methods and attributes;

Derive Macro Python Method/Attribute
PyNew __new__()
PyMatchArgs __match_args__
PyRepr __repr__()
PyStr __str__()
PyEq __eq__() and __ne__()
PyOrd __lt__(), __le__(), __gt__() and __ge__()
PyRichCmp ==, !=, >, >=, < and <= by __richcmp__()
PyIter __iter__()
PyReversed __reversed__()
PyLen __len__()
PyDataclassFields __dataclass_fields__
PyNumeric Numeric op methods (__add__() etc.)
PyBitwise Bitwise op methods (__and__() etc.)

The field attributes #[pyderive(..)] is used to customize the implementation, like dataclasses.field() of Python.

Module pyderive::ops and pyderive::convert provides derive macros that implement individual method that enumerating numeric type (__add__() etc.) and called by builtin functions (__int__() etc.).

It requires to enable multiple-pymethods feature of PyO3 because this may produce multiple #[pymethods].

Feature

PyRepr and PyStr

The methods implemented by PyRepr and PyStr are recursively calls repr() or str() like a Python dataclass.

PyEq and PyOrd

The implementation of PyEq and PyOrd does not use __richcmp__().

License

MIT or Apache-2.0

Commit count: 263

cargo fmt