diff --git a/README.md b/README.md index b3c84d8..b889446 100644 --- a/README.md +++ b/README.md @@ -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() @@ -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() diff --git a/poltergeist/__init__.py b/poltergeist/__init__.py index 9fa3dff..0ceba96 100644 --- a/poltergeist/__init__.py +++ b/poltergeist/__init__.py @@ -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"] diff --git a/poltergeist/decorator.py b/poltergeist/decorator.py index 909498d..14c96be 100644 --- a/poltergeist/decorator.py +++ b/poltergeist/decorator.py @@ -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]]: diff --git a/tests/mypy/test_decorator.yml b/tests/mypy/test_decorator.yml index 44afc38..435f33d 100644 --- a/tests/mypy/test_decorator.yml +++ b/tests/mypy/test_decorator.yml @@ -1,8 +1,8 @@ - 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]]" @@ -10,18 +10,18 @@ - 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[]" [arg-type] + main:3: error: Argument 1 to "catch" has incompatible type "int"; expected "Type[]" [arg-type] diff --git a/tests/test_decorator.py b/tests/test_decorator.py index ae91e37..6e1cf9a 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -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) @@ -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) @@ -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)