utils.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  2. # For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
  4. """This module contains utility functions for scoped nodes."""
  5. from __future__ import annotations
  6. from typing import TYPE_CHECKING
  7. from astroid.manager import AstroidManager
  8. if TYPE_CHECKING:
  9. from astroid import nodes
  10. def builtin_lookup(name: str) -> tuple[nodes.Module, list[nodes.NodeNG]]:
  11. """Lookup a name in the builtin module.
  12. Return the list of matching statements and the ast for the builtin module
  13. """
  14. manager = AstroidManager()
  15. try:
  16. _builtin_astroid = manager.builtins_module
  17. except KeyError:
  18. # User manipulated the astroid cache directly! Rebuild everything.
  19. manager.clear_cache()
  20. _builtin_astroid = manager.builtins_module
  21. if name == "__dict__":
  22. return _builtin_astroid, ()
  23. try:
  24. stmts: list[nodes.NodeNG] = _builtin_astroid.locals[name] # type: ignore[assignment]
  25. except KeyError:
  26. stmts = []
  27. return _builtin_astroid, stmts