Skip to main content

exceptly

PyPI Python Tests License

Tiny utilities for raising exceptions on a condition and rendering them as readable strings.

Installation

pip install exceptly

Requires Python 3.11+.

Why exceptly?

Two small annoyances show up in every Python codebase:

  • Guard clauses take three lines. if not value: raise ValueError("missing") is four tokens of logic wrapped in ceremony. raise_if_not(value, ValueError, "missing") is the same thing on one line, with the exception type spelled out up front where the reader can see it.
  • repr(e) and str(e) are both wrong for logs. repr(e) gives you ValueError('missing') (quoted, parenthesised, noisy). str(e) gives you 'missing' — no type at all. repr_exception(e) gives you ValueError: missing — the form humans actually want in log lines and assertion messages.

No dependencies, no configuration, no magic — three functions that do one thing each.

Quick Start

from exceptly import raise_if, raise_if_not, repr_exception


def set_positive(value: int) -> None:
    raise_if_not(isinstance(value, int), TypeError, f"expected int, got {type(value).__qualname__}")
    raise_if(value <= 0, ValueError, "expected a positive value, got", value)
    # ... actual work


try:
    set_positive(None)
except TypeError as e:
    assert repr_exception(e) == "TypeError: expected int, got NoneType"

try:
    set_positive(0)
except ValueError as e:
    assert repr_exception(e) == "ValueError: ('expected a positive value, got', 0)"

Overview

raise_if | raise_if_not | repr_exception


raise_if

Raises the given exception when condition is truthy; does nothing otherwise.

Use when: you want an inline guard clause without the if ...: raise ... boilerplate, and you want the exception type to be visible at the start of the call rather than buried inside the if body.

from exceptly import raise_if

# Pass a pre-built exception instance:
raise_if(x < 0, ValueError("negative value"))

# Or pass the class and its constructor args — the instance is only built if the condition fires:
raise_if(x < 0, ValueError, "negative value")
raise_if(x < 0, ValueError, "negative value", x)  # ValueError('negative value', -1)

The two forms are equivalent, but the class-plus-args form avoids constructing the exception when the guard does not trip — useful when the message is expensive to format (e.g. repr of a large object).

raise_if_not

Inverse of raise_if: raises when condition is falsy.

Use when: the natural phrasing of the guard is positive ("must be non-empty", "must be an int") rather than negative. Also a drop-in replacement for assert in code paths where assert is unreliable (stripped by python -O, silent in production configs).

from exceptly import raise_if_not

raise_if_not(isinstance(value, int), TypeError, f"expected int, got {type(value).__qualname__}")
raise_if_not(items, ValueError("items must not be empty"))

repr_exception

Renders an exception as "ClassName: message", or "ClassName" when the exception carries no message.

Use when: writing log lines, assertion messages, or error responses. repr(e) is cluttered (ValueError('missing')); str(e) loses the type ('missing'); f"{type(e).__name__}: {e}" is the hand-rolled version you end up writing anyway.

from exceptly import repr_exception

repr_exception(ValueError("not found"))     # "ValueError: not found"
repr_exception(RuntimeError())              # "RuntimeError"
repr_exception(ValueError(1, 2, 3))         # "ValueError: (1, 2, 3)"


class DomainError(Exception):
    pass


repr_exception(DomainError("boom"))         # "DomainError: boom"

The output is plain text, safe for logs and assertion messages, and never raises.

License

MIT

Metadata

Release files for exceptly 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for exceptly 1.0.0
File Size Uploaded Provenance Actions
exceptly-1.0.0.tar.gz 5.6 kB With provenance Details for exceptly-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for exceptly 1.0.0
File Interpreter ABI Platform Actions
exceptly-1.0.0-py3-none-any.whl Python 3 none any Details for exceptly-1.0.0-py3-none-any.whl

Total release size: 10.3 kB

Release files / exceptly-1.0.0.tar.gz

Download URL exceptly-1.0.0.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
33af87b0b0d99db28066fc5ac0cf7405980492ee1792e46c1e9fb4d5ab6bde83
BLAKE2b-256 checksum
How to use checksums
99561057876729b66f14dff39a7d654434686491685e2b0b9730b9876564c823
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 22, 2026.

Transparency log

Release files / exceptly-1.0.0-py3-none-any.whl

Download URL exceptly-1.0.0-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
372e3e9bddd4b83336e9a51009b00f576c18736663211baf381c08a3c052b52d
BLAKE2b-256 checksum
How to use checksums
87ffec02b539144fcf042226d6f8f7ebf233259565a9cd94e86fb16e0cba4b85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 22, 2026.

Transparency log

Security for exceptly 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for exceptly-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page