Skip to content

Commit 6abb955

Browse files
DanielNoordcdce8p
andauthored
Move builtin_lookup out of main scoped_nodes file (#1460)
Co-authored-by: Marc Mueller <[email protected]>
1 parent d30592a commit 6abb955

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

astroid/nodes/scoped_nodes/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
Module,
2020
SetComp,
2121
_is_metaclass,
22-
builtin_lookup,
2322
function_to_method,
2423
get_wrapping_class,
2524
)
25+
from astroid.nodes.scoped_nodes.utils import builtin_lookup
2626

2727
__all__ = (
2828
"AsyncFunctionDef",

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
new local scope in the language definition : Module, ClassDef, FunctionDef (and
4646
Lambda, GeneratorExp, DictComp and SetComp to some extent).
4747
"""
48-
import builtins
4948
import io
5049
import itertools
5150
import os
@@ -80,6 +79,7 @@
8079
from astroid.interpreter.objectmodel import ClassModel, FunctionModel, ModuleModel
8180
from astroid.manager import AstroidManager
8281
from astroid.nodes import Arguments, Const, node_classes
82+
from astroid.nodes.scoped_nodes.utils import builtin_lookup
8383
from astroid.nodes.utils import Position
8484

8585
if sys.version_info >= (3, 6, 2):
@@ -215,21 +215,6 @@ def function_to_method(n, klass):
215215
return n
216216

217217

218-
def builtin_lookup(name):
219-
"""lookup a name into the builtin module
220-
return the list of matching statements and the astroid for the builtin
221-
module
222-
"""
223-
builtin_astroid = AstroidManager().ast_from_module(builtins)
224-
if name == "__dict__":
225-
return builtin_astroid, ()
226-
try:
227-
stmts = builtin_astroid.locals[name]
228-
except KeyError:
229-
stmts = ()
230-
return builtin_astroid, stmts
231-
232-
233218
# TODO move this Mixin to mixins.py; problem: 'FunctionDef' in _scope_lookup
234219
class LocalsDictNodeNG(node_classes.LookupMixIn, node_classes.NodeNG):
235220
"""this class provides locals handling common to Module, FunctionDef

astroid/nodes/scoped_nodes/utils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
4+
"""
5+
This module contains utility functions for scoped nodes.
6+
"""
7+
8+
import builtins
9+
from typing import TYPE_CHECKING, Sequence, Tuple
10+
11+
from astroid.manager import AstroidManager
12+
13+
if TYPE_CHECKING:
14+
from astroid import nodes
15+
16+
17+
_builtin_astroid: "nodes.Module | None" = None
18+
19+
20+
def builtin_lookup(name: str) -> Tuple["nodes.Module", Sequence["nodes.NodeNG"]]:
21+
"""Lookup a name in the builtin module.
22+
23+
Return the list of matching statements and the ast for the builtin module
24+
"""
25+
# pylint: disable-next=global-statement
26+
global _builtin_astroid
27+
if _builtin_astroid is None:
28+
_builtin_astroid = AstroidManager().ast_from_module(builtins)
29+
if name == "__dict__":
30+
return _builtin_astroid, ()
31+
try:
32+
stmts: Sequence["nodes.NodeNG"] = _builtin_astroid.locals[name]
33+
except KeyError:
34+
stmts = ()
35+
return _builtin_astroid, stmts

0 commit comments

Comments
 (0)