lazy_fixture_callable.py 966 B

123456789101112131415161718192021222324252627282930313233
  1. from typing import Callable, Optional, Union
  2. import pytest
  3. from .lazy_fixture import LazyFixtureWrapper
  4. class LazyFixtureCallableWrapper(LazyFixtureWrapper):
  5. _func: Optional[Callable]
  6. args: tuple
  7. kwargs: dict
  8. def __init__(self, func_or_name: Union[Callable, str], *args, **kwargs):
  9. if callable(func_or_name):
  10. self._func = func_or_name
  11. self.name = func_or_name.__name__
  12. else:
  13. self.name = func_or_name
  14. self._func = None
  15. self.args = args
  16. self.kwargs = kwargs
  17. def get_func(self, request: pytest.FixtureRequest) -> Callable:
  18. func = self._func
  19. if func is None:
  20. func = self.load_fixture(request)
  21. assert callable(func)
  22. return func
  23. def lfc(name: Union[Callable, str], *args, **kwargs) -> LazyFixtureCallableWrapper:
  24. """lfc is a lazy fixture callable."""
  25. return LazyFixtureCallableWrapper(name, *args, **kwargs)