helpers.py 908 B

123456789101112131415161718192021222324
  1. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  2. # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
  3. # Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
  4. from __future__ import annotations
  5. from collections.abc import Callable
  6. from astroid.manager import AstroidManager
  7. from astroid.nodes.scoped_nodes import Module
  8. def register_module_extender(
  9. manager: AstroidManager, module_name: str, get_extension_mod: Callable[[], Module]
  10. ) -> None:
  11. def transform(node: Module) -> None:
  12. extension_module = get_extension_mod()
  13. for name, objs in extension_module.locals.items():
  14. node.locals[name] = objs
  15. for obj in objs:
  16. if obj.parent is extension_module:
  17. obj.parent = node
  18. manager.register_transform(Module, transform, lambda n: n.name == module_name)