facet-python
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;
use to_python;
let python_code = ;
This generates:
:
:
: # 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:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.