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):
    Ok(res): print(f"Got {res}")
    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.1.tar.gz (18.6 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.1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for neverraise-0.1.1.tar.gz
Algorithm Hash digest
SHA256 61f2ba66d7e49fafacfdf3ede6153045750b80e7cc9af40d07eb9b32fc47ff1c
MD5 533db08cdbba4cf39152e5848e724e79
BLAKE2b-256 75e0c2a08c1be40364bd2d17dd1d911d716464bbf96d0ae2a615c74ded1f0a98

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for neverraise-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c001c4ae54c0854001ab603e7fe091bdbe4cfd46390de7ee8aaf9ad7770a1592
MD5 c61a154f5ec91435699acc69f01121f4
BLAKE2b-256 6568b67e87051c4eac679b70fce11764334350385e3cb5d05de42875b4500588

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