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.0.tar.gz (18.5 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.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for neverraise-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2ca80fd4ec3751bb4504bc6a6da9cdd7a18d1c07f6dc7e55f7c57d96d5f3e862
MD5 7ff0d4b0eb9d07ba167585cb56b86000
BLAKE2b-256 20b8e1479cc355bc71a0039cf8ae4477eaf6e8ff297fffd46f02b0dff30f71ea

See more details on using hashes here.

File details

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

File metadata

  • Download URL: neverraise-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 19b4dd5a9f99bcf908d55b0a2647d011b51e4cdd349b403650b9110ad5dfaf23
MD5 14a902e4239908bbdb4ce6c468d4f427
BLAKE2b-256 59c2edba7b05e5268a331e14fc1b3d0ffb329b9931ff35df086673a553f3d69d

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