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.2.tar.gz (4.3 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.2-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pa_py_rs-0.1.2.tar.gz
Algorithm Hash digest
SHA256 67bff2b72c306d7ebee17b027fe245b2cc838bc935baf4d09a36bcd378cf2a26
MD5 0825546f47fa922ceeb51af1f72ed8ba
BLAKE2b-256 e57d10f9942fe303fb1267761efa8348e0a94ab81b13f9575efbbfb0d98b1fee

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pa_py_rs-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d17eea9f551a2d3e4a1bc959856ef9ae3338565fc65367cb5fbc0ae99cfccbc3
MD5 99f6bbbdc3a37211607130af1455c294
BLAKE2b-256 2a85378efd0d9185176e6027a51ddafbe6fc91e16b13ef82946b6923554c2365

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