Skip to main content

A Python to Rust transpiler for type-annotated Python code

Project description

spicycrab

A Python to Rust transpiler for type-annotated Python code.

Note: spicycrab and cookcrab are under active development. APIs, CLI options, and generated code may change frequently.

Features

  • crabpy - Transpile Python code to Rust
  • cookcrab - Generate Python stubs from Rust crates

Installation

python3 -m pip install spicycrab

Development Setup

# Clone the repository
git clone https://github.com/kushaldas/spicycrab.git
cd spicycrab

# Create virtual environment
uv venv
source .venv/bin/activate

# Install with all development and documentation dependencies
uv pip install -e ".[dev,docs]"

# Verify installation
crabpy --version
cookcrab --version

Quick Start

Transpile Python to Rust

crabpy transpile input.py -o rust_output -n my_project
crabpy transpile src/ -o rust_project/ -n my_project

Use Rust Crates in Python

# Generate stubs for a Rust crate
cookcrab generate clap -o /tmp/stubs

# Install the stubs
python3 -m pip install -e /tmp/stubs/clap_builder
python3 -m pip install -e /tmp/stubs/clap

# Write Python code using Rust crate types
cat > myapp.py << 'EOF'
from spicycrab_clap import Command, Arg, ArgMatches

def main() -> None:
    matches: ArgMatches = (
        Command.new("myapp")
        .arg(Arg.new("name").required(True))
        .get_matches()
    )
    name: str = matches.get_one("name").unwrap().clone()
    print(f"Hello, {name}!")
EOF

# Transpile to Rust
crabpy transpile myapp.py -o rust_myapp -n myapp

# Build and run
cd rust_myapp && cargo build --release
./target/release/myapp World

Requirements

  • Python 3.10+
  • All Python code must have type annotations

Supported Python Features

String Methods

The transpiler supports common Python string methods with automatic Rust equivalents:

Python Rust
s.upper() s.to_uppercase()
s.lower() s.to_lowercase()
s.strip() s.trim().to_string()
s.replace(a, b) s.replace(a, b)
s.startswith(x) s.starts_with(x)
s.endswith(x) s.ends_with(x)
s.split(sep) s.split(sep).collect::<Vec<_>>()
s.join(iter) iter.join(&s)
s.isdigit() s.chars().all(|c| c.is_ascii_digit())
s.find(x) >= 0 s.contains(x)
s.find(x) == -1 !s.contains(x)

Index Operations

Integer variables used for indexing are automatically cast to usize:

# Python
values: list[int] = [1, 2, 3]
i: int = 0
while i < len(values):
    print(values[i])
    i = i + 1
// Generated Rust
let values: Vec<i64> = vec![1, 2, 3];
let mut i: i64 = 0;
while (i as usize) < values.len() {
    println!("{}", values[i as usize]);
    i += 1;
}

Membership Operators

The in and not in operators are supported:

if x in container:      # -> container.contains(&x)
if x not in container:  # -> !container.contains(&x)

Error Handling

Functions returning Result[T, E] automatically get ? operator propagation:

def might_fail() -> Result[int, str]:
    return Ok(42)

def caller() -> Result[int, str]:
    value: int = might_fail()  # Automatically gets ? operator
    return Ok(value + 1)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spicycrab-0.0.1.tar.gz (170.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

spicycrab-0.0.1-cp311-cp311-win_amd64.whl (956.7 kB view details)

Uploaded CPython 3.11Windows x86-64

spicycrab-0.0.1-cp311-cp311-macosx_11_0_arm64.whl (945.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

spicycrab-0.0.1-cp311-cp311-macosx_10_12_x86_64.whl (996.3 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

spicycrab-0.0.1-cp38-cp38-manylinux_2_24_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.24+ x86-64

File details

Details for the file spicycrab-0.0.1.tar.gz.

File metadata

  • Download URL: spicycrab-0.0.1.tar.gz
  • Upload date:
  • Size: 170.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spicycrab-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3f14b92a07b5b6d4fa82a45b83bc8e49e6bda69537e549994fe80f1973e4d68a
MD5 d3d63d0548ea26ab04fefd552086f53d
BLAKE2b-256 e527d63ec9dca3a8a2960a17b0fc19518d53f9fb108d01d422254c12d90e68e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for spicycrab-0.0.1.tar.gz:

Publisher: release.yml on kushaldas/spicycrab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spicycrab-0.0.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: spicycrab-0.0.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 956.7 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spicycrab-0.0.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a95b40722f7a5b826e8d093fedc03f51a69ec16dacc0d23fa11233f7daa22c93
MD5 f966cf62f54917acc3e1482711934a87
BLAKE2b-256 f543176bbc523fc58db0b52cf089451baa0b5a67d4cea25d10990ddef5904d32

See more details on using hashes here.

Provenance

The following attestation bundles were made for spicycrab-0.0.1-cp311-cp311-win_amd64.whl:

Publisher: release.yml on kushaldas/spicycrab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spicycrab-0.0.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for spicycrab-0.0.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 19a38197e90f2744557e8a026f0737488c33afba12dc5bb90000b744ae987cda
MD5 94afd0cbb1d998b370af6ddcc1a23b4a
BLAKE2b-256 f2305d86fc92aa7fe52fa3cc1c545eaeb7ff9dcb74d0218b54b663f3ae5d4b4b

See more details on using hashes here.

Provenance

The following attestation bundles were made for spicycrab-0.0.1-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: release.yml on kushaldas/spicycrab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spicycrab-0.0.1-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for spicycrab-0.0.1-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e7a64d5c47bec9ac732b01a7ef503e4af2ff8f5f2778148ccae78fbc403c6675
MD5 c9eba851a8d792d528736da6f9786d99
BLAKE2b-256 8c7c7f587b0eb18ae13fe43df1e7539e27fdd0f7af96a178e745bdcdc310ba7a

See more details on using hashes here.

Provenance

The following attestation bundles were made for spicycrab-0.0.1-cp311-cp311-macosx_10_12_x86_64.whl:

Publisher: release.yml on kushaldas/spicycrab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spicycrab-0.0.1-cp38-cp38-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for spicycrab-0.0.1-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 88940684de2be8aef20f7e51172da1f334df3bbe26f92430984add4ca9261993
MD5 f998ba8ac865c3f97191aab32f3fa4b0
BLAKE2b-256 6688a32ddbf3aabef06825dc6e5f20d49686b96514ca19b631a99538db6fa5d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for spicycrab-0.0.1-cp38-cp38-manylinux_2_24_x86_64.whl:

Publisher: release.yml on kushaldas/spicycrab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page