Skip to main content

Crate typewriter_python

Crate typewriter_python 

Source
Expand description

§typewriter-python

Python Pydantic v2 / dataclass emitter for the typewriter type sync SDK. Generates .py files with Pydantic BaseModel classes.

§Type Mappings

Rust TypePython Type
Stringstr
boolbool
u8, u16, u32, u64, i8, i16, i32, i64int
f32, f64float
UuidUUID
DateTime<Utc>datetime
Option<T>Optional[T] = None
Vec<T>list[T]
HashMap<K, V>dict[K, V]
Custom struct/enumClass reference

§Configuration

In typewriter.toml:

[python]
output_dir = "./generated/python"
file_style = "snake_case"   # snake_case (default), kebab-case, or PascalCase
pydantic_v2 = true          # Use Pydantic v2 (default: true)
use_dataclass = false       # Use @dataclass instead of BaseModel

§Example

Rust Input:

#[derive(TypeWriter)]
#[sync_to(python)]
pub struct User {
    pub id: String,
    pub email: String,
    pub age: Option<u32>,
}

Generated Python:

from pydantic import BaseModel
from typing import Optional


class User(BaseModel):
    id: str
    email: str
    age: Optional[int] = None

Structs§

PythonMapper
Python language mapper.