Skip to main content

Yet another implementation of Rust's Result type: minimal, dependency-free and fully-typed.

Project description

better-result-py

Inspired by better-result

Yet another implementation of Rust's Result type: minimal, dependency-free, and fully-typed. Wraps synchronous and asynchronous functions so exceptions become values rather than control flow.

Installation

# with uv
uv add better-result-py
# with pip
pip install better-result-py

Usage

Synchronous

Wrap any callable with Result. If the function raises, ok is Unset and err holds the exception. If it succeeds, ok holds the value and err is None.

from better_result import Result

def divide(a: int, b: int) -> float:
    return a / b

result = Result(divide, 10, 2)
result.is_ok()   # True
result.ok        # 5.0
result.err       # None

result = Result(divide, 10, 0)
result.is_ok()   # False
result.err       # ZeroDivisionError(...)

Asynchronous

AsyncResult works the same way for async functions.

import asyncio
from better_result import AsyncResult

async def fetch_data(url: str) -> str:
    ...  # may raise

result = await AsyncResult(fetch_data, "https://example.com")

if result.is_ok():
    print(result.ok)
else:
    print(result.err)

Extracting values

unwrap()

Returns the value or raises the original exception (or UnsetError if the result is unset).

value = Result(divide, 10, 2).unwrap()   # 5.0
Result(divide, 10, 0).unwrap()           # raises ZeroDivisionError

unwrap_or(default)

Returns the value or falls back to a default on error.

value = Result(divide, 10, 0).unwrap_or(0.0)  # 0.0

expect(message)

Like unwrap(), but always raises ExpectError with a custom message (preserving the original exception as __cause__).

result = Result(divide, 10, 0)
result.expect("division must succeed")  # raises ExpectError("division must succeed")

API reference

Method Description
is_ok() -> bool True if no error and value is set
is_err() -> bool True if an error occurred or value is unset
unwrap() -> T Return value or raise
unwrap_or(default: T) -> T Return value or default
expect(message: str) -> T Return value or raise ExpectError

Requirements

Python 3.10 or higher. No runtime dependencies.

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

better_result_py-1.0.0.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

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

better_result_py-1.0.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file better_result_py-1.0.0.tar.gz.

File metadata

  • Download URL: better_result_py-1.0.0.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for better_result_py-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6a9cce21cf7062f4e7a0deba09cbeb89b8ef2fc35f7eb38628f6bad6119bf4e3
MD5 b5a294fa7e244635be9668a5be065064
BLAKE2b-256 6691979e2cdb49b63824ad848e90023d0bc45c00ee6e2c3d493d7607ccddeaa9

See more details on using hashes here.

File details

Details for the file better_result_py-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: better_result_py-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for better_result_py-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 82634fdce8943dc0357210f5272dcf6a0b714792ffa996dc576973cafb7c661a
MD5 6cfe80f832f40612b7f39fead4b4b2e8
BLAKE2b-256 724509acca08f8c46a36e345f8649baae909932e6f908357c17ba40ddbfaecd6

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