haystack/haystack/lazy_imports.py
Tobias Wochinger fe0ac5c4a2
chore: enforce kwarg logging (#7207)
* chore: add logger which eases logging of extras

* chore: start migrating to key value

* fix: import fixes

* tests: temporarily comment out breaking test

* refactor: move to kwarg based logging

* style: fix import order

* chore: implement self-review comments

* test: drop failing test

* chore: fix more import orders

* docs: add changelog

* tests: fix broken tests

* chore: fix getting the frames

* chore: add comment

* chore: cleanup

* chore: adapt remaining `%s` usages
2024-02-29 14:31:20 +01:00

45 lines
1.5 KiB
Python

from types import TracebackType
from typing import Optional, Type
from lazy_imports.try_import import _DeferredImportExceptionContextManager
DEFAULT_IMPORT_ERROR_MSG = "Try 'pip install {}'"
class LazyImport(_DeferredImportExceptionContextManager):
"""
Wrapper on top of lazy_import's _DeferredImportExceptionContextManager that adds the possibility to customize the
error messages.
"""
def __init__(self, message: str = DEFAULT_IMPORT_ERROR_MSG) -> None:
super().__init__()
self.import_error_msg = message
def __exit__(
self, exc_type: Optional[Type[Exception]], exc_value: Optional[Exception], traceback: Optional[TracebackType]
) -> Optional[bool]:
"""Exit the context manager.
Args:
exc_type:
Raised exception type. :obj:`None` if nothing is raised.
exc_value:
Raised exception object. :obj:`None` if nothing is raised.
traceback:
Associated traceback. :obj:`None` if nothing is raised.
Returns:
:obj:`None` if nothing is deferred, otherwise :obj:`True`.
:obj:`True` will suppress any exceptions avoiding them from propagating.
"""
if isinstance(exc_value, ImportError):
message = (
f"Failed to import '{exc_value.name}'. {self.import_error_msg.format(exc_value.name)}. "
f"Original error: {exc_value}"
)
self._deferred = (exc_value, message)
return True
return None