Skip to main content

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 generated 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)

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.1.1.tar.gz (235.9 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.1.1-cp314-cp314-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.14Windows x86-64

spicycrab-0.1.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

spicycrab-0.1.1-cp314-cp314-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

spicycrab-0.1.1-cp314-cp314-macosx_10_12_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for spicycrab-0.1.1.tar.gz
Algorithm Hash digest
SHA256 99e36fa41cdc029d0e15f4fa0c674ac9319ca6ec57715af303ef8ebccfc991f7
MD5 a9e1b03cf57bb4c443ca375595a2bba9
BLAKE2b-256 5dd056824474308fd5dcb50f09cd52b5cc9adf5fb5c383a37bb53540e607eedb

See more details on using hashes here.

Provenance

The following attestation bundles were made for spicycrab-0.1.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.1.1-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: spicycrab-0.1.1-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for spicycrab-0.1.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 0947ef3af5300075e747ebd8cb211ba7f3ad7a39e59f801ea83b5cbf2777213c
MD5 d15dec4b98cdc2fa922a447e49f356aa
BLAKE2b-256 d4597477cbe4b8e7382fdc9f92c4ce32cc75867d83e68865428b9bca01077715

See more details on using hashes here.

Provenance

The following attestation bundles were made for spicycrab-0.1.1-cp314-cp314-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.1.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for spicycrab-0.1.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 653bfbbd4b23db955ad9b40ffceda188542aae501333138f61b189fd39cdcf41
MD5 e8dc8a6d590f811f62b9199067aba9ed
BLAKE2b-256 d7e9950a4a23edaa063e3148aafaffa0ae4912ad85fa4950e3a2e35859f03df4

See more details on using hashes here.

Provenance

The following attestation bundles were made for spicycrab-0.1.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_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.1.1-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for spicycrab-0.1.1-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b199f5d32236fd4d14f04abb4ef06b6d431ed5840d41642bb42f15186144821f
MD5 e26dde26136f8bc989dad63ec1049234
BLAKE2b-256 8f61ccd6daa93f2f769c11abc1a16cbcaadeac2dd9b6889fec011bf9ecde2e54

See more details on using hashes here.

Provenance

The following attestation bundles were made for spicycrab-0.1.1-cp314-cp314-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.1.1-cp314-cp314-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for spicycrab-0.1.1-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6535c0ceb831a810ac105a473cd25f889aae5ef658c309706c68e50c2d42a830
MD5 a74d72b366438e20eb4d917159e72ef6
BLAKE2b-256 e353f53483051d263c3155ec80146e5d5b45759475f7d945491bcbadd6c4216c

See more details on using hashes here.

Provenance

The following attestation bundles were made for spicycrab-0.1.1-cp314-cp314-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.

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