Helper function to import submodules lazily in Python 3.7+ Parameters ---------- rel_modules: list of str list of submodules to import, of the form .submodule rel_classes: list of str list of submodule classes/variables to import, of the form ._submodule.Foo
(parent_name, rel_modules=(), rel_classes=())
| 2 | |
| 3 | |
| 4 | def relative_import(parent_name, rel_modules=(), rel_classes=()): |
| 5 | """ |
| 6 | Helper function to import submodules lazily in Python 3.7+ |
| 7 | |
| 8 | Parameters |
| 9 | ---------- |
| 10 | rel_modules: list of str |
| 11 | list of submodules to import, of the form .submodule |
| 12 | rel_classes: list of str |
| 13 | list of submodule classes/variables to import, of the form ._submodule.Foo |
| 14 | |
| 15 | Returns |
| 16 | ------- |
| 17 | tuple |
| 18 | Tuple that should be assigned to __all__, __getattr__ in the caller |
| 19 | """ |
| 20 | module_names = {rel_module.split(".")[-1]: rel_module for rel_module in rel_modules} |
| 21 | class_names = {rel_path.split(".")[-1]: rel_path for rel_path in rel_classes} |
| 22 | |
| 23 | def __getattr__(import_name): |
| 24 | # In Python 3.7+, lazy import submodules |
| 25 | |
| 26 | # Check for submodule |
| 27 | if import_name in module_names: |
| 28 | rel_import = module_names[import_name] |
| 29 | return importlib.import_module(rel_import, parent_name) |
| 30 | |
| 31 | # Check for submodule class |
| 32 | if import_name in class_names: |
| 33 | rel_path_parts = class_names[import_name].split(".") |
| 34 | rel_module = ".".join(rel_path_parts[:-1]) |
| 35 | class_name = import_name |
| 36 | class_module = importlib.import_module(rel_module, parent_name) |
| 37 | return getattr(class_module, class_name) |
| 38 | |
| 39 | raise AttributeError( |
| 40 | "module {__name__!r} has no attribute {name!r}".format( |
| 41 | name=import_name, __name__=parent_name |
| 42 | ) |
| 43 | ) |
| 44 | |
| 45 | __all__ = list(module_names) + list(class_names) |
| 46 | |
| 47 | def __dir__(): |
| 48 | return __all__ |
| 49 | |
| 50 | return __all__, __getattr__, __dir__ |
no test coverage detected