Skip to content

Module mapping coverage: stdlib, numpy, sklearn, pytorch #5

Description

@noahgift

Problem

Many examples fail because Python modules aren't mapped to Rust equivalents. Currently depyler emits module.function() as-is, causing E0425 (undefined) errors.

Current Module Support (partial)

Python Rust Status
argparse clap ✅ Working
datetime chrono ⚠️ Partial
pathlib std::path ⚠️ Partial
json serde_json ⚠️ Partial
typing Native types ✅ Working

Modules Needed

Python Standard Library (Priority 1)

Module Rust Crate Examples Using
os std::fs, std::env 15+
sys std::env 10+
re regex 8+
collections std::collections 12+
itertools itertools 6+
functools Native closures 5+
io std::io 10+
subprocess std::process 4+
asyncio tokio 5+
threading std::thread 3+
zipfile zip 2+
csv csv 4+
hashlib sha2, md5 3+
base64 base64 2+
struct byteorder 2+
socket std::net 2+
logging log, tracing 5+
unittest (skip/stub) N/A
tempfile tempfile 3+
shutil fs_extra 3+
glob glob 2+
pickle serde + bincode 2+

NumPy (Priority 2)

Function Rust Equivalent Notes
np.array Vec or ndarray Type inference needed
np.zeros/ones vec![0.0; n] Shape handling
np.sum/mean/std Iterator methods Aggregate functions
np.dot Manual or nalgebra Matrix ops
np.sqrt/exp/log f64 methods Math functions
np.reshape Manual indexing Shape manipulation
np.concatenate Vec::extend Array joining
np.where Iterator::filter Conditional
Broadcasting Manual loops Complex

Scikit-learn (Priority 3)

Class Rust Equivalent Notes
train_test_split Manual split Simple
StandardScaler Manual normalize Stateful
LinearRegression linfa Full crate
KMeans linfa-clustering Full crate
metrics.* Manual impl accuracy, f1, etc
Pipeline Trait chain Design needed

PyTorch (Priority 4)

Class Rust Equivalent Notes
torch.Tensor tch::Tensor tch-rs bindings
nn.Module Trait Custom trait
nn.Linear tch layers Direct mapping
optim.Adam Manual or tch Optimizer
autograd tch autograd Complex

Proposed Implementation

Option A: Stub Generation

// Generated stub for unsupported module
mod zipfile {
    pub struct ZipFile;
    impl ZipFile {
        pub fn new(_: &str, _: &str) -> Self { todo!("zipfile not implemented") }
    }
}

Option B: Crate Mapping

# depyler.toml
[module_mappings]
"numpy" = { crate = "ndarray", features = ["blas"] }
"sklearn" = { crate = "linfa", features = ["all"] }
"torch" = { crate = "tch", features = [] }

Option C: Feature Flags

depyler transpile --features numpy,sklearn input.py

Acceptance Criteria

  1. Document all stdlib modules used in examples
  2. Implement top 10 stdlib modules
  3. Add numpy array basics (array, zeros, sum, mean)
  4. Add sklearn train_test_split
  5. Emit helpful error for unmapped modules

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions