Skip to content

Commit 0acb961

Browse files
thejcannoncdce8p
andauthored
Refactor: Stop adding arbitrary attributes to module obj when building (#1215)
* Move code form _post_build into _data_build * Pass builder to _post_build Co-authored-by: Marc Mueller <[email protected]>
1 parent 62aa3bb commit 0acb961

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

astroid/builder.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import textwrap
2929
import types
3030
from tokenize import detect_encoding
31-
from typing import List, Optional, Union
31+
from typing import List, Optional, Tuple, Union
3232

3333
from astroid import bases, modutils, nodes, raw_building, rebuilder, util
3434
from astroid._ast import get_parser_module
@@ -147,36 +147,40 @@ def file_build(self, path, modname=None):
147147
except ImportError:
148148
modname = os.path.splitext(os.path.basename(path))[0]
149149
# build astroid representation
150-
module = self._data_build(data, modname, path)
151-
return self._post_build(module, encoding)
150+
module, builder = self._data_build(data, modname, path)
151+
return self._post_build(module, builder, encoding)
152152

153153
def string_build(self, data, modname="", path=None):
154154
"""Build astroid from source code string."""
155-
module = self._data_build(data, modname, path)
155+
module, builder = self._data_build(data, modname, path)
156156
module.file_bytes = data.encode("utf-8")
157-
return self._post_build(module, "utf-8")
157+
return self._post_build(module, builder, "utf-8")
158158

159-
def _post_build(self, module, encoding):
159+
def _post_build(
160+
self, module: nodes.Module, builder: rebuilder.TreeRebuilder, encoding: str
161+
) -> nodes.Module:
160162
"""Handles encoding and delayed nodes after a module has been built"""
161163
module.file_encoding = encoding
162164
self._manager.cache_module(module)
163165
# post tree building steps after we stored the module in the cache:
164-
for from_node in module._import_from_nodes:
166+
for from_node in builder._import_from_nodes:
165167
if from_node.modname == "__future__":
166168
for symbol, _ in from_node.names:
167169
module.future_imports.add(symbol)
168170
self.add_from_names_to_locals(from_node)
169171
# handle delayed assattr nodes
170-
for delayed in module._delayed_assattr:
172+
for delayed in builder._delayed_assattr:
171173
self.delayed_assattr(delayed)
172174

173175
# Visit the transforms
174176
if self._apply_transforms:
175177
module = self._manager.visit_transforms(module)
176178
return module
177179

178-
def _data_build(self, data: str, modname, path):
179-
"""Build tree node from data and add some information"""
180+
def _data_build(
181+
self, data: str, modname, path
182+
) -> Tuple[nodes.Module, rebuilder.TreeRebuilder]:
183+
"""Build tree node from data and add some informations"""
180184
try:
181185
node, parser_module = _parse_string(data, type_comments=True)
182186
except (TypeError, ValueError, SyntaxError) as exc:
@@ -202,9 +206,7 @@ def _data_build(self, data: str, modname, path):
202206
)
203207
builder = rebuilder.TreeRebuilder(self._manager, parser_module, data)
204208
module = builder.visit_module(node, modname, node_file, package)
205-
module._import_from_nodes = builder._import_from_nodes
206-
module._delayed_assattr = builder._delayed_assattr
207-
return module
209+
return module, builder
208210

209211
def add_from_names_to_locals(self, node):
210212
"""Store imported names to the locals

0 commit comments

Comments
 (0)