Module class that surfaces all objects but only performs associated imports when the objects are requested.
| 1589 | |
| 1590 | |
| 1591 | class _LazyModule(ModuleType): |
| 1592 | """ |
| 1593 | Module class that surfaces all objects but only performs associated imports when the objects are requested. |
| 1594 | """ |
| 1595 | |
| 1596 | # Very heavily inspired by optuna.integration._IntegrationModule |
| 1597 | # https://github.com/optuna/optuna/blob/master/optuna/integration/__init__.py |
| 1598 | def __init__(self, name, module_file, import_structure, module_spec=None, extra_objects=None): |
| 1599 | super().__init__(name) |
| 1600 | self._modules = set(import_structure.keys()) |
| 1601 | self._class_to_module = {} |
| 1602 | for key, values in import_structure.items(): |
| 1603 | for value in values: |
| 1604 | self._class_to_module[value] = key |
| 1605 | # Needed for autocompletion in an IDE |
| 1606 | self.__all__ = list(import_structure.keys()) + list(chain(*import_structure.values())) |
| 1607 | self.__file__ = module_file |
| 1608 | self.__spec__ = module_spec |
| 1609 | self.__path__ = [os.path.dirname(module_file)] |
| 1610 | self._objects = {} if extra_objects is None else extra_objects |
| 1611 | self._name = name |
| 1612 | self._import_structure = import_structure |
| 1613 | |
| 1614 | # Needed for autocompletion in an IDE |
| 1615 | def __dir__(self): |
| 1616 | result = super().__dir__() |
| 1617 | # The elements of self.__all__ that are submodules may or may not be in the dir already, depending on whether |
| 1618 | # they have been accessed or not. So we only add the elements of self.__all__ that are not already in the dir. |
| 1619 | for attr in self.__all__: |
| 1620 | if attr not in result: |
| 1621 | result.append(attr) |
| 1622 | return result |
| 1623 | |
| 1624 | def __getattr__(self, name: str) -> Any: |
| 1625 | if name in self._objects: |
| 1626 | return self._objects[name] |
| 1627 | if name in self._modules: |
| 1628 | value = self._get_module(name) |
| 1629 | elif name in self._class_to_module.keys(): |
| 1630 | module = self._get_module(self._class_to_module[name]) |
| 1631 | value = getattr(module, name) |
| 1632 | else: |
| 1633 | raise AttributeError(f"module {self.__name__} has no attribute {name}") |
| 1634 | |
| 1635 | setattr(self, name, value) |
| 1636 | return value |
| 1637 | |
| 1638 | def _get_module(self, module_name: str): |
| 1639 | try: |
| 1640 | return importlib.import_module("." + module_name, self.__name__) |
| 1641 | except Exception as e: |
| 1642 | raise RuntimeError( |
| 1643 | f"Failed to import {self.__name__}.{module_name} because of the following error (look up to see its" |
| 1644 | f" traceback):\n{e}" |
| 1645 | ) from e |
| 1646 | |
| 1647 | def __reduce__(self): |
| 1648 | return (self.__class__, (self._name, self.__file__, self._import_structure)) |
no outgoing calls
no test coverage detected