Rust-like error handling in Python, with type-safety in mind.
Project description
poltergeist
Rust-like error handling in Python, with type-safety in mind.
Installation
pip install poltergeist
Examples
Use the @catch
decorator on any function:
from poltergeist import catch
# Handle an exception type potentially raised within the function
@catch(OSError)
def read_text(path: str) -> str:
with open(path) as f:
return f.read()
# Returns an object of type Result[str, OSError]
result = read_text("my_file.txt")
Or wrap errors manually:
from poltergeist import Result, Ok, Err
# Equivalent to the decorated function above
def read_text(path: str) -> Result[str, OSError]:
try:
with open(path) as f:
return Ok(f.read())
except OSError as e:
return Err(e)
Then handle the result in a type-safe way:
# Get the Ok value or re-raise the Err exception
content: str = result.unwrap()
# Get the Ok value or a default if it's an Err
content: str = result.unwrap_or("lorem ipsum")
content: str | None = result.unwrap_or()
# Get the Ok value or compute it from the exception
content: str = result.unwrap_or_else(lambda e: f"The exception was: {e}")
# Get the Err exception or None if it's an Ok
err: OSError | None = result.err()
# Handle the result using structural pattern matching
match result:
case Ok(content):
print("Text in upper:", content.upper())
case Err(FileNotFoundError() as e):
print("File not found:", e.filename)
It's also possible to wrap multiple exception types with the decorator:
@catch(OSError, UnicodeDecodeError)
def read_text(path: str) -> str:
with open(path) as f:
return f.read()
Or manually:
def read_text(path: str) -> Result[str, OSError | UnicodeDecodeError]:
try:
with open(path) as f:
return Ok(f.read())
except (OSError, UnicodeDecodeError) as e:
return Err(e)
There is also an async-compatible decorator:
from poltergeist import catch_async
@catch_async(OSError)
async def read_text(path: str) -> str:
with open(path) as f:
return f.read()
# Returns an object of type Result[str, OSError]
result = await read_text("my_file.txt")
Contributing
Set up the project using Poetry:
poetry install
Format the code:
make lint
Run tests:
make test
Check for typing and format issues:
make check
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
poltergeist-0.10.0.tar.gz
(4.2 kB
view details)
Built Distribution
File details
Details for the file poltergeist-0.10.0.tar.gz
.
File metadata
- Download URL: poltergeist-0.10.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.11.9 Linux/6.5.0-1018-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd9b6fb3d3ec07977ebe81fddd25b05ac21fc07989c7fc7afeb6307be759a0d9 |
|
MD5 | 9df28e13eaca316156260515cb867caf |
|
BLAKE2b-256 | f2627e15904842d1e326e02d95373479ccdd5f7464115b1e1afe9af5a31aa313 |
File details
Details for the file poltergeist-0.10.0-py3-none-any.whl
.
File metadata
- Download URL: poltergeist-0.10.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.11.9 Linux/6.5.0-1018-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4f73e0265e5b6e87ec43a6e74da620fee0bfb204ebf82dc6dbe6f06591813d3 |
|
MD5 | 1a21a9cdd560c6f8c78484c53c07ed83 |
|
BLAKE2b-256 | bba161459b824598a13f1f88593895f295f839edd95b969da2540f3b3a93e9d1 |