18 from typing
import Any, Callable, TypeVar, Generic, Union, NoReturn, Type, Tuple
19 from typing_extensions
import ParamSpec
24 ParamsType = ParamSpec(
"ParamsType")
25 ReturnType = TypeVar(
"ReturnType")
26 ExceptionType = TypeVar(
"ExceptionType", bound = BaseException)
29 class Success(Generic[ReturnType]):
31 __match_args__ = (
"value",)
33 def __init__(self, value: ReturnType) ->
None:
36 def unwrap(self) -> ReturnType:
40 class Error(Generic[ReturnType]):
42 __match_args__ = (
"exception",)
44 def __init__(self, exception: ExceptionType) ->
None:
45 self.exception = exception
47 def unwrap(self) -> NoReturn:
51 class Throws(Generic[ExceptionType]):
53 def __init__(self, exceptions: Tuple[Type[ExceptionType], ...]) ->
None:
54 self.exceptions = exceptions
56 def __call__(self, function: Callable[ParamsType, ReturnType]) -> Callable[ParamsType, Union[Success[ReturnType], Error[ExceptionType]]]:
57 def inner(*args: Any, **kwargs: Any) -> Union[Success[ReturnType], Error]:
59 result = function(*args, **kwargs)
60 return Success(result)
61 except KeyboardInterrupt:
63 except BaseException
as ex:
64 if len(self.exceptions) > 0
and not isinstance(ex, self.exceptions):
65 logging.warning(
">> [Coretex] Received unexpected exception")
70 inner.__name__ = function.__name__