Skip to main content

Rusty results

Docs Run Python Tests codecov Downloads GitHub release Gitpod ready-to-code

Rusty results is a Python library for dealing with result and optional types inspired by rust standard library.

Pattern matching ready!

It exposes two main types with two constructors each one.

Result

Result[T, E] is the type used for returning and propagating errors. It is based in the variants, Ok(T), representing success and containing a value, and Err(E), representing error and containing an error value.

Option

Option[T] represents an optional value: every Option is either Some(T) that contains a value, or Empty() that does not. Option types have a number of uses:

  • Initial values
  • Return values for functions that are not defined over the entire input range (partial function)
  • Return value for otherwise reporting simple errors, where Empty is returned on error.
  • Optional struct fields
  • Optional function arguments

Installation

Use the package manager pip to install rusty results.

pip install rusty_results

Usage

from rusty_results import Option, Some, Empty, Result, Ok, Err, UnwrapException

Examples

"""
Example on pattern matching handling of Result
"""

from rusty_results import Result, Ok, Err


def divide(a: int, b: int) -> Result[int, str]:
    if b == 0:
        return Err("Cannot divide by zero")
    return Ok(a // b)


if __name__ == "__main__":
    values = [(10, 0), (10, 5)]
    for a, b in values:
        divide_result = divide(a, b)
        match divide_result:
            case Ok(value):
                print(f"{a} // {b} == {value}")
            case Err(e):
                print(e)

You can find more examples in the /examples folder.

Pydantic

Option and Result are fully compatible with pydantic models

import pydantic
from rusty_results import Option, Some, Empty


class MyData(pydantic.BaseModel):
    name: Option[str]
    phone: Option[int]

    
if __name__ == "__main__":
    import json
    # serialize to json
    json_data = MyData(name=Some("Link"), phone=Empty()).json()
    print(json_data)
    # deserialize json data
    data = MyData(**json.loads(json_data))
    print(data)

prints out:

{"name": {"Some": "Link"}, "phone": {}}
name=Some(Some='Link') phone=Empty()

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Metadata

Release files for rusty-results 1.1.1

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

Source distribution (sdist)

Source distribution for rusty-results 1.1.1
File Size Uploaded
rusty-results-1.1.1.tar.gz 10.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rusty-results 1.1.1
File Interpreter ABI Platform
rusty_results-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.8 kB

Release files / rusty-results-1.1.1.tar.gz

Download URL rusty-results-1.1.1.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d2ca6da1db15dc1ea0aa97fb35254b125ef6c872b6228d99a42f0d9fff55d40f
BLAKE2b-256 checksum
How to use checksums
3a22ac95c4aa79f917182aaa11fbf0c0b95cc3a48854bb2ce81333880b5d007d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.2

Release files / rusty_results-1.1.1-py3-none-any.whl

Download URL rusty_results-1.1.1-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f685877d2797a90c7ee0c9cb053dd050b237a6ee7ff6b417fcc1484ea28e10ce
BLAKE2b-256 checksum
How to use checksums
6619e2fa391bfac3a34e08f1a8c9151a7d97c595265a22d036121eec6df61629
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.2

Release history Release notifications | RSS feed

This release

1.1.1 This release

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