-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from DavidCEllis/improve_coverage
Prevent relative imports without an assigned name. Force asname values to be valid python identifiers. Remove unreachable exceptions where python will error in the import. Fix an infinite loop on invalid input data. Add eager_process parameter to force early processing of imports. Excluded from repr. Add globs to LazyImporter repr. Fix get_importer_state bug with lazy processing of imports. Add tests for these.
- Loading branch information
Showing
7 changed files
with
513 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
from ducktools.lazyimporter import ( | ||
LazyImporter, | ||
FromImport, | ||
get_module_funcs, | ||
) | ||
|
||
name = "ex_othermod" | ||
|
||
laz = LazyImporter( | ||
[FromImport("..ex_mod.ex_submod", "name")], | ||
[FromImport("..ex_mod.ex_submod", "name", "submod_name")], | ||
globs=globals(), | ||
) | ||
|
||
__getattr__, __dir__ = get_module_funcs(laz, module_name=__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
Test the external functions | ||
""" | ||
|
||
from ducktools.lazyimporter import ( | ||
ModuleImport, | ||
FromImport, | ||
MultiFromImport, | ||
TryExceptImport, | ||
_SubmoduleImports, | ||
MultiFromImport, | ||
get_importer_state, | ||
get_module_funcs, | ||
LazyImporter, | ||
) | ||
|
||
|
||
class TestImporterState: | ||
def test_module_importer_state(self): | ||
laz = LazyImporter([ModuleImport("collections")]) | ||
|
||
state = get_importer_state(laz) | ||
|
||
assert state["lazy_attributes"] == ["collections"] | ||
assert state["imported_attributes"] == {} | ||
|
||
collections_mod = laz.collections | ||
|
||
state = get_importer_state(laz) | ||
|
||
assert state["lazy_attributes"] == [] | ||
assert state["imported_attributes"] == {"collections": collections_mod} | ||
|
||
|
||
class TestModuleFuncs: | ||
def test_getattr_func(self): | ||
import collections | ||
|
||
laz = LazyImporter([ModuleImport("collections")]) | ||
|
||
getattr_func, _ = get_module_funcs(laz) | ||
|
||
assert getattr_func("collections") is collections | ||
|
||
def test_dir_func(self): | ||
laz = LazyImporter([ModuleImport("collections")]) | ||
|
||
_, dir_func = get_module_funcs(laz) | ||
|
||
assert dir_func() == ["collections"] | ||
|
||
def test_getattr_module_func(self): | ||
import example_modules.ex_othermod as ex_othermod # noqa # pyright: ignore | ||
|
||
assert ex_othermod.submod_name == "ex_submod" | ||
|
||
def test_dir_module_func(self): | ||
import example_modules.ex_othermod as ex_othermod # noqa # pyright: ignore | ||
|
||
assert "name" in dir(ex_othermod) | ||
assert "submod_name" in dir(ex_othermod) |
Oops, something went wrong.