diff --git a/CHANGELOG.md b/CHANGELOG.md index 00bdfe1c..707af5fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ Changelog follow https://keepachangelog.com/ format. consistency with `ecolab.adhoc` * Added: `epy.pretty_repr_top_level` * Added: `epy.wraps_cls` equivalent of `functools.wraps` but for classes. + * Breaking: `epy.lazy_imports(error_callback=)` has now signature + `(Exception) -> None` (instead of `(str) -> None`) * Fixed: `epy.pretty_repr` missing trailing `,` for tuple with single element. * `ecolab`: diff --git a/etils/epy/lazy_imports_utils.py b/etils/epy/lazy_imports_utils.py index 364f33ae..fea2d92a 100644 --- a/etils/epy/lazy_imports_utils.py +++ b/etils/epy/lazy_imports_utils.py @@ -33,7 +33,7 @@ from typing import Any, Callable, Iterator -_ErrorCallback = Callable[[str], None] +_ErrorCallback = Callable[[Exception], None] _SuccessCallback = Callable[[str], None] @@ -68,9 +68,9 @@ def _module(self) -> types.ModuleType: if self.success_callback is not None: self.success_callback(self.module_name) return module - except ImportError: + except ImportError as e: if self.error_callback is not None: - self.error_callback(self.module_name) + self.error_callback(e) raise def __getattr__(self, name: str) -> Any: @@ -124,8 +124,8 @@ def lazy_imports( the original `ecolab.adhoc` context is re-created to import the lazy module. Args: - error_callback: a callback to trigger when an import fails. The callback is - passed the name of the imported module as an arg. + error_callback: a callback to trigger when an import fails. The exception is + passed as an arg, so user can use `epy.reraise(e, 'Additional message')`. success_callback: a callback to trigger when an import succeeds. The callback is passed the name of the imported module as an arg. diff --git a/etils/epy/lazy_imports_utils_test.py b/etils/epy/lazy_imports_utils_test.py index b2949ab7..780fbf40 100644 --- a/etils/epy/lazy_imports_utils_test.py +++ b/etils/epy/lazy_imports_utils_test.py @@ -79,7 +79,7 @@ def test_error_callback(): _ = doesnotexist.stack except ImportError: pass - error_callback.assert_called_once_with('doesnotexist') + error_callback.assert_called_once() success_callback.assert_not_called()