Transpile Python to Safe, Idiomatic Rust
A Python-to-Rust transpiler with semantic verification and memory safety analysis - compiles typed Python to a native binary via a single CLI command.
Why it matters
Automate the translation of Python code to Rust, ensuring semantic equivalence and memory safety. This asset enables the creation of performant, secure native binaries from existing Python projects.
Outcomes
What it gets done
Convert Python code to Rust with type annotations.
Perform semantic verification to ensure code equivalence.
Analyze migration complexity from Python to Rust.
Conduct code quality analysis on transpiled Rust code.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/vb-depyler | bash Capabilities
Tools your agent gets
Convert Python code to Rust with type-driven transpilation using annotations
Analyze the complexity of migrating Python code to Rust
Verify semantic equivalence between original Python and transpiled Rust code
Perform code quality analysis on transpiled Rust code
Overview
Depyler MCP Server
A type-directed Python-to-Rust transpiler with memory safety analysis and property-based semantic verification, compiling Python straight to a native binary or to inspectable Rust source. Use when Python code with type annotations needs converting to Rust, or compiling Python directly to a native binary is the goal.
What it does
Translates annotated Python code into idiomatic Rust, preserving program semantics while adding compile-time safety guarantees, part of the PAIML Stack. Type-directed transpilation uses the source Python's own type annotations to generate the matching Rust types, memory safety analysis infers ownership and borrowing patterns automatically rather than requiring manual annotation, and semantic verification uses property-based testing to confirm the transpiled Rust actually behaves the same as the original Python. The pipeline itself runs Python AST through a high-level intermediate representation, type inference, a Rust AST, and finally code generation via syn/quote. The simplest path compiles Python straight to a native binary with depyler compile script.py; a lower-level path transpiles to a .rs file instead, optionally with --verify to run the semantic-equivalence checks, --trace to show the transpilation trace, or --explain to see the reasoning behind specific transformation decisions. A recursive Fibonacci function is a representative example - a typed Python function transpiles directly into:
fn fibonacci(n: i32) -> i32 {
if n <= 1 {
return n;
}
fibonacci(n - 1) + fibonacci(n - 2)
}
keeping the same recursive structure and type signature.
When to use - and when NOT to
Use it when Python code with type annotations needs converting to Rust for performance or memory-safety reasons, or when compiling Python directly to a native binary is the goal rather than working with the generated Rust source. 27 standard library modules are validated with 151 passing tests at 100% coverage, spanning serialization (json, struct, base64, csv), date/time, cryptography (hashlib, secrets), text processing, math, filesystem, data structures, functional tools, random, and sys. Supported Python language features cover typed functions, basic types, collections, control flow including match statements, comprehensions, generator expressions, exception handling mapped to Rust's Result<T, E>, classes and methods, async/await, and context managers. Explicitly not supported: dynamic features like eval and exec, runtime reflection, multiple inheritance, and monkey patching, since none of these have a sound Rust equivalent.
Capabilities
Single-command Python-to-binary compilation, standalone Python-to-Rust transpilation with optional semantic verification, 27 validated stdlib module mappings, a library API for programmatic transpilation with TranspileOptions, and a migration-complexity analysis command.
How to install
Install with cargo install depyler, requiring Rust 1.83.0 or later and Python 3.8+ for test validation; the current release reports a 92.7% compile rate on its stdlib corpus, 62.5% on a typed-CLI corpus, and 47.5% on an algorithms corpus, with an 80%+ single-shot compile rate across its internal example suite of 320 files.
Who it's for
Developers migrating performance- or safety-critical Python code to Rust who want a type-directed, semantically-verified transpiler rather than a manual rewrite, and who can work within its currently supported feature set. The project is MIT-licensed.
Source README
Depyler translates annotated Python code into idiomatic Rust, preserving program semantics while providing compile-time safety guarantees. Part of the PAIML Stack.
Table of Contents
- What's New in v4.1.1
- Features
- Installation
- Quick Start
- Usage
- Supported Python Features
- Stdlib Module Support
- Architecture
- Documentation
- Quality Metrics
- Contributing
- License
What's New in v4.1.1
str(dict.get(key, default))fix: No longer generates spurious.unwrap()calls- 25+ Python module mappings:
tempfile,datetime,os,shutil,glob, and more - E0433 import resolution: Improved handling of unresolved imports
[profile.test] opt-level=1: Faster test execution
Multi-Corpus Convergence (v3.25.0+)
All three external corpus targets met:
| Corpus | Compile Rate | Target |
|---|---|---|
| Tier 1 (stdlib) | 92.7% (38/41) | 80% |
| Tier 2 (typed-cli) | 62.5% (10/16) | 60% |
| Tier 5 (algorithms) | 47.5% (48/101) | 40% |
| Internal examples | 80% (256/320) | 80% |
Features
- Type-Directed Transpilation - Uses Python type annotations to generate appropriate Rust types
- Memory Safety Analysis - Infers ownership and borrowing patterns automatically
- Semantic Verification - Property-based testing to verify behavioral equivalence
- Single-Command Compilation - Compile Python to native binaries with
depyler compile - 27 Stdlib Modules - Production-ready support for common Python standard library modules
- 80%+ Single-Shot Compile Rate - Most Python files compile on first transpilation attempt
Installation
cargo install depyler
Requirements
- Rust 1.83.0 or later
- Python 3.8+ (for test validation)
Quick Start
Compile to Binary
The fastest way to use Depyler:
# Compile Python to a standalone binary
depyler compile script.py
# Run the compiled binary
./script
Transpile to Rust
# Transpile a Python file to Rust
depyler transpile example.py
# Transpile with semantic verification
depyler transpile example.py --verify
Example
Input (fibonacci.py):
def fibonacci(n: int) -> int:
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
Output (fibonacci.rs):
fn fibonacci(n: i32) -> i32 {
if n <= 1 {
return n;
}
fibonacci(n - 1) + fibonacci(n - 2)
}
Usage
Compilation Options
# Compile with custom output name
depyler compile script.py -o my_app
# Debug build (faster compilation)
depyler compile script.py --profile debug
# Release build (optimized, default)
depyler compile script.py --profile release
Transpilation Options
# Show transpilation trace
depyler transpile example.py --trace
# Explain transformation decisions
depyler transpile example.py --explain
# Analyze migration complexity
depyler analyze example.py
Library Usage
use depyler::{transpile_file, TranspileOptions};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let options = TranspileOptions::default()
.with_verification(true);
let rust_code = transpile_file("example.py", options)?;
println!("{}", rust_code);
Ok(())
}
Supported Python Features
| Feature | Status |
|---|---|
| Functions with type annotations | Supported |
| Basic types (int, float, str, bool) | Supported |
| Collections (List, Dict, Tuple, Set) | Supported |
| Control flow (if, while, for, match) | Supported |
| Comprehensions (list, dict, set) | Supported |
| Generator expressions | Supported |
| Exception handling (→ Result<T, E>) | Supported |
| Classes and methods | Supported |
| Async/await | Supported |
| Context managers | Supported |
Not Supported: Dynamic features (eval, exec), runtime reflection, multiple inheritance, monkey patching.
Stdlib Module Support
27 modules validated with 151 tests passing (100% coverage).
| Category | Modules |
|---|---|
| Serialization | json, struct, base64, csv |
| Date/Time | datetime, calendar, time |
| Cryptography | hashlib, secrets |
| Text | textwrap, re, string |
| Math | math, decimal, fractions, statistics |
| File System | os, pathlib, io |
| Data Structures | collections, copy, memoryview, array |
| Functional | itertools, functools |
| Random | random |
| System | sys |
See validation report for details.
Architecture
Python AST → HIR → Type Inference → Rust AST → Code Generation
| Component | Description |
|---|---|
| Parser | RustPython AST parser |
| HIR | High-level intermediate representation |
| Type System | Conservative type inference with annotation support |
| Verification | Property-based testing for semantic equivalence |
| Codegen | Rust code generation via syn/quote |
Documentation
- API Documentation
- MCP Quickstart
- Agent Mode Guide
- Developer Prompts
- Changelog
- TDD Book
- 🤖 Coursera Hugging Face AI Development Specialization - Build Production AI systems with Hugging Face in Pure Rust
Quality Metrics
| Metric | Value |
|---|---|
| Single-Shot Compile Rate | 80% (256/320 examples) |
| Line Coverage | 87.85% |
| Function Coverage | 92.85% |
| Total Tests | 23,335+ |
| Mutation Kill Rate | 75%+ |
Run coverage locally:
cargo llvm-cov nextest --workspace --lib --summary-only
MSRV
Minimum Supported Rust Version: 1.83
Cookbook
See depyler-cookbook for examples and recipes.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.