Skip to main content

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

trye-0.1.3.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

trye-0.1.3-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file trye-0.1.3.tar.gz.

File metadata

  • Download URL: trye-0.1.3.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for trye-0.1.3.tar.gz
Algorithm Hash digest
SHA256 783c50d8cb60cded12450b685c2df6bdf4fb99301c4e8f1f55452f3ed7be9c92
MD5 f1b963fa800b8c2f6c722d5885062ff7
BLAKE2b-256 cbc39932e2f02a486964196513364bd9fcdc30c27240218a67e8990d14127234

See more details on using hashes here.

Provenance

The following attestation bundles were made for trye-0.1.3.tar.gz:

Publisher: release.yml on carderne/trye

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trye-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: trye-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for trye-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 71e3afc55042d8637c78d17f957979dc9034fbcdcbc9062848a9f8bc6e454269
MD5 ae31262d67d6e98654c29f89b8483d78
BLAKE2b-256 fe3f05ae2232fc7ddadd17cff2ff6f206532e7e7d6f5046cb7205d21564d66f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for trye-0.1.3-py3-none-any.whl:

Publisher: release.yml on carderne/trye

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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