aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/pytest-lazy-fixtures/pytest_lazy_fixtures/lazy_fixture_callable.py
blob: a11b04091da8f3bbda6cfb4ce58abe0f333c0933 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from typing import Callable, Optional, Union

import pytest

from .lazy_fixture import LazyFixtureWrapper


class LazyFixtureCallableWrapper(LazyFixtureWrapper):
    _func: Optional[Callable]
    args: tuple
    kwargs: dict

    def __init__(self, func_or_name: Union[Callable, str], *args, **kwargs):
        if callable(func_or_name):
            self._func = func_or_name
            self.name = func_or_name.__name__
        else:
            self.name = func_or_name
            self._func = None
        self.args = args
        self.kwargs = kwargs

    def get_func(self, request: pytest.FixtureRequest) -> Callable:
        func = self._func
        if func is None:
            func = self.load_fixture(request)
            assert callable(func)
        return func


def lfc(name: Union[Callable, str], *args, **kwargs) -> LazyFixtureCallableWrapper:
    """lfc is a lazy fixture callable."""
    return LazyFixtureCallableWrapper(name, *args, **kwargs)