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!
Metadata
Release files for gaffe 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| gaffe-0.3.0.tar.gz | 5.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| gaffe-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.9 kB
Release files / gaffe-0.3.0.tar.gz
| Download URL | gaffe-0.3.0.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1c09015fc8ff0343e8c94e37cfc4d24b92dd6c2e0e7cba87bea210eeab68ee3f
|
|
BLAKE2b-256 checksum How to use checksums |
df63f6903f5c1bd0472e1fa842ae89514cbd527cf8e11d8f60b5eff70c29637e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.9.17 Darwin/22.5.0
|
Release files / gaffe-0.3.0-py3-none-any.whl
| Download URL | gaffe-0.3.0-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fce5ad7cc5b2b6596775220db88ad54767cb32e5d589dfe1ca356ae7dc5c15e9
|
|
BLAKE2b-256 checksum How to use checksums |
642559fb4e2b53f5755d8123e703500e2705865adb6713694fefd6014d7becb2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.9.17 Darwin/22.5.0
|