Skip to content

Commit

Permalink
Fixed a serious bug when lazy-loading a submodule of a fully-loaded b…
Browse files Browse the repository at this point in the history
…ase.

Closes #2

Bumped version to 0.2.2
  • Loading branch information
mnmelo committed Jan 22, 2018
1 parent eb4de50 commit 0c66e99
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
22/01/2018 v0.2.2
- fixed a serious bug when lazy-loading a submodule of a fully-loaded base.
17/01/2018 v0.2.1
- annoying release to fix install notice on PyPi (because README.rst was
incorrect).
Expand Down
2 changes: 1 addition & 1 deletion lazy_import/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.1
0.2.2
2 changes: 1 addition & 1 deletion lazy_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def __repr__(self):
# ModuleSpec(modname, None))
if fullsubmodname:
submod = sys.modules[fullsubmodname]
super(LazyModule, mod).__setattr__(submodname, submod)
ModuleType.__setattr__(mod, submodname, submod)
_LazyModule._lazy_import_submodules[submodname] = submod
fullsubmodname = modname
modname, _, submodname = modname.rpartition('.')
Expand Down
17 changes: 16 additions & 1 deletion lazy_import/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class _TestLazyModule(lazy_import.LazyModule):
pass

_GENERATED_NAMES = []
# Modules not usually loaded on startup
# Modules not usually loaded on startup. Must include at least one with
# submodule
NAMES_EXISTING = ("sched", "distutils.core")

LEVELS = ("leaf", "base")
Expand Down Expand Up @@ -243,3 +244,17 @@ def test_callable_missing(modname, errors, cnames, fn):
_check_callable_missing(lazy, msg=expected_err)


@pytest.mark.parametrize("modname",
[modname for modname in NAMES_EXISTING if '.' in modname])
def test_load_lazysub_on_fullmodule(modname, lazy_opts):
level, modclass, errors = lazy_opts
base = modname.split('.')[0]
# A fully loaded base...
importlib.import_module(base)
# and a lazy sub...
mod = lazy_import.lazy_module(modname, error_strings=errors,
lazy_mod_class=modclass, level=level)
if level == 'base':
assert not isinstance(mod, modclass)
else:
assert isinstance(mod, modclass)

0 comments on commit 0c66e99

Please sign in to comment.