Skip to content

Commit 33d0e84

Browse files
Delay astroid_bootstrapping() until instantiating AstroidBuilder (#2210)
1 parent 588aacc commit 33d0e84

File tree

5 files changed

+34
-18
lines changed

5 files changed

+34
-18
lines changed

ChangeLog

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ Release date: TBA
1717

1818
* Reduce file system access in ``ast_from_file()``.
1919

20+
* Reduce time to ``import astroid`` by delaying ``astroid_bootstrapping()`` until
21+
the first instantiation of ``AstroidBuilder``.
22+
23+
Closes #2161
24+
2025
* Make ``igetattr()`` idempotent. This addresses some reports of varying results
2126
when running pylint with ``--jobs``.
2227

@@ -42,7 +47,7 @@ Release date: TBA
4247
We have tried to minimize the amount of breaking changes caused by this change
4348
but some are unavoidable.
4449

45-
* ``infer_call_result`` now shares the same interface across all implemenations. Namely:
50+
* ``infer_call_result`` now shares the same interface across all implementations. Namely:
4651
```python
4752
def infer_call_result(
4853
self,

astroid/brain/brain_builtin_inference.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,14 @@ def _extend_builtins(class_transforms):
129129
transform(builtin_ast[class_name])
130130

131131

132-
_extend_builtins(
133-
{
134-
"bytes": partial(_extend_string_class, code=BYTES_CLASS, rvalue="b''"),
135-
"str": partial(_extend_string_class, code=STR_CLASS, rvalue="''"),
136-
}
137-
)
132+
def on_bootstrap():
133+
"""Called by astroid_bootstrapping()."""
134+
_extend_builtins(
135+
{
136+
"bytes": partial(_extend_string_class, code=BYTES_CLASS, rvalue="b''"),
137+
"str": partial(_extend_string_class, code=STR_CLASS, rvalue="''"),
138+
}
139+
)
138140

139141

140142
def _builtin_filter_predicate(node, builtin_name) -> bool:

astroid/brain/brain_nose.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
import re
88
import textwrap
99

10-
import astroid.builder
10+
from astroid.bases import BoundMethod
1111
from astroid.brain.helpers import register_module_extender
12+
from astroid.builder import AstroidBuilder
1213
from astroid.exceptions import InferenceError
1314
from astroid.manager import AstroidManager
14-
15-
_BUILDER = astroid.builder.AstroidBuilder(AstroidManager())
16-
15+
from astroid.nodes import List, Module
1716

1817
CAPITALS = re.compile("([A-Z])")
1918

@@ -24,7 +23,7 @@ def _pep8(name, caps=CAPITALS):
2423

2524
def _nose_tools_functions():
2625
"""Get an iterator of names and bound methods."""
27-
module = _BUILDER.string_build(
26+
module = AstroidBuilder().string_build(
2827
textwrap.dedent(
2928
"""
3029
import unittest
@@ -42,10 +41,10 @@ class Test(unittest.TestCase):
4241
for method in case.methods():
4342
if method.name.startswith("assert") and "_" not in method.name:
4443
pep8_name = _pep8(method.name)
45-
yield pep8_name, astroid.BoundMethod(method, case)
44+
yield pep8_name, BoundMethod(method, case)
4645
if method.name == "assertEqual":
4746
# nose also exports assert_equals.
48-
yield "assert_equals", astroid.BoundMethod(method, case)
47+
yield "assert_equals", BoundMethod(method, case)
4948

5049

5150
def _nose_tools_transform(node):
@@ -55,7 +54,7 @@ def _nose_tools_transform(node):
5554

5655
def _nose_tools_trivial_transform():
5756
"""Custom transform for the nose.tools module."""
58-
stub = _BUILDER.string_build("""__all__ = []""")
57+
stub = AstroidBuilder().string_build("""__all__ = []""")
5958
all_entries = ["ok_", "eq_"]
6059

6160
for pep8_name, method in _nose_tools_functions():
@@ -65,7 +64,7 @@ def _nose_tools_trivial_transform():
6564
# Update the __all__ variable, since nose.tools
6665
# does this manually with .append.
6766
all_assign = stub["__all__"].parent
68-
all_object = astroid.List(all_entries)
67+
all_object = List(all_entries)
6968
all_object.parent = all_assign
7069
all_assign.value = all_object
7170
return stub
@@ -75,5 +74,5 @@ def _nose_tools_trivial_transform():
7574
AstroidManager(), "nose.tools.trivial", _nose_tools_trivial_transform
7675
)
7776
AstroidManager().register_transform(
78-
astroid.Module, _nose_tools_transform, lambda n: n.name == "nose.tools"
77+
Module, _nose_tools_transform, lambda n: n.name == "nose.tools"
7978
)

astroid/builder.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ def __init__(
6969
) -> None:
7070
super().__init__(manager)
7171
self._apply_transforms = apply_transforms
72+
if not raw_building.InspectBuilder.bootstrapped:
73+
raw_building._astroid_bootstrapping()
7274

7375
def module_build(
7476
self, module: types.ModuleType, modname: str | None = None

astroid/raw_building.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ class InspectBuilder:
427427
FunctionDef and ClassDef nodes and some others as guessed.
428428
"""
429429

430+
bootstrapped: bool = False
431+
430432
def __init__(self, manager_instance: AstroidManager | None = None) -> None:
431433
self._manager = manager_instance or AstroidManager()
432434
self._done: dict[types.ModuleType | type, nodes.Module | nodes.ClassDef] = {}
@@ -725,5 +727,11 @@ def _astroid_bootstrapping() -> None:
725727
builder.object_build(klass, _type)
726728
astroid_builtin[_type.__name__] = klass
727729

730+
InspectBuilder.bootstrapped = True
731+
732+
# pylint: disable-next=import-outside-toplevel
733+
from astroid.brain.brain_builtin_inference import on_bootstrap
728734

729-
_astroid_bootstrapping()
735+
# Instantiates an AstroidBuilder(), which is where
736+
# InspectBuilder.bootstrapped is checked, so place after bootstrapped=True.
737+
on_bootstrap()

0 commit comments

Comments
 (0)