mnmelo / lazy_import

A module for lazy loading of Python modules
Other
86 stars 29 forks source link

Lazy Packages Fail to Maintain Lazy Submodule Context Post-Load #15

Open matthewphsmith opened 5 years ago

matthewphsmith commented 5 years ago

Two issues I am seeing in Python 2.7:

  1. Only the first lazy submodule will appear in a package shared by two lazy submodules.
    
    la = lazy_import.lazy_module('package.a')
    lb = lazy_import.lazy_module('package.b')

import package 'a' in type(package)._lazy_import_submodules 'b' not in type(package)._lazy_import_submodules


This is because when a cached lazy package is found (https://github.com/mnmelo/lazy_import/blob/master/lazy_import/__init__.py#L378), we don't update the reference of _LazyModule to the cached module and hence the module from the last iteration of the loop has it's `_lazy_import_submodules` attribute updated. Thus resulting in the following:

'b' in type(lb)._lazy_import_submodules


2. After a package is loaded, references to lazy modules are lost, resulting in:

la = lazy_import.lazy_module('package.a')

import package package.a == la from package import a !!!! fails !!!!



This problem is rooted here: https://github.com/mnmelo/lazy_import/blob/master/lazy_import/__init__.py#L689

The issue is that prior to reloading the module, we clean all lazy sub-module attributes and LazyModule class attributes. We then try to restore those attributes, but we rely on a class attribute which has been removed. Thus no sub-modules are found and the lazy state isn't restored.
matthewphsmith commented 5 years ago

Proposed fix: https://github.com/mnmelo/lazy_import/pull/14