_util.py 930 B

123456789101112131415161718192021222324252627282930313233343536
  1. from typing import Union
  2. _mock_module = None
  3. def get_mock_module(config):
  4. """
  5. Import and return the actual "mock" module. By default this is
  6. "unittest.mock", but the user can force to always use "mock" using
  7. the mock_use_standalone_module ini option.
  8. """
  9. global _mock_module
  10. if _mock_module is None:
  11. use_standalone_module = parse_ini_boolean(
  12. config.getini("mock_use_standalone_module")
  13. )
  14. if use_standalone_module:
  15. import mock
  16. _mock_module = mock
  17. else:
  18. import unittest.mock
  19. _mock_module = unittest.mock
  20. return _mock_module
  21. def parse_ini_boolean(value: Union[bool, str]) -> bool:
  22. if isinstance(value, bool):
  23. return value
  24. if value.lower() == "true":
  25. return True
  26. if value.lower() == "false":
  27. return False
  28. raise ValueError("unknown string for bool: %r" % value)