Skip to main content

poltergeist

pypi versions

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

Metadata

Release files for poltergeist 0.10.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 poltergeist 0.10.0
File Size Uploaded Provenance Actions
poltergeist-0.10.0.tar.gz 4.2 kB Without provenance Details for poltergeist-0.10.0.tar.gz

Built distribution (wheel)

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

Total release size: 8.7 kB

Release files / poltergeist-0.10.0.tar.gz

Download URL poltergeist-0.10.0.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
bd9b6fb3d3ec07977ebe81fddd25b05ac21fc07989c7fc7afeb6307be759a0d9
BLAKE2b-256 checksum
How to use checksums
f2627e15904842d1e326e02d95373479ccdd5f7464115b1e1afe9af5a31aa313
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.11.9 Linux/6.5.0-1018-azure

Release files / poltergeist-0.10.0-py3-none-any.whl

Download URL poltergeist-0.10.0-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c4f73e0265e5b6e87ec43a6e74da620fee0bfb204ebf82dc6dbe6f06591813d3
BLAKE2b-256 checksum
How to use checksums
bba161459b824598a13f1f88593895f295f839edd95b969da2540f3b3a93e9d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.11.9 Linux/6.5.0-1018-azure

Security for poltergeist 0.10.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

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