Skip to main content

Resulting

Bring the rust Result and Option type into python.

pip install resulting

Result

from resulting import Result, Ok, Err

def get_user_id() -> Result[int, str]:
    return Ok(123)

match get_user():
    case Ok(user_id):
        print(f"User ID: {user_id}")
    case Err(msg):
        print(f"Failed to get user: {msg}")

Tracebacks

When unwrapping a Result or Option, a traceback will be inserted up to the place the `Err`` was created

This allows you to have the powerful debugging of Exceptions with guarentees of results

# example.py
from resulting import Err

def create_err() -> Err[str]:
    return Err("Throw Me!")

res = create_err()
res.unwrap()
Traceback (most recent call last):
  File "/example.py", line 7, in <module>
    res.unwrap()
  File "/example.py", line 6, in <module>
    res = create_err()
  File "/example.py", line 4, in create_err
    return Err("Throw Me!")
RuntimeError: Unwrapped Err(Throw Me!)

Option

from resulting import Option, Some, none

def get_user_id() -> Option[int]:
    return Some(123)

match get_user():
    case Some(user_id):
        print(f"User ID: {user_id}")
    case none():
        print(f"User does not exist")

Catch Errors

You can use the catch decorator to catch exceptions

from resulting import catch

@catch()
def get_user() -> str:
    raise KeyError("User Not Found")

res: Result[str, Exception] = get_user()

or catch specific exceptions using catch

from resulting import catch

@catch([KeyError, LookupError])
def get_user() -> str:
    raise KeyError("User Not Found")

res: Result[str, KeyError|LookupError] = get_user()

Optional

You can use the optional wrapper to wrap existing functions in an Option

from resulting import optional

@optional
def get_user() -> str|None:
    return "1"

res: Option[str] = get_user()

Metadata

Release files for resulting 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for resulting 1.3.0
File Size Uploaded
resulting-1.3.0.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for resulting 1.3.0
File Interpreter ABI Platform
resulting-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / resulting-1.3.0.tar.gz

Download URL resulting-1.3.0.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d19bd13234405316d410e1315e520df88f7469aa6dac784cab56718cf07037fa
BLAKE2b-256 checksum
How to use checksums
0893137db14b89fc4fae506276c313a1b2e45ce0509ee0b28e88fc9e4f764869
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Release files / resulting-1.3.0-py3-none-any.whl

Download URL resulting-1.3.0-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2cd67ab69b6cc5bdd3dc49e7a407928b74c197d0ebd09330cf1c5e68ccac81a9
BLAKE2b-256 checksum
How to use checksums
937e597697bc53c93c097dbe3249399f8e73c1ccc84c2a380a22f92f2b346e08
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page