Skip to main content

Result types for python

Project description

neverraise

A mix of Rust's Result type and the neverthrow typescript library.

from neverraise import Result, Ok, Err


def divide(a: int, b: int) -> Result[float, ZeroDivisionError]:
    if b == 0:
        return Err(ZeroDivisionError())
    return Ok(a / b)

match divide(1, 0):
    case Ok(res): print(f"Got {res}")
    case Err(): print("Couldn't divide")
def get_todos() -> ResultAsync[list[Todo], HTTPError | JsonParseError | DecodeError]:
    client = httpx.AsyncClient()
    return (
        ResultAsync.from_coro(
            client.get("https://jsonplaceholder.typicode.com/todos/"),
            lambda e: HTTPError(e),
        )
        .try_catch(lambda response: response.json(), lambda e: JsonParseError(e))
        .try_catch(
            lambda json: msgspec.convert(json, type=list[Todo]),
            lambda e: DecodeError(e),
        )
    )

[!IMPORTANT] Functions that return neverraise.Result or neverraise.ResultAsync do not throw exceptions, apart from

  • UnwrapError caused by calling
    • map when the mapping function throws (use .try_catch for map operations that can fail)
    • unwrap, expect, unwrap_err, or expect_err when called on a non-matching variant of Result (e.g. Err("oh no").unwrap())
  • Any subclass of BaseException such as asyncio.CancelledError.

Do not catch UnwrapError, as they are not expected to be raised. If one is raised, it means that the code is incorrect. It should be treated as a failed assertion.

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

neverraise-0.1.2.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

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

neverraise-0.1.2-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file neverraise-0.1.2.tar.gz.

File metadata

  • Download URL: neverraise-0.1.2.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.11

File hashes

Hashes for neverraise-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1096d0c54d77add26c0e289ffe70b97cfe42d513e4c98f117f2f241ccf77fac3
MD5 a5c674cee779cfedd4f0808eae08da39
BLAKE2b-256 859812e38ea95ab62e4c223fbd693ef18f9a09a72d977b15605a14af91f9f285

See more details on using hashes here.

File details

Details for the file neverraise-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: neverraise-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.11

File hashes

Hashes for neverraise-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 95150a64ae87404863ade4071232e104f076a60510588af31425a29d6034772f
MD5 95988a0f88bf007f7d514f7371d92eba
BLAKE2b-256 7132f2ad54b58b2cb6494493d6ecb3c4b02c8825f24362aae55d72d1ad9a9281

See more details on using hashes here.

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