Metadata-Version: 2.1
Name: poltergeist
Version: 0.2.2
Summary: Experimental Rust-like error handling in Python, with type-safety in mind.
Home-page: https://github.com/alexandermalyga/poltergeist
License: MIT
Author: Alexander Malyga
Author-email: alexander@malyga.io
Requires-Python: >=3.10,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Project-URL: Repository, https://github.com/alexandermalyga/poltergeist
Description-Content-Type: text/markdown

# poltergeist

[![pypi](https://img.shields.io/pypi/v/poltergeist.svg)](https://pypi.python.org/pypi/poltergeist)
[![versions](https://img.shields.io/pypi/pyversions/poltergeist.svg)](https://github.com/alexandermalyga/poltergeist)

Experimental Rust-like error handling in Python, with type-safety in mind.

## Installation

```
pip install poltergeist
```

## Examples

Use the provided `@poltergeist` decorator on any function:

```python
from pathlib import Path
from poltergeist import Err, Ok, poltergeist

@poltergeist(FileNotFoundError)
def read_text(path: Path) -> str:
    return path.read_text()

result = read_text(Path("test.txt"))

# Handle errors using structural pattern matching
match result:
    case Ok(content):
        # Type-checkers know that content is a string,
        # carried over from the return type of the original function.
        print("File content:", content)
    case Err(e):
        # The exception type is also known
        print("File not found:", e.filename)

# Or directly get the returned value
# This will raise the original exception, if there was one
content = result.unwrap()
```

You can also wrap errors yourself:

```python
from pathlib import Path
from poltergeist import Err, Ok, Result

def read_text(path: Path) -> Result[str, FileNotFoundError]:
    try:
        return Ok(path.read_text())
    except FileNotFoundError as e:
        return Err(e)
```

Both of these examples pass type checking and provide in-editor autocompletion.

