#python-bindings #py #python #bindings

py-rs

generate python bindings from rust types

2 releases

0.1.1 Feb 24, 2025
0.1.0 Feb 23, 2025

#131 in WebAssembly

MIT license

84KB
1K SLoC

py-rs

logo
py-rs

Generate python type declarations from rust types

Why?

When building an api in rust, data structures have to be shared between backend and client. Using this library, you can easily generate python bindings to your rust structs & enums so that you can keep your types in one place.

Note: This is a work in progress. There are still some features of ts-rs that are not tested or converted.

How?

py-rs exposes a single trait, PY. Using a derive macro, you can implement this interface for your types. Then, you can use this trait to obtain the Python bindings. We recommend doing this in your tests. See the example and the docs.

Get started

[dependencies]
py-rs = "0.1.0"
use py_rs::PY;

#[derive(PY)]
#[py(export)]
struct User {
    user_id: i32,
    first_name: String,
    last_name: String,
}

When running cargo test or cargo test export_bindings, the Python bindings will be exported to the file bindings/User.py and will contain the following code:

from pydanic import BaseModel

class User(BaseModel):
    user_id: int
    first_name: str
    last_name: str

Features

  • generate type declarations from rust structs
  • generate union declarations from rust enums
  • generate necessary imports when exporting to multiple files
  • serde compatibility
  • generic types
  • support for ESM imports

Note: not all the features are tested for Python.

cargo features

Feature Description
serde-compat Enabled by default
See the "serde compatibility" section below for more information.
format Enables formatting of the generated Python bindings.
Currently, this unfortunately adds quite a few dependencies.
no-serde-warnings By default, warnings are printed during build if unsupported serde attributes are encountered.
Enabling this feature silences these warnings.
serde-json-impl Implement PY for types from serde_json
chrono-impl Implement PY for types from chrono
bigdecimal-impl Implement PY for types from bigdecimal
url-impl Implement PY for types from url
uuid-impl Implement PY for types from uuid
bson-uuid-impl Implement PY for bson::oid::ObjectId and bson::uuid
bytes-impl Implement PY for types from bytes
indexmap-impl Implement PY for types from indexmap
ordered-float-impl Implement PY for types from ordered_float
heapless-impl Implement PY for types from heapless
semver-impl Implement PY for types from semver
smol_str-impl Implement PY for types from smol_str
tokio-impl Implement PY for types from tokio

If there's a type you're dealing with which doesn't implement PY, use either #[py(as = "..")] or #[py(type = "..")], or open a PR.

serde compatability

With the serde-compat feature (enabled by default), serde attributes can be parsed for enums and structs. Supported serde attributes:

  • rename
  • rename-all
  • rename-all-fields
  • tag
  • content
  • untagged
  • skip
  • flatten
  • default

Note: skip_serializing and skip_deserializing are ignored. If you wish to exclude a field from the generated type, but cannot use #[serde(skip)], use #[py(skip)] instead.

When py-rs encounters an unsupported serde attribute, a warning is emitted, unless the feature no-serde-warnings is enabled.

Contributing

Contributions are always welcome! Feel free to open an issue, discuss using GitHub discussions or open a PR. See CONTRIBUTING.md

MSRV

The Minimum Supported Rust Version for this crate is 1.63.0

License: MIT

Dependencies

~0.2–11MB
~128K SLoC