-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow specifying multiple exception types in the decorator, and remov…
…e using Exception by default
- Loading branch information
1 parent
fe0b8bc
commit f10f59a
Showing
4 changed files
with
52 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,23 @@ | ||
import functools | ||
from typing import Any, Callable, ParamSpec, Type, overload | ||
from typing import Callable, ParamSpec, Type | ||
|
||
from poltergeist.result import E, Err, Ok, Result, T | ||
|
||
P = ParamSpec("P") | ||
|
||
|
||
@overload | ||
def poltergeist(func: Callable[P, T], /) -> Callable[P, Result[T, Exception]]: | ||
# Called as @poltergeist | ||
... | ||
|
||
|
||
@overload | ||
def poltergeist() -> Callable[[Callable[P, T]], Callable[P, Result[T, Exception]]]: | ||
# Called as @poltergeist() | ||
... | ||
|
||
|
||
@overload | ||
def poltergeist( | ||
*, | ||
error: Type[E], | ||
*errors: Type[E], | ||
) -> Callable[[Callable[P, T]], Callable[P, Result[T, E]]]: | ||
# Called as @poltergeist(error=SomeError) | ||
... | ||
|
||
|
||
def poltergeist(func: Any = None, /, *, error: Any = Exception) -> Any: | ||
""" | ||
Decorator that wraps the result of a function into an Ok object if it | ||
executes without raising an exception. Otherwise, returns an Err object with | ||
the exception raised by the function. | ||
""" | ||
|
||
if func is None: | ||
return functools.partial(poltergeist, error=error) | ||
|
||
@functools.wraps(func) | ||
def wrapper(*args: Any, **kwargs: Any) -> Any: | ||
try: | ||
result = func(*args, **kwargs) | ||
except error as e: | ||
return Err(e) | ||
return Ok(result) | ||
|
||
return wrapper | ||
def decorator(func: Callable[P, T]) -> Callable[P, Result[T, E]]: | ||
@functools.wraps(func) | ||
def wrapper(*args: P.args, **kwargs: P.kwargs) -> Result[T, E]: | ||
try: | ||
result = func(*args, **kwargs) | ||
except errors as e: | ||
return Err(e) | ||
return Ok(result) | ||
|
||
return wrapper | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,27 @@ | ||
- case: decorator_no_args | ||
- case: decorator_single_error | ||
main: | | ||
from poltergeist import poltergeist, Result | ||
@poltergeist | ||
@poltergeist(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.Exception]]" | ||
- case: decorator_default | ||
main: | | ||
from poltergeist import poltergeist, Result | ||
@poltergeist() | ||
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.Exception]]" | ||
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_with_args | ||
- case: decorator_multiple_errors | ||
skip: True # TODO: Enable this test once MyPy properly detects the return type | ||
main: | | ||
from poltergeist import poltergeist, Result | ||
@poltergeist(error=ValueError) | ||
@poltergeist(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[builtins.ValueError]]" | ||
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 | ||
@poltergeist(error=123) | ||
@poltergeist(123) | ||
def test(a: int, b: str) -> float | None: ... | ||
out: | | ||
main:3: error: No overload variant of "poltergeist" matches argument type "int" [call-overload] | ||
main:3: note: Possible overload variants: | ||
main:3: note: def [P`-1, T] poltergeist(Callable[P, T], /) -> Callable[P, Union[Ok[T], Err[Exception]]] | ||
main:3: note: def poltergeist() -> Callable[[Callable[P, T]], Callable[P, Union[Ok[T], Err[Exception]]]] | ||
main:3: note: def [E <: BaseException] poltergeist(*, error: Type[E]) -> Callable[[Callable[P, T]], Callable[P, Union[Ok[T], Err[E]]]] | ||
main:3: error: Argument 1 to "poltergeist" has incompatible type "int"; expected "Type[<nothing>]" [arg-type] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters