Skip to main content

A rust-like result type for Python

Project description

Build status PyPI Downloads Coverage

A simple Result type inspired by Rust.

The idea is that a Result value can be either Ok(value) or Err(error), with a way to differentiate between the two. It will change code like this:

def get_user_by_email(email):
    """
    Return the user instance or an error message.
    """
    if not user_exists(email):
        return None, 'User does not exist'
    if not user_active(email):
        return None, 'User is inactive'
    user = get_user(email)
    return user, None

user, reason = get_user_by_email('ueli@example.com')
if user is None:
    raise RuntimeError('Could not fetch user: %s' % reason)
else:
    do_something(user)

To something like this:

from result import Ok, Err

def get_user_by_email(email):
    """
    Return the user instance or an error message.
    """
    if not user_exists(email):
        return Err('User does not exist')
    if not user_active(email):
        return Err('User is inactive')
    user = get_user(email)
    return Ok(user)

user_result = get_user_by_email(email)
if user_result.is_ok():
    do_something(user_result.value)
else:
    raise RuntimeError('Could not fetch user: %s' user_result.value)

As this is Python and not Rust, you will lose some of the advantages that it brings, like elegant combinations with the match statement. On the other side, you don’t have to return semantically unclear tuples anymore.

Not all methods (https://doc.rust-lang.org/std/result/enum.Result.html) have been implemented, only the ones that make sense in the Python context. You still don’t get any type safety, but some easier handling of types that can be OK or not, without resorting to custom exceptions.

API

Creating an instance:

>>> from result import Ok, Err
>>> res1 = Ok('yay')
>>> res2 = Err('nay')

Or through the class methods:

>>> from result import Result
>>> res1 = Result.Ok('yay')
>>> res2 = Result.Err('nay')

Checking whether a result is ok or not:

>>> res = Ok('yay')
>>> res.is_ok()
True
>>> res.is_err()
False

Convert a Result to the value or None:

>>> res1 = Ok('yay')
>>> res2 = Err('nay')
>>> res1.ok()
'yay'
>>> res2.ok()
None

Convert a Result to the error or None:

>>> res1 = Ok('yay')
>>> res2 = Err('nay')
>>> res1.err()
None
>>> res2.err()
'nay'

Access the value directly, without any other checks (like unwrap() in Rust):

>>> res1 = Ok('yay')
>>> res2 = Err('nay')
>>> res1.value
'yay'
>>> res2.value
'nay'

Note that this is a property, you cannot assign to it. Results are immutable.

In case you’re missing methods like unwrap_or(default), these can be achieved by regular Python constructs:

>>> res1 = Ok('yay')
>>> res2 = Err('nay')
>>> res1.ok() or 'default'
'yay'
>>> res2.ok() or 'default'
'default'

License

MIT License

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

result-0.1.1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distributions

result-0.1.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

result-0.1.1-py2-none-any.whl (6.1 kB view details)

Uploaded Python 2

File details

Details for the file result-0.1.1.tar.gz.

File metadata

  • Download URL: result-0.1.1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for result-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2b104296a1b326167f262a33f177ddebae0f8c91d9d8ecea5e16dc115feb62ee
MD5 36dfdc828502cb385a0c6071f6c027e7
BLAKE2b-256 b523128679988640f51436a13043e9684adb0640bb9f9f591be19c07a248d97e

See more details on using hashes here.

File details

Details for the file result-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for result-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 65d8479929189be6533d6dfb4ac6c60220f3587dc13debde6a7678ce098b3565
MD5 3ab7cf09cc4449d1288f640628b0c03a
BLAKE2b-256 322cf82ab8d4163473d181885187d23b4e567cbb3c97fd9fa758f795abd80fea

See more details on using hashes here.

File details

Details for the file result-0.1.1-py2-none-any.whl.

File metadata

File hashes

Hashes for result-0.1.1-py2-none-any.whl
Algorithm Hash digest
SHA256 566aa192f96631f407cef595d942d512b4e19239f29a486620560735914c6b57
MD5 7d5a3e2009b420055b25ed9732130a1b
BLAKE2b-256 af4e6b7b1388cbd3dd93d2b7e79621753725ae30059595c9e0383ab971dde2f1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page