Skip to content

Commit abcbe35

Browse files
Replace the constant BUILTINS by the string 'builtins'
This make for clearer and also sligtly faster code (means time seems to decrese by 0.68% with this change alone (astroid/pylint) in the pylint tests benchmarks). Done because we were using an import from astroid from astroid.bases for one of those, which is kinda messy.
1 parent 7603614 commit abcbe35

16 files changed

+81
-90
lines changed

ChangeLog

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ What's New in astroid 2.8.0?
66
============================
77
Release date: TBA
88

9-
9+
* ``astroid.const.BUILTINS`` and ``astroid.bases.BUILTINS`` have been removed,
10+
simply replace this by the string 'builtins' for better performances.
1011

1112
What's New in astroid 2.7.2?
1213
============================

astroid/bases.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
from astroid import context as contextmod
3131
from astroid import decorators, util
32-
from astroid.const import BUILTINS, PY310_PLUS
32+
from astroid.const import PY310_PLUS
3333
from astroid.exceptions import (
3434
AstroidTypeError,
3535
AttributeInferenceError,
@@ -45,7 +45,7 @@
4545
# TODO: check if needs special treatment
4646
BOOL_SPECIAL_METHOD = "__bool__"
4747

48-
PROPERTIES = {BUILTINS + ".property", "abc.abstractproperty"}
48+
PROPERTIES = {"builtins.property", "abc.abstractproperty"}
4949
if PY310_PLUS:
5050
PROPERTIES.add("enum.property")
5151

@@ -94,7 +94,7 @@ def _is_property(meth, context=None):
9494
if base_class.__class__.__name__ != "Name":
9595
continue
9696
module, _ = base_class.lookup(base_class.name)
97-
if module.name == BUILTINS and base_class.name == "property":
97+
if module.name == "builtins" and base_class.name == "property":
9898
return True
9999

100100
return False
@@ -394,7 +394,7 @@ def infer_call_result(self, caller, context):
394394
# instance of the class given as first argument.
395395
if (
396396
self._proxied.name == "__new__"
397-
and self._proxied.parent.frame().qname() == "%s.object" % BUILTINS
397+
and self._proxied.parent.frame().qname() == "builtins.object"
398398
):
399399
if caller.args:
400400
node_context = context.extra_context.get(caller.args[0])
@@ -445,7 +445,7 @@ def _infer_type_new_call(self, caller, context):
445445
if mcs.__class__.__name__ != "ClassDef":
446446
# Not a valid first argument.
447447
return None
448-
if not mcs.is_subtype_of("%s.type" % BUILTINS):
448+
if not mcs.is_subtype_of("builtins.type"):
449449
# Not a valid metaclass.
450450
return None
451451

@@ -558,7 +558,7 @@ def callable(self):
558558
return False
559559

560560
def pytype(self):
561-
return "%s.generator" % BUILTINS
561+
return "builtins.generator"
562562

563563
def display_type(self):
564564
return "Generator"
@@ -579,7 +579,7 @@ class AsyncGenerator(Generator):
579579
"""Special node representing an async generator"""
580580

581581
def pytype(self):
582-
return "%s.async_generator" % BUILTINS
582+
return "builtins.async_generator"
583583

584584
def display_type(self):
585585
return "AsyncGenerator"

astroid/const.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import builtins
21
import enum
32
import sys
43

@@ -18,5 +17,3 @@ class Context(enum.Enum):
1817
Load = Context.Load
1918
Store = Context.Store
2019
Del = Context.Del
21-
22-
BUILTINS = builtins.__name__ # Could be just 'builtins' ?

astroid/helpers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from astroid import bases
2222
from astroid import context as contextmod
2323
from astroid import manager, nodes, raw_building, util
24-
from astroid.const import BUILTINS
2524
from astroid.exceptions import (
2625
AstroidTypeError,
2726
AttributeInferenceError,
@@ -40,7 +39,7 @@ def _build_proxy_class(cls_name, builtins):
4039

4140
def _function_type(function, builtins):
4241
if isinstance(function, scoped_nodes.Lambda):
43-
if function.root().name == BUILTINS:
42+
if function.root().name == "builtins":
4443
cls_name = "builtin_function_or_method"
4544
else:
4645
cls_name = "function"

astroid/nodes/node_classes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from astroid import bases
4646
from astroid import context as contextmod
4747
from astroid import decorators, mixins, util
48-
from astroid.const import BUILTINS, Context
48+
from astroid.const import Context
4949
from astroid.exceptions import (
5050
AstroidIndexError,
5151
AstroidTypeError,
@@ -2191,7 +2191,7 @@ def pytype(self):
21912191
:returns: The name of the type.
21922192
:rtype: str
21932193
"""
2194-
return "%s.dict" % BUILTINS
2194+
return "builtins.dict"
21952195

21962196
def get_children(self):
21972197
"""Get the key and value nodes below this node.
@@ -3083,7 +3083,7 @@ def pytype(self):
30833083
:returns: The name of the type.
30843084
:rtype: str
30853085
"""
3086-
return "%s.list" % BUILTINS
3086+
return "builtins.list"
30873087

30883088
def getitem(self, index, context=None):
30893089
"""Get an item from this node.
@@ -3278,7 +3278,7 @@ def pytype(self):
32783278
:returns: The name of the type.
32793279
:rtype: str
32803280
"""
3281-
return "%s.set" % BUILTINS
3281+
return "builtins.set"
32823282

32833283

32843284
class Slice(NodeNG):
@@ -3356,7 +3356,7 @@ def pytype(self):
33563356
:returns: The name of the type.
33573357
:rtype: str
33583358
"""
3359-
return "%s.slice" % BUILTINS
3359+
return "builtins.slice"
33603360

33613361
def igetattr(self, attrname, context=None):
33623362
"""Infer the possible values of the given attribute on the slice.
@@ -3710,7 +3710,7 @@ def pytype(self):
37103710
:returns: The name of the type.
37113711
:rtype: str
37123712
"""
3713-
return "%s.tuple" % BUILTINS
3713+
return "builtins.tuple"
37143714

37153715
def getitem(self, index, context=None):
37163716
"""Get an item from this node.

astroid/nodes/scoped_nodes.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
from astroid import context as contextmod
5050
from astroid import decorators as decorators_mod
5151
from astroid import mixins, util
52-
from astroid.const import BUILTINS, PY39_PLUS
52+
from astroid.const import PY39_PLUS
5353
from astroid.exceptions import (
5454
AstroidBuildingError,
5555
AstroidTypeError,
@@ -575,7 +575,7 @@ def pytype(self):
575575
:returns: The name of the type.
576576
:rtype: str
577577
"""
578-
return "%s.module" % BUILTINS
578+
return "builtins.module"
579579

580580
def display_type(self):
581581
"""A human readable type of this node.
@@ -1137,9 +1137,9 @@ def _infer_decorator_callchain(node):
11371137
if isinstance(result, bases.Instance):
11381138
result = result._proxied
11391139
if isinstance(result, ClassDef):
1140-
if result.is_subtype_of("%s.classmethod" % BUILTINS):
1140+
if result.is_subtype_of("builtins.classmethod"):
11411141
return "classmethod"
1142-
if result.is_subtype_of("%s.staticmethod" % BUILTINS):
1142+
if result.is_subtype_of("builtins.staticmethod"):
11431143
return "staticmethod"
11441144
if isinstance(result, FunctionDef):
11451145
if not result.decorators:
@@ -1152,7 +1152,7 @@ def _infer_decorator_callchain(node):
11521152
if (
11531153
isinstance(decorator, node_classes.Attribute)
11541154
and isinstance(decorator.expr, node_classes.Name)
1155-
and decorator.expr.name == BUILTINS
1155+
and decorator.expr.name == "builtins"
11561156
and decorator.attrname in BUILTIN_DESCRIPTORS
11571157
):
11581158
return decorator.attrname
@@ -1241,8 +1241,8 @@ def pytype(self):
12411241
:rtype: str
12421242
"""
12431243
if "method" in self.type:
1244-
return "%s.instancemethod" % BUILTINS
1245-
return "%s.function" % BUILTINS
1244+
return "builtins.instancemethod"
1245+
return "builtins.function"
12461246

12471247
def display_type(self):
12481248
"""A human readable type of this node.
@@ -1541,7 +1541,7 @@ def type(
15411541
if (
15421542
isinstance(node, node_classes.Attribute)
15431543
and isinstance(node.expr, node_classes.Name)
1544-
and node.expr.name == BUILTINS
1544+
and node.expr.name == "builtins"
15451545
and node.attrname in BUILTIN_DESCRIPTORS
15461546
):
15471547
return node.attrname
@@ -1571,9 +1571,9 @@ def type(
15711571
for ancestor in inferred.ancestors():
15721572
if not isinstance(ancestor, ClassDef):
15731573
continue
1574-
if ancestor.is_subtype_of("%s.classmethod" % BUILTINS):
1574+
if ancestor.is_subtype_of("builtins.classmethod"):
15751575
return "classmethod"
1576-
if ancestor.is_subtype_of("%s.staticmethod" % BUILTINS):
1576+
if ancestor.is_subtype_of("builtins.staticmethod"):
15771577
return "staticmethod"
15781578
except InferenceError:
15791579
pass
@@ -2162,8 +2162,8 @@ def pytype(self):
21622162
:rtype: str
21632163
"""
21642164
if self.newstyle:
2165-
return "%s.type" % BUILTINS
2166-
return "%s.classobj" % BUILTINS
2165+
return "builtins.type"
2166+
return "builtins.classobj"
21672167

21682168
def display_type(self):
21692169
"""A human readable type of this node.
@@ -2250,7 +2250,7 @@ def _infer_type_call(self, caller, context):
22502250

22512251
def infer_call_result(self, caller, context=None):
22522252
"""infer what a class is returning when called"""
2253-
if self.is_subtype_of(f"{BUILTINS}.type", context) and len(caller.args) == 3:
2253+
if self.is_subtype_of("builtins.type", context) and len(caller.args) == 3:
22542254
result = self._infer_type_call(caller, context)
22552255
yield result
22562256
return
@@ -2666,7 +2666,7 @@ def has_dynamic_getattr(self, context=None):
26662666

26672667
def _valid_getattr(node):
26682668
root = node.root()
2669-
return root.name != BUILTINS and getattr(root, "pure_python", None)
2669+
return root.name != "builtins" and getattr(root, "pure_python", None)
26702670

26712671
try:
26722672
return _valid_getattr(self.getattr("__getattr__", context)[0])

astroid/objects.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424

2525
from astroid import bases, decorators, util
26-
from astroid.const import BUILTINS
2726
from astroid.exceptions import (
2827
AttributeInferenceError,
2928
InferenceError,
@@ -40,7 +39,7 @@ class FrozenSet(node_classes.BaseContainer):
4039
"""class representing a FrozenSet composite node"""
4140

4241
def pytype(self):
43-
return "%s.frozenset" % BUILTINS
42+
return "builtins.frozenset"
4443

4544
def _infer(self, context=None):
4645
yield self
@@ -120,7 +119,7 @@ def _proxied(self):
120119
return ast_builtins.getattr("super")[0]
121120

122121
def pytype(self):
123-
return "%s.super" % BUILTINS
122+
return "builtins.super"
124123

125124
def display_type(self):
126125
return "Super of"
@@ -307,7 +306,7 @@ def __init__(
307306
type = "property"
308307

309308
def pytype(self):
310-
return "%s.property" % BUILTINS
309+
return "builtins.property"
311310

312311
def infer_call_result(self, caller=None, context=None):
313312
raise InferenceError("Properties are not callable")

tests/resources.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import sys
1616

1717
from astroid import builder
18-
from astroid.bases import BUILTINS
1918
from astroid.manager import AstroidManager
2019

2120
DATA_DIR = os.path.join("testdata", "python3")
@@ -58,9 +57,9 @@ class AstroidCacheSetupMixin:
5857

5958
@classmethod
6059
def setup_class(cls):
61-
cls._builtins = AstroidManager().astroid_cache.get(BUILTINS)
60+
cls._builtins = AstroidManager().astroid_cache.get("builtins")
6261

6362
@classmethod
6463
def teardown_class(cls):
6564
if cls._builtins:
66-
AstroidManager().astroid_cache[BUILTINS] = cls._builtins
65+
AstroidManager().astroid_cache["builtins"] = cls._builtins

tests/unittest_brain.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ def test_multiprocessing_manager(self):
695695

696696
for attr in ("list", "dict"):
697697
obj = next(module[attr].infer())
698-
self.assertEqual(obj.qname(), f"{bases.BUILTINS}.{attr}")
698+
self.assertEqual(obj.qname(), f"builtins.{attr}")
699699

700700
# pypy's implementation of array.__spec__ return None. This causes problems for this inference.
701701
if not hasattr(sys, "pypy_version_info"):
@@ -771,10 +771,9 @@ def mymethod(self, x):
771771
one = enumeration["one"]
772772
self.assertEqual(one.pytype(), ".MyEnum.one")
773773

774-
property_type = f"{bases.BUILTINS}.property"
775774
for propname in ("name", "value"):
776775
prop = next(iter(one.getattr(propname)))
777-
self.assertIn(property_type, prop.decoratornames())
776+
self.assertIn("builtins.property", prop.decoratornames())
778777

779778
meth = one.getattr("mymethod")[0]
780779
self.assertIsInstance(meth, astroid.FunctionDef)
@@ -861,9 +860,8 @@ class MyEnum(enum.IntEnum):
861860
one = enumeration["one"]
862861

863862
clazz = one.getattr("__class__")[0]
864-
int_type = f"{bases.BUILTINS}.int"
865863
self.assertTrue(
866-
clazz.is_subtype_of(int_type),
864+
clazz.is_subtype_of("builtins.int"),
867865
"IntEnum based enums should be a subtype of int",
868866
)
869867

tests/unittest_builder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import pytest
3232

3333
from astroid import Instance, builder, nodes, test_utils, util
34-
from astroid.const import BUILTINS, PY38_PLUS
34+
from astroid.const import PY38_PLUS
3535
from astroid.exceptions import (
3636
AstroidBuildingError,
3737
AstroidSyntaxError,
@@ -287,7 +287,7 @@ def test_missing_file(self):
287287

288288
def test_inspect_build0(self):
289289
"""test astroid tree build from a living object"""
290-
builtin_ast = self.manager.ast_from_module_name(BUILTINS)
290+
builtin_ast = self.manager.ast_from_module_name("builtins")
291291
# just check type and object are there
292292
builtin_ast.getattr("type")
293293
objectastroid = builtin_ast.getattr("object")[0]
@@ -314,7 +314,7 @@ def test_inspect_build3(self):
314314
self.builder.inspect_build(unittest)
315315

316316
def test_inspect_build_type_object(self):
317-
builtin_ast = self.manager.ast_from_module_name(BUILTINS)
317+
builtin_ast = self.manager.ast_from_module_name("builtins")
318318

319319
inferred = list(builtin_ast.igetattr("object"))
320320
self.assertEqual(len(inferred), 1)

0 commit comments

Comments
 (0)