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