@brettcannon, thank you for your comments. I agree we have what it looks to be too many ways of controlling lazy imports, but these are the result of real needs. For example, we need the try
/ except
/ finally
for compatibility with a lot of code which uses the idiom:
try:
import foo
except ImportError:
foo = None
Imports being eager inside with
block was a byproduct of that idiom (since in the original implementation I checked f_iblock == 0
). We are now producing IMPORT
at module level only (outside blocks as you are suggesting) and EAGER_IMPORT
on all other imports, so we are no longer relying in the now defunct f_iblock
. We could maybe consider leaving out with
, but it made sense to have that do-nothing context manager to make things clearer (instead of using try
/ finally
).
I like the idea of making sys.flags
writable, but that doesn’t address having a container of module names that are to load things eagerly (the excluding
argument to set_lazy_imports()
); and this is needed for incompatible libraries.
enable_lazy_imports_in_module()
is for SciPy use cases, where it’s desirable to have all imports in a module being lazy, without affecting how it currently works anywhere else. We could of course explore other ways of doing it, what would you propose it’d be a cleaner way for this use case?