Skip to main content

No project description provided

Project description

pypi precommit mypy

result.py

A Python implementation inspired by Rust's Result<T, E>, representing operations that can either succeed (Ok) or fail (Err).

Installation

$ pip install pytresult

Key Features

  • 100% Typed: Works flawlessly with mypy — zero type errors, guaranteed.

  • Well Tested: Solid 100% test coverage with pytest so you can trust it.

  • No Dependencies: Just install and go — nothing else required.

  • Rust-Style: Clean, explicit error handling inspired by Rust’s Result.

Motivation

Using Result helps avoid excessive exception handling and promotes more explicit and robust error handling.

Usage Example

from result import Result, Ok, Err

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

result = divide(10, 2)

if result.is_ok():
    print("Success:", result.unwrap())
else:
    print("Error:", result)  # or result.unwrap_or(0)

Patterns

if let Ok(...) { }

  • Rust

    fn divide(a: i32, b: i32) -> Result<f32, String> {
        if b == 0 {
            Err("division by zero".to_string())
        } else {
            Ok(a as f32 / b as f32)
        }
    }
    
    let result = divide(10, 2);
    
    if let Ok(value) = result {
        println!("Success: {}", value);
    }
    
  • Python

    from result import Result, Ok, Err
    
    def divide(a: int, b: int) -> Result[int, str]:
        if b == 0:
            return Err("division by zero")
        return Ok(a // b)
    
    result = divide(10, 2)
    
    if r := result.ok():
        assert r is not None
        assert r == 5
    

if let Err(...) { }

  • Rust

    fn divide(a: i32, b: i32) -> Result<f32, String> {
        if b == 0 {
            Err("division by zero".to_string())
        } else {
            Ok(a as f32 / b as f32)
        }
    }
    
    let result = divide(10, 0);
    
    if let Err(error) = result {
        println!("Error: {}", error);
    }
    
  • Python

    from result import Result, Ok, Err
    
    def divide(a: int, b: int) -> Result[int, str]:
        if b == 0:
            return Err("division by zero")
        return Ok(a // b)
    
    result = divide(10, 0)
    
    if e := result.err():
        assert e is not None
        assert e == "division by zero"
    

Testing

Tests are written using pytest.

To run the tests:

poetry run pytest

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

pytresult-1.0.2.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

pytresult-1.0.2-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file pytresult-1.0.2.tar.gz.

File metadata

  • Download URL: pytresult-1.0.2.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/6.11.0-1018-azure

File hashes

Hashes for pytresult-1.0.2.tar.gz
Algorithm Hash digest
SHA256 ca864926f705d0ca310f63ae3b05abe86ad8b88eb21494372e6edffb78f1ff00
MD5 9d2af008c80443f24b1ce802282c470c
BLAKE2b-256 6df7a284a810d070e2437a1e96d5db4adef06555152536e5e4f0b5e00004362b

See more details on using hashes here.

File details

Details for the file pytresult-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pytresult-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/6.11.0-1018-azure

File hashes

Hashes for pytresult-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 43a5ed2ad38664c4cd1891748f1d27aeacde2cde5e7bf470aaeb36f36084919c
MD5 f1e3bd53d07cf9571fe860f901c74054
BLAKE2b-256 d1d3acca70faaa3507ec04da20359f82cf277cf7ebf44cbede876fa9b76d6950

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