Skip to main content

sain — Write safe Python code like Rust

a dependency-free which implements Rust's core crates, purely, in Python. Offering core Rust items such as Vec<T>, Result<T, E>, Option<T> and more. See the built-in types section below.

Install

Python 3.10 or higher is required.

Using uv:

uv pip install sain

Using pip:

pip install sain

Overview

sain is built completely on-top of Python's stdlib, inspired by The Rust Standard Library, designed to bring modern, safe, and ergonomic data structures and utilities to Python.

The goal is to provide developers with high-level abstractions and low-level control similar to what Rust offers, making it easier to write robust, and maintainable code.

Example

In this example we are writing a simple app that takes advantage of Rust's types which're implemented in Python.

This example provides a simple library that contains a Vec of books. Here, we take advantage of the Vec type and its idiomatic methods.

from __future__ import annotations
from sain import Result, Ok, Err  # used for safe error handling.
from sain import Option  # used for absense of a value, similar to `T | None`.
from sain import Vec  # A replacement for `list` type.

from dataclasses import dataclass, field

@dataclass
class Book:
    name: str
    author: str
    tags: set[str]
    pages = field(default_factory=set[str])

@dataclass
class Library:
    # just like any other mutable sequences.
    # it needs a default factory.
    books = field(default_factory=Vec[Book])

    # add a books to this library.
    def add(self, *books: Book):
        self.books.extend(books)

    # finds the first book that contains a specific tag.
    def find(self, pattern: str) -> Option[Book]:
        return self.books.iter().find(lambda book: pattern in book.tags)

    # finds the first book that contains a specific tag,
    # mapping Option[Book] to Result[Book, str] where `str` is the context
    # of the error.
    def find_or(self, pattern: str) -> Result[Book, str]:
        return self.find(pattern).ok_or(f"book with pattern {pattern} not found.")

    # We simply filter books that matches `author` and collect
    # them into a list[Book].
    def books_for(self, author: str):
        return self.books.iter().filter(lambda book: book.author == author).collect()


lib = Library()
lib.add(
    Book("Twilight", "Stephenie Meyer", {"Vampire", "Romance"}),
    Book("Silo", "Hugh Howey", {"Dystopian", "Sci-Fi"}),
    Book("The Eternaut", "Héctor Germán", {"Invasion", "Mystery"}),
)

# find the first vampire book then maps the book to its pages.
# maps Option[Book] -> Option[set[str]]
for page in lib.find("Vampire").map(lambda book: book.pages).unwrap():
    print(page)  # {page1, page2, ...}

match lib.find_or("Sci-Fi"):
    case Ok(book):
        print(book)  # Book("Silo", ...)
    case Err(err):
        print(err)  # book with pattern Sci-Fi not found.

# show Hugh's books.
print(lib.books_for("Hugh Howey"))  # [Book("Silo", ...)]

How to use this library

  • Option, Result, Iterator, */collections, Default - The are core routines which are used almost everywhere within the library. You can easily opt these in your projects.
  • sync, convert, error, macros, cfg - These are also core impls, but not widely used as the ones above.
  • maybe_uninit, futures, boxed - You will probably see those used once or less anywhere, they're designed for lower-level, general use.

built-in types

name in Rust name in Python note restrictions
Option<T>, Some(T), None Option[T], Some(T), Some(None) Some(None) has the same layout as None in Rust
Result<T, E>, Ok(T), Err(E) Result[T, E], Ok(T), Err(E) Basically a better and more verbose try/except
Vec<T> Vec[T] Same layout as list[T]
HashMap<K, V> HashMap[K, V] Same layout as dict[K, V]
bytes::Bytes Bytes
bytes::BytesMut BytesMut
LazyLock<T> Lazy[T]
OnceLock<T> Once[T]
Box<T> Box[T] this isn't a heap box
MaybeUninit<T> MaybeUninit[T] they serve the same purpose, but slightly different
impl Default Default[T]
&dyn Error Error
impl Iterator<T> Iterator[T]
Iter<'a, T> Iter[T] collections called by .iter() are built from this type
iter::once::<T>() iter.once[T]
iter::empty::<T>() iter.empty[T]
iter::repeat::<T>() iter.repeat[T]
cfg!() cfg() runtime cfg, not all predictions are supported
#[cfg_attr] @cfg_attr() runtime cfg, not all predictions are supported
#[doc] @doc() the docs get generated at runtime
todo!() todo()
#[deprecated] @deprecated() will get removed when it get stabilized in warnings in Python 3.13
unimplemented!() @unimplemented()

Contributing

See CONTRIBUTING.md.

Remaining work

This is still early days for sain, it is no where near as stable as Python's stdlib.

This project mainly started as a fun / learning experience but turned into something more inspiring.

The release cycles were breaking due to poor decision making at first, but it should be stable enough now.

Release files for sain 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sain 1.4.0
File Size Uploaded
sain-1.4.0.tar.gz 924.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sain 1.4.0
File Interpreter ABI Platform
sain-1.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.9 MB

Release files / sain-1.4.0.tar.gz

Download URL sain-1.4.0.tar.gz
Size 924.6 kB
Tags Source
SHA-256 checksum
How to use checksums
cfc3afd3f4857028cbec890a09f098207d16713f9b72090c895589fe5c7e8727
BLAKE2b-256 checksum
How to use checksums
14d43c77e84343c7d78a3694ffcf3565b122e9977a14a984e93d6384fa3cb494
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.8

Release files / sain-1.4.0-py3-none-any.whl

Download URL sain-1.4.0-py3-none-any.whl
Size 1.0 MB
Tags Python 3
SHA-256 checksum
How to use checksums
6ba4494b0be9e82f41e90f1537cb56c966777db6f4d47e835e258e407cc2ded7
BLAKE2b-256 checksum
How to use checksums
fd2462c0baa1827102c964b0366c6b63b9221745a2766dbfeea0c7f6fcc6ba45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.8

Release history Release notifications | RSS feed

This release

1.4.0 This release

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.0.6

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page