Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion etils/epy/lazy_imports_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ def _module(self) -> types.ModuleType:
# When multiple threads try to import the same module, make sure only
# one of them is importing it at the same time.
with _LOCK_PER_MODULE[self.module_name]:
module = importlib.import_module(self.module_name)
object.__setattr__(self, "_importing", True)
try:
module = importlib.import_module(self.module_name)
finally:
object.__setattr__(self, "_importing", False)
except ImportError as e:
if self.error_callback is not None:
if isinstance(self.error_callback, str):
Expand Down Expand Up @@ -119,6 +123,11 @@ def __getattr__(self, name: str) -> Any:
if name in self._submodules:
# known submodule accessed. Do not trigger import
return self._submodules[name]
elif _getattr_static(self, "_importing", False):
raise AttributeError(
f"Module '{self.module_name}' is currently being imported, no"
f" attribute '{name}'"
)
else:
return getattr(self._module, name)

Expand Down
Loading