Skip to main content

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

Project description

simple-result

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!"

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.1.0.tar.gz (5.0 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.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: simple_result-0.1.0.tar.gz
  • Upload date:
  • Size: 5.0 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.1.0.tar.gz
Algorithm Hash digest
SHA256 a01a057c34c6dabd57a9ff3779fc97b04a561f2658508e8f73fd30fa2cf41f1c
MD5 0c8cbcd1be16794dc95388b10815371a
BLAKE2b-256 6b032051cf483b196977f51ad459fd703ae990028b721f86502306f39366649e

See more details on using hashes here.

Provenance

The following attestation bundles were made for simple_result-0.1.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: simple_result-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e85bfacd4b44a997cbfffcaf49a7f826476e2a5ad00faad46c02d00a05d2ed8a
MD5 1fdcadbd0ea7962f0650ba5f21f3ef84
BLAKE2b-256 f6a4a3646e389bb8c9ff528c111653907fa8cf46bc2162bca89a909188edb363

See more details on using hashes here.

Provenance

The following attestation bundles were made for simple_result-0.1.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