utils.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  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. """This module contains utility functions for scoped nodes."""
  5. from __future__ import annotations
  6. from collections.abc import Sequence
  7. from typing import TYPE_CHECKING
  8. from astroid.manager import AstroidManager
  9. if TYPE_CHECKING:
  10. from astroid import nodes
  11. def builtin_lookup(name: str) -> tuple[nodes.Module, Sequence[nodes.NodeNG]]:
  12. """Lookup a name in the builtin module.
  13. Return the list of matching statements and the ast for the builtin module
  14. """
  15. manager = AstroidManager()
  16. try:
  17. _builtin_astroid = manager.builtins_module
  18. except KeyError:
  19. # User manipulated the astroid cache directly! Rebuild everything.
  20. manager.clear_cache()
  21. _builtin_astroid = manager.builtins_module
  22. if name == "__dict__":
  23. return _builtin_astroid, ()
  24. try:
  25. stmts: Sequence[nodes.NodeNG] = _builtin_astroid.locals[name] # type: ignore[assignment]
  26. except KeyError:
  27. stmts = ()
  28. return _builtin_astroid, stmts