Skip to content

Commit e8210b8

Browse files
Y-T-Gglenn-jocher
andauthored
Fix subpackage imports and handle duplicate imports (#17)
Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com> Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com> Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
1 parent 46f307f commit e8210b8

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

autoimport/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
from autoimport.main import LazyLoader, lazy
44

55
__all__ = ("LazyLoader", "lazy")
6-
__version__ = "0.0.1"
6+
__version__ = "0.0.2"

autoimport/main.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,25 @@ def lazy_import(name, globals=None, locals=None, fromlist=(), level=0):
7171
**{from_item: self._lazy_modules[f"{module_name}.{from_item}"] for from_item in fromlist}
7272
)
7373
else:
74-
if module_name not in self._lazy_modules:
74+
if module_name in globals:
75+
self._lazy_modules[module_name] = globals[module_name]
76+
elif module_name in sys.modules:
77+
self._lazy_modules[module_name] = sys.modules[module_name] # module already loaded in session
78+
elif module_name not in self._lazy_modules:
7579
self._lazy_modules[module_name] = LazyLoader(module_name)
80+
81+
if "." in name: # we need to send loader for parent before subpackage
82+
parts = name.split('.')
83+
parent = ['.'.join(parts[:i]) for i in range(1, len(parts))][0]
84+
return LazyLoader(parent)
85+
7686
return self._lazy_modules[module_name]
7787

7888
builtins.__import__ = lazy_import
79-
return self
8089

8190
def __exit__(self, *args):
8291
"""Restores the original import mechanism and updates sys.modules with any loaded lazy modules."""
8392
builtins.__import__ = self._original_import
84-
# Now that builtins is restored, we can load any modules that need loading
85-
for name, lazy_module in self._lazy_modules.items():
86-
if name in sys.modules: # Update sys.modules to avoid issues with subsequent imports
87-
sys.modules[name] = lazy_module
88-
8993

9094
if __name__ == "__main__":
9195
import time

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ dependencies = []
6565
# Optional dependencies ------------------------------------------------------------------------------------------------
6666
[project.optional-dependencies]
6767
dev = [
68-
"numpy", # for import tests
68+
"numpy", # for tests
6969
]
7070

7171
[project.urls] # Optional

tests/test_autoimport.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ def test_attribute_access(self):
3131
self.assertIsInstance(result, str)
3232

3333
def test_submodule_imports(self):
34-
"""Test nested module imports and functionality."""
34+
"""Test numpy module imports and functionality."""
3535
with lazy():
3636
import numpy.random
3737
random_number = numpy.random.rand()
3838
self.assertLess(random_number, 1.0)
39-
39+
4040
def test_direct_lazyloader(self):
4141
"""Test direct LazyLoader instantiation."""
4242
base64_lazy = LazyLoader("base64")

0 commit comments

Comments
 (0)