facet-python 0.43.2

Generate Python type definitions from facet type metadata
Documentation

facet-python

Coverage Status crates.io documentation MIT/Apache-2.0 licensed Discord

Generate Python type definitions from facet type metadata.

Overview

This crate uses facet's reflection capabilities to generate Python type hints and TypedDicts from any Rust type that implements Facet. This enables type-safe interop when your Rust code exchanges data with Python.

Example

use facet::Facet;
use facet_python::to_python;

#[derive(Facet)]
struct User {
    name: String,
    age: u32,
    email: Option<String>,
}

let python_code = to_python::<User>(false);

This generates:

from typing import TypedDict, Required, NotRequired

class User(TypedDict, total=False):
    name: Required[str]
    age: Required[int]
    email: str  # Optional fields become NotRequired

Type Mappings

Rust Type Python Type
String, &str str
i32, u32, etc. int
f32, f64 float
bool bool
Vec<T> list[T]
Option<T> T (NotRequired in TypedDict)
HashMap<K, V> dict[K, V]
Struct TypedDict
Enum Union[...] of variants

Features

  • Recursive types: Handles nested structs and enums
  • Documentation: Preserves doc comments as Python docstrings
  • Reserved keywords: Automatically handles Python reserved words as field names
  • Generic support: Maps Rust generics to Python type parameters

Sponsors

Thanks to all individual sponsors:

...along with corporate sponsors:

...without whom this work could not exist.

Special thanks

The facet logo was drawn by Misiasart.

License

Licensed under either of:

at your option.