Skip to content

lazy ¤

This module contains a lazy decorator.

LazyCallable ¤

LazyCallable(
    call: Callable[_P, _R],
    args: tuple,
    kwargs: dict,
    name: str | None = None,
)

Bases: Generic[_R]

This class allows users to create and pass lazy callables to the runner.

Parameters:

  • call (Callable[_P, _R]) –

    The origin callable.

  • args (tuple) –

    The *args to pass when calling.

  • kwargs (dict) –

    The **kwargs to pass when calling.

  • name (str | None, default: None ) –

    The name of the callable.

Source code in src/failprint/lazy.py
22
23
24
25
26
27
28
29
30
31
32
33
34
def __init__(self, call: Callable[_P, _R], args: tuple, kwargs: dict, name: str | None = None) -> None:
    """Initialize a lazy callable.

    Parameters:
        call: The origin callable.
        args: The `*args` to pass when calling.
        kwargs: The `**kwargs` to pass when calling.
        name: The name of the callable.
    """
    self.call = call
    self.args = args
    self.kwargs = kwargs
    self.name = name

lazy ¤

lazy(
    call: Callable[_P, _R], name: str | None = None
) -> Callable[_P, LazyCallable]
lazy(
    call: None = None, name: str | None = None
) -> _DecoratorType
lazy(
    call: Callable[_P, _R] | None = None,
    name: str | None = None,
) -> Callable[_P, LazyCallable] | _DecoratorType

Transform a callable into a lazy callable.

Being able to create a lazy callable improves the UX/DX. Instead of having to pass args and kwargs to the runner, one can now call the function directly, enjoying auto-completion and other editor features. Before:

from failprint.runners import run


def greet(name):
    return f"hello {name}"


run(greet, args=["tim"])

After:

from failprint.runners import run
from failprint.lazy import lazy


@lazy
def greet(name):
    return f"hello {name}"


run(greet("tim"))

Parameters:

  • call (Callable[_P, _R] | None, default: None ) –

    The callable to lazify.

  • name (str | None, default: None ) –

    An optional name to give to the new callable.

Returns:

Source code in src/failprint/lazy.py
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def lazy(call: Callable[_P, _R] | None = None, name: str | None = None) -> Callable[_P, LazyCallable] | _DecoratorType:
    """Transform a callable into a lazy callable.

    Being able to create a lazy callable improves the UX/DX.
    Instead of having to pass `args` and `kwargs` to the runner,
    one can now call the function directly, enjoying auto-completion
    and other editor features. Before:

    ```python
    from failprint.runners import run


    def greet(name):
        return f"hello {name}"


    run(greet, args=["tim"])
    ```

    After:

    ```python
    from failprint.runners import run
    from failprint.lazy import lazy


    @lazy
    def greet(name):
        return f"hello {name}"


    run(greet("tim"))
    ```

    Parameters:
        call: The callable to lazify.
        name: An optional name to give to the new callable.

    Returns:
        A lazy callable instance.
    """
    if name is None and isinstance(call, str):
        call, name = name, call
        warnings.warn(
            "Passing a name as positional argument is deprecated. Use a keyword argument instead.",
            DeprecationWarning,
            stacklevel=2,
        )

    if call is None:

        def decorator(func: _FunctionType) -> _FunctionType:
            return _lazy(func, name)

        return decorator

    return _lazy(call, name)