Skip to main content

A very simple, fully typed Rust-like Result type for Python 3.

Project description

simple-result

Supported Python versions Package version Publish License

A very simple, fully typed Rust-like Result type for Python 3.

If you need a Result type only for checking if an operation succeeded or failed, and don't need to perform special actions like chaining operations, mapping, etc., then this library is for you.

If you are looking for a more feature-rich Result type, check out rustedpy/result.

Installation

# Using pip
pip install simple-result

# Using Poetry
poetry add simple-result

# Using uv
uv add simple-result

Usage

import random

from simple_result import Err, Ok, Result

def fetch_data() -> Result[str, ConnectionError]:
    fetched = random.choice([True, False])
    if fetched:
        return Ok('Data fetched!')
    return Err(ConnectionError('Error fetching data!'))

Check if the result is Ok or Err using type narrowing:

if res := fetch_data():
    print(res.value) # "Data fetched!"
    print(res.error) # None
else:
    print(res.error) # "Error fetching data!"
    print(res.value) # None

Or using match:

match fetch_data():
    case Ok(data):
        print(data) # "Data fetched!"
    case Err(error):
        print(error) # "Error fetching data!"

match fetch_data():
    case Ok(data):
        print(data) # "Data fetched!"
    case Err(error, code):
        print(error, code) # "Error fetching data! 1"

Call .unwrap_value() to get the value or raise an UnwrapError if the result is Err:

from simple_result import UnwrapError

try:
    res = Err(ConnectionError('Error fetching data!'))
    print(res.unwrap_value())
except UnwrapError as exc:
    print(str(exc)) # called `Result.unwrap_value()` on an `Err` value
    print(exc.result.error) # "Error fetching data!"

Call .unwrap_error() to get the error or raise an UnwrapError if the result is Ok:

try:
    res = Ok('Data fetched!')
    print(res.unwrap_error())
except UnwrapError as exc:
    print(str(exc)) # called `Result.unwrap_error()` on an `Ok` value
    print(exc.result.value) # "Data fetched!"

Compare results:

assert Ok(1) == Ok(1)
assert Ok(1) != Ok(2)

exc = ValueError('error')
assert Err(exc) == Err(exc)

other_exc = ValueError('other error')
assert Err(exc) != Err(other_exc)

Check if the result is Ok or Err using isinstance:

from simple_result import ResultOption

res = fetch_data()
assert isinstance(res, ResultOption)
assert isinstance(res, (Ok, Err))

Contributing

See the contribution guidelines.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Support

If you find this project useful, give it a ⭐ on GitHub!

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

simple_result-0.2.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

simple_result-0.2.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file simple_result-0.2.0.tar.gz.

File metadata

  • Download URL: simple_result-0.2.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for simple_result-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3468547e36f8079c3b0e5325b3b2018e3c32fd61de9d0f2cd8d36f852d761d71
MD5 cfb459a3083db4c4d1237b14d3b93cd5
BLAKE2b-256 b3f7e1ea503bfd0b456f2c1df5369d4070700e9e2ca1451f6ccd2998f4d6b70c

See more details on using hashes here.

Provenance

The following attestation bundles were made for simple_result-0.2.0.tar.gz:

Publisher: publish.yml on daireto/simple-result

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file simple_result-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: simple_result-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for simple_result-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d15e7a64f0c6bcb4ea83f6cd055cbfb29786649994428c7c64c4e0ddc96d28ea
MD5 b3b86edf7e61501d89c4050810b5d8cf
BLAKE2b-256 1bffff55a6e2ad190558fe25c933103c565914eb780f767a315613844a16561b

See more details on using hashes here.

Provenance

The following attestation bundles were made for simple_result-0.2.0-py3-none-any.whl:

Publisher: publish.yml on daireto/simple-result

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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