Ergonomic exception handling for Python.
Project description
trye
Ergonomic exception handling for Python.
Inspired by the ECMAScript Try Operator proposal. Instead of try/except blocks, wrap any callable in trye() and get back a typed Result — either Ok(val) or Err(err).
Requires Python 3.14+ (uses generic syntax and type aliases).
Install
uv add trye
Usage
from trye import trye, Ok, Err
isinstance narrowing
result = trye(json.loads, '{"foo": "bar"}')
if isinstance(result, Ok):
print(result.val) # dict
else:
print(result.err) # Exception
isinstance with else
result = trye(some_function, arg1, arg2)
if isinstance(result, Err):
log_error(result.err)
return
# result is narrowed to Ok here
use_value(result.val)
match/case
result = trye(int, user_input)
match result:
case Ok(val=v):
print(f"parsed: {v}")
case Err(err=e):
print(f"failed: {e}")
Sentinel fields
Both Ok and Err have sentinel fields so you can always safely check either side:
result = trye(some_function, arg)
if result.err is not None:
handle_error(result.err)
if result.val is not None:
use_value(result.val)
API
| Name | Description |
|---|---|
trye(f, *args, **kwargs) |
Call f with args, return Ok(result) or Err(exception) |
Ok[T] |
Success wrapper. .val: T, .err: None |
Err |
Error wrapper. .err: Exception, .val: None |
Result[T] |
Type alias for Ok[T] | Err |
Arguments are fully typed via ParamSpec, so type checkers will catch incorrect arguments to the wrapped function.
Development
Install uv, then:
uv sync
This project uses poethepoet for tasks:
uv run poe fmt # format
uv run poe lint # lint
uv run poe check # type check (basedpyright + ty)
uv run poe test # test
uv run poe all # all of the above
License
MIT
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
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 trye-0.1.1.tar.gz.
File metadata
- Download URL: trye-0.1.1.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b0145989ece879bbcb736b8ba2ee75f219b8100b108d1a92493d7360a5862b0e
|
|
| MD5 |
b6e95228e31e0b94690b176f4a5bd8d8
|
|
| BLAKE2b-256 |
387a64a215ee95ca5b4ec7a9d76d10eac5fa1ecbe259c0db17fad30db0e268ca
|
File details
Details for the file trye-0.1.1-py3-none-any.whl.
File metadata
- Download URL: trye-0.1.1-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c4d06a7a68c97bc0e80edc7ac75dd4c6603ee23795dfaadda40f487d718eda9
|
|
| MD5 |
135581a8f4f3151e52549bc1395a19e1
|
|
| BLAKE2b-256 |
6b1a6d2715fda4bde065343f530ead4521b26e8ee4cae9dd8fc3e16860d335df
|