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.Resultorneverraise.ResultAsyncdo not throw exceptions, apart from
UnwrapErrorcaused by callingmapwhen the mapping function throws (use.try_catchfor map operations that can fail)unwrap,expect,unwrap_err, orexpect_errwhen called on a non-matching variant ofResult(e.g.Err("oh no").unwrap())
- Any subclass of
BaseExceptionsuch asasyncio.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61f2ba66d7e49fafacfdf3ede6153045750b80e7cc9af40d07eb9b32fc47ff1c
|
|
| MD5 |
533db08cdbba4cf39152e5848e724e79
|
|
| BLAKE2b-256 |
75e0c2a08c1be40364bd2d17dd1d911d716464bbf96d0ae2a615c74ded1f0a98
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c001c4ae54c0854001ab603e7fe091bdbe4cfd46390de7ee8aaf9ad7770a1592
|
|
| MD5 |
c61a154f5ec91435699acc69f01121f4
|
|
| BLAKE2b-256 |
6568b67e87051c4eac679b70fce11764334350385e3cb5d05de42875b4500588
|