Skip to content

Commit

Permalink
Fix module_lazy_loader.py error handling to catch ImportErrors instea…
Browse files Browse the repository at this point in the history
…d of ModuleNotFoundErrors.

ModuleNotFoundError is a subclass of ImportError.

PiperOrigin-RevId: 621664440
Change-Id: I42dd73382fdb2358587a5937dbb9c0ce0f1cc0e0
GitOrigin-RevId: 39c428fafef22070e101cf57542831eda4902a54
  • Loading branch information
fionalang authored and alpiccioni committed Dec 4, 2024
1 parent 1a72048 commit 38aa671
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions xmanager/module_lazy_loader/module_lazy_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ def get_module_getattr(
) -> Callable[[str], types.ModuleType | Any | None]:
"""Returns __getattr__ for the xmanager sub-package __init__.py file."""

def _import_module_with_reloaded_parent(
module_name: str, e: ModuleNotFoundError
):
def _import_module_with_reloaded_parent(module_name: str, e: ImportError):
# reload module's parent as a last resort (likely in the case that a
# module was imported outside adhoc import context but later
# used within it). Assuming the parent package has a lazy-loaded
Expand All @@ -90,7 +88,7 @@ def _import_module_with_reloaded_parent(
def _import_module(module_name: str):
try:
return importlib.import_module(module_name)
except ModuleNotFoundError as e:
except ImportError as e:
return _import_module_with_reloaded_parent(e)

def _module_getattr(name: str) -> types.ModuleType | Any | None:
Expand Down

0 comments on commit 38aa671

Please sign in to comment.