Skip to content

Commit

Permalink
Raise better errors from lazy __getattr__ (napari#4848)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 authored Jul 20, 2022
1 parent 5827a55 commit 03e6ce1
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion napari/_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,20 @@ def __getattr__(name):
if name in submodules:
return import_module(f'{module_name}.{name}')
elif name in attr_to_modules:
submod = import_module(f'{module_name}.{attr_to_modules[name]}')
try:
submod = import_module(
f'{module_name}.{attr_to_modules[name]}'
)
except AttributeError as er:
# if we want any useful error message to show
# (besides just "cannot import name...") then we need raise anything
# BUT an attribute error here, because the __getattr__ protocol will
# swallow that error.
raise ImportError(
f'Failed to import {attr_to_modules[name]} from {module_name}. '
'See cause above'
) from er
# this is where we allow an attribute error to be raised.
return getattr(submod, name)
else:
raise AttributeError(f'No {module_name} attribute {name}')
Expand Down

0 comments on commit 03e6ce1

Please sign in to comment.