Skip to content

toolr.utils._imports

Imports related utilities.

report_on_import_errors

report_on_import_errors(message: str) -> Iterator[None]

Catch import errors and raise a CommandDependencyNotFoundError.

Source code in python/toolr/utils/_imports.py
@contextmanager
def report_on_import_errors(message: str) -> Iterator[None]:
    """
    Catch import errors and raise a CommandDependencyNotFoundError.
    """
    try:
        yield
    except ModuleNotFoundError as exc:
        # Suppress the current frame (the yield line) from the traceback
        # We just want to show the user the import error from the code that actually uses the command
        if TYPE_CHECKING:
            assert exc.__traceback__ is not None
        exc.__traceback__ = exc.__traceback__.tb_next
        log.warning(message, exc_info=exc)