Skip to main content

Simple structured exceptions for python.

Project description

Introducing Gaffe: Streamlined Exception Handling for Python

Are you tired of managing messy, unstructured exceptions in your Python projects? Gaffe is here to save the day! This elegant library offers a metaclass-based approach for highly extensible and easy-to-integrate custom exceptions, leading to better error handling and improved code readability.

🔥 Key Features

  • 🎯 Simple, concise syntax for defining custom errors with optional subtypes
  • 🧩 Clean integration through metaclass-based approach
  • 🌳 Supports inheritance and composition of custom errors
  • 🏗️ Automatic generation of error classes with custom attributes
  • 🧮 Easy error comparison with the eq method, supporting both class and instance comparisons
  • 🕵️‍♂️ raises decorator to inspect and validate exceptions raised by functions or methods
  • 🚀 Quick Installation

For pip enthusiasts:

pip install gaffe

For poetry aficionados:

poetry add gaffe

💡 Getting Started

To employ Gaffe's custom error system, import the Error class and create custom errors by inheriting from it:

from gaffe import Error

class NotFoundError(Exception):
    ...

class MyError(Error):
    not_found: NotFoundError
    invalid_input: ...
    authentication_error = "authentication_error"

With this example, you'll get three custom errors under the MyError class, ready to be used just like any other Python exceptions.

🎩 Raises Decorator

Harness the power of the raises decorator to define and validate the types of exceptions a function or method can raise:

from gaffe import raises

@raises(TypeError, ValueError)
def my_function(x: int, y: int) -> float:
    if x <= 0 or y <= 0:
        raise ValueError("x and y must be positive")
    return x / y

The raises decorator ensures that my_function can only raise TypeError and ValueError. If it tries to raise an unlisted exception, an AssertionError will be raised with a suitable error message.

🤖 Mypy Integration

To keep mypy happy, use the gaffe.mypy:plugin in your config file, and ensure that error properties are annotated with Exception type instead of ...

[tool.mypy]
plugins = "gaffe.mypy:plugin"

Ready to revolutionize your Python exception handling? Get started with Gaffe today and check out the test scenarios for more examples!

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

gaffe-0.3.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

gaffe-0.3.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file gaffe-0.3.0.tar.gz.

File metadata

  • Download URL: gaffe-0.3.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.9.17 Darwin/22.5.0

File hashes

Hashes for gaffe-0.3.0.tar.gz
Algorithm Hash digest
SHA256 1c09015fc8ff0343e8c94e37cfc4d24b92dd6c2e0e7cba87bea210eeab68ee3f
MD5 db951de26a38638c802ef975104f9467
BLAKE2b-256 df63f6903f5c1bd0472e1fa842ae89514cbd527cf8e11d8f60b5eff70c29637e

See more details on using hashes here.

File details

Details for the file gaffe-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: gaffe-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.9.17 Darwin/22.5.0

File hashes

Hashes for gaffe-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fce5ad7cc5b2b6596775220db88ad54767cb32e5d589dfe1ca356ae7dc5c15e9
MD5 0f4bf7ea401f5ea9a43eda8b502ce53b
BLAKE2b-256 642559fb4e2b53f5755d8123e703500e2705865adb6713694fefd6014d7becb2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page