Skip to main content

A Python module that brings ergonomic, Rust-inspired utilities like Result, and panics to Python

Project description

PyPI version info PyPI supported Python versions

Key Features

  • Result Type: Rust-style Result[T, E] for explicit error handling without exceptions
  • Ok/Err Variants: Type-safe success and error cases with pattern matching methods
  • Panic Mechanism: Rust-like panic() for unrecoverable errors with detailed stack traces
  • Decorator Utilities: Convert existing functions to return Results with @resultify and @resultify_catch_only
  • Chainable Operations: Functional methods like map, map_err, unwrap_or, and more
  • Fully Typed: Complete type hints for excellent IDE support and type checking

Installation

# using pip
pip install pa-py-rs

# using uv
uv add pa-py-rs

NOTE: Python 3.7 or higher is required

Examples

Result

Basic Result Usage

from pa_py_rs import Result, Ok, Err

def divide(a: float, b: float) -> Result[float, str]:
    if b == 0:
        return Err("Division by zero")
    return Ok(a / b)

result = divide(10, 2)
print(result)   # Ok(5.0)
if result.is_ok():
    print(f"Success: {result.unwrap()}")    # Success: 5.0

result = divide(10, 0)
print(result)   # Err(Division by zero)
if result.is_err():
    print(f"Error: {result.unwrap_err()}")  # Error: Division by zero

Using decorators

from pa_py_rs import resultify, resultify_catch_only

# Convert any function to return Result
# Automatically catch errors and wrap in Err(...)
@resultify
def parse_int(value: str) -> int:
    return int(value)

result = parse_int("123")   # Ok(123)
result = parse_int("abc")   # Err(...)

# For functions already returning Result
# Automatically catch errors and wrap in Err(...)
@resultify_catch_only
def safe_divide(a: float, b: float) -> Result[float, str]:
    return Ok(a / b)

result = safe_divide(10, 2)  # Ok(5.0)
result = safe_divide(10, 0)  # Err("cannot divide by zero")

Chaining Operations

from pa_py_rs import Ok, Err

result = Err("error").unwrap_or(0)
print(result)   # 0

result = (Ok(10)
    .map(lambda x: x * 2)
    .map(lambda x: x + 5)
    .unwrap())
print(result)   # 25

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

pa_py_rs-0.1.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

pa_py_rs-0.1.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pa_py_rs-0.1.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for pa_py_rs-0.1.1.tar.gz
Algorithm Hash digest
SHA256 902be81a6a8d4bdf202eb1e07ed49676f2479cee38d2b4e1d5248363297333a9
MD5 f30a49bbee901d575ef5e2257d5fe44c
BLAKE2b-256 664bbb6b648e1286222b6934e85d5464226f2e100ea1bcd9f4f492c93dab6e44

See more details on using hashes here.

File details

Details for the file pa_py_rs-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pa_py_rs-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for pa_py_rs-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2019ba51d8f85267aefb04b896b943114719df568999857a68afc15e0b1b81d2
MD5 6297808ab19ea7a4dfefda48d47e4e3a
BLAKE2b-256 4f280ec2a78300402764df54b98884bb9292e97d2c06954c8f1bb80a62da3941

See more details on using hashes here.

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