facet-python

Crates.iofacet-python
lib.rsfacet-python
version0.43.2
created_at2026-01-22 16:54:28.557305+00
updated_at2026-01-23 18:06:40.74262+00
descriptionGenerate Python type definitions from facet type metadata
homepagehttps://facet.rs
repositoryhttps://github.com/facet-rs/facet
max_upload_size
id2062165
size57,348
Amos Wenger (fasterthanlime)

documentation

README

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:

GitHub Sponsors Patreon

...along with corporate sponsors:

AWS Zed Depot

...without whom this work could not exist.

Special thanks

The facet logo was drawn by Misiasart.

License

Licensed under either of:

at your option.

Commit count: 3380

cargo fmt