Skip to content

Decorators

Early return decorators.

early_option(function: OptionCallable[P, T]) -> OptionCallable[P, T]

Decorates the function returning Option[T] to handle early returns via the early (? in Rust) operator.

Parameters:

Name Type Description Default
function OptionCallable[P, T]

The function to wrap.

required

Returns:

Type Description
OptionCallable[P, T]

The wrapping function.

Source code in src/wraps/early/decorators.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def early_option(function: OptionCallable[P, T]) -> OptionCallable[P, T]:
    """Decorates the `function` returning [`Option[T]`][wraps.option.Option]
    to handle *early returns* via the `early` (`?` in Rust) operator.

    Arguments:
        function: The function to wrap.

    Returns:
        The wrapping function.
    """

    @wraps(function)
    def wrap(*args: P.args, **kwargs: P.kwargs) -> Option[T]:
        try:
            return function(*args, **kwargs)

        except EarlyOption:
            return NULL

    return wrap

early_option_await(function: OptionAsyncCallable[P, T]) -> OptionAsyncCallable[P, T]

Decorates the asynchronous function returning Option[T] to handle early returns via the early (? in Rust) operator.

Parameters:

Name Type Description Default
function OptionAsyncCallable[P, T]

The asynchronous function to wrap.

required

Returns:

Type Description
OptionAsyncCallable[P, T]

The asynchronous wrapping function.

Source code in src/wraps/early/decorators.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def early_option_await(function: OptionAsyncCallable[P, T]) -> OptionAsyncCallable[P, T]:
    """Decorates the asynchronous `function` returning [`Option[T]`][wraps.option.Option]
    to handle *early returns* via the `early` (`?` in Rust) operator.

    Arguments:
        function: The asynchronous function to wrap.

    Returns:
        The asynchronous wrapping function.
    """

    @wraps(function)
    async def wrap(*args: P.args, **kwargs: P.kwargs) -> Option[T]:
        try:
            return await function(*args, **kwargs)

        except EarlyOption:
            return NULL

    return wrap

early_result(function: ResultCallable[P, T, E]) -> ResultCallable[P, T, E]

Decorates the function returning Result[T, E] to handle early returns via the early (? in Rust) operator.

Parameters:

Name Type Description Default
function ResultCallable[P, T, E]

The function to wrap.

required

Returns:

Type Description
ResultCallable[P, T, E]

The wrapping function.

Source code in src/wraps/early/decorators.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def early_result(function: ResultCallable[P, T, E]) -> ResultCallable[P, T, E]:
    """Decorates the `function` returning [`Result[T, E]`][wraps.result.Result]
    to handle *early returns* via the `early` (`?` in Rust) operator.

    Arguments:
        function: The function to wrap.

    Returns:
        The wrapping function.
    """

    @wraps(function)
    def wrap(*args: P.args, **kwargs: P.kwargs) -> Result[T, E]:
        try:
            return function(*args, **kwargs)

        except EarlyResult[E] as early:
            return Err(early.error)

    return wrap

early_result_await(function: ResultAsyncCallable[P, T, E]) -> ResultAsyncCallable[P, T, E]

Decorates the asynchronous function returning Result[T, E] to handle early returns via the early (? in Rust) operator.

Parameters:

Name Type Description Default
function ResultAsyncCallable[P, T, E]

The asynchronous function to wrap.

required

Returns:

Type Description
ResultAsyncCallable[P, T, E]

The asynchronous wrapping function.

Source code in src/wraps/early/decorators.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def early_result_await(function: ResultAsyncCallable[P, T, E]) -> ResultAsyncCallable[P, T, E]:
    """Decorates the asynchronous `function` returning [`Result[T, E]`][wraps.result.Result]
    to handle *early returns* via the `early` (`?` in Rust) operator.

    Arguments:
        function: The asynchronous function to wrap.

    Returns:
        The asynchronous wrapping function.
    """

    @wraps(function)
    async def wrap(*args: P.args, **kwargs: P.kwargs) -> Result[T, E]:
        try:
            return await function(*args, **kwargs)

        except EarlyResult[E] as early:
            return Err(early.error)

    return wrap