Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Python

Generates Python Pydantic v2 models.

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]

Example

Rust:

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

Generated:

from pydantic import BaseModel
from typing import Optional

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

Dataclass Mode

Use Python dataclasses instead of Pydantic:

[python]
use_dataclass = true
pydantic_v2 = false
from dataclasses import dataclass
from typing import Optional

@dataclass
class User:
    id: str
    email: str
    age: Optional[int] = None

File Naming

Files use snake_case by default: UserProfileuser_profile.py

[python]
file_style = "kebab-case"   # user-profile.py
# or
file_style = "PascalCase"   # UserProfile.py