Skip to content

Commit

Permalink
Renamed decorator from poltergeist to catch
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandermalyga committed May 21, 2023
1 parent f10f59a commit 577636a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ pip install poltergeist

## Examples

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

```python
from poltergeist import poltergeist
from poltergeist import catch

# Handle an exception type potentially raised within the function
@poltergeist(OSError)
@catch(OSError)
def read_text(path: str) -> str:
with open(path) as f:
return f.read()
Expand Down Expand Up @@ -69,7 +69,7 @@ match result:
It's also possible to wrap multiple exception types with the decorator:

```python
@poltergeist(OSError, UnicodeDecodeError)
@catch(OSError, UnicodeDecodeError)
def read_text(path: str) -> str:
with open(path) as f:
return f.read()
Expand Down
4 changes: 2 additions & 2 deletions poltergeist/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from poltergeist.decorator import poltergeist
from poltergeist.decorator import catch
from poltergeist.result import Err, Ok, Result

__all__ = ["Err", "Ok", "Result", "poltergeist"]
__all__ = ["Err", "Ok", "Result", "catch"]
2 changes: 1 addition & 1 deletion poltergeist/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
P = ParamSpec("P")


def poltergeist(
def catch(
*errors: Type[E],
) -> Callable[[Callable[P, T]], Callable[P, Result[T, E]]]:
def decorator(func: Callable[P, T]) -> Callable[P, Result[T, E]]:
Expand Down
14 changes: 7 additions & 7 deletions tests/mypy/test_decorator.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
- case: decorator_single_error
main: |
from poltergeist import poltergeist, Result
from poltergeist import catch, Result
@poltergeist(ValueError)
@catch(ValueError)
def test(a: int, b: str) -> float | None: ...
reveal_type(test) # N: Revealed type is "def (a: builtins.int, b: builtins.str) -> Union[poltergeist.result.Ok[Union[builtins.float, None]], poltergeist.result.Err[builtins.ValueError]]"
- case: decorator_multiple_errors
skip: True # TODO: Enable this test once MyPy properly detects the return type
main: |
from poltergeist import poltergeist, Result
from poltergeist import catch, Result
@poltergeist(ValueError, TypeError)
@catch(ValueError, TypeError)
def test(a: int, b: str) -> float | None: ...
reveal_type(test) # N: Revealed type is "def (a: builtins.int, b: builtins.str) -> Union[poltergeist.result.Ok[Union[builtins.float, None]], poltergeist.result.Err[Union[builtins.ValueError, builtins.TypeError]]]"
- case: decorator_invalid_error_type
main: |
from poltergeist import poltergeist, Result
from poltergeist import catch, Result
@poltergeist(123)
@catch(123)
def test(a: int, b: str) -> float | None: ...
out: |
main:3: error: Argument 1 to "poltergeist" has incompatible type "int"; expected "Type[<nothing>]" [arg-type]
main:3: error: Argument 1 to "catch" has incompatible type "int"; expected "Type[<nothing>]" [arg-type]
8 changes: 4 additions & 4 deletions tests/test_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import pytest

from poltergeist import Err, Ok, poltergeist
from poltergeist import Err, Ok, catch


def test_decorator() -> None:
decorated = poltergeist(ZeroDivisionError)(operator.truediv)
decorated = catch(ZeroDivisionError)(operator.truediv)

assert decorated(4, 2) == Ok(2)

Expand All @@ -20,7 +20,7 @@ def test_decorator() -> None:

def test_decorator_other_error() -> None:
# Only catching instances of ValueError
decorated = poltergeist(ValueError)(operator.truediv)
decorated = catch(ValueError)(operator.truediv)

assert decorated(4, 2) == Ok(2)

Expand All @@ -30,7 +30,7 @@ def test_decorator_other_error() -> None:


def test_decorator_multiple_errors() -> None:
decorated = poltergeist(ZeroDivisionError, TypeError)(operator.truediv)
decorated = catch(ZeroDivisionError, TypeError)(operator.truediv)

assert decorated(4, 2) == Ok(2)

Expand Down

0 comments on commit 577636a

Please sign in to comment.