Skip to content

Commit c21b18c

Browse files
authored
No longer pass doc to various node constructors (#1451)
1 parent b0c0c45 commit c21b18c

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

astroid/builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def delayed_assattr(self, node):
280280

281281

282282
def build_namespace_package_module(name: str, path: List[str]) -> nodes.Module:
283-
return nodes.Module(name, doc="", path=path, package=True)
283+
return nodes.Module(name, path=path, package=True)
284284

285285

286286
def parse(code, module_name="", path=None, apply_transforms=True):

astroid/raw_building.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ def attach_import_node(node, modname, membername):
110110

111111
def build_module(name: str, doc: Optional[str] = None) -> nodes.Module:
112112
"""create and initialize an astroid Module node"""
113-
node = nodes.Module(name, doc, pure_python=False)
114-
node.package = False
115-
node.parent = None
113+
node = nodes.Module(name, pure_python=False, package=False)
114+
node.postinit(
115+
body=[],
116+
doc_node=nodes.Const(value=doc) if doc else None,
117+
)
116118
return node
117119

118120

@@ -135,13 +137,13 @@ def build_function(
135137
args: Optional[List[str]] = None,
136138
posonlyargs: Optional[List[str]] = None,
137139
defaults=None,
138-
doc=None,
140+
doc: Optional[str] = None,
139141
kwonlyargs: Optional[List[str]] = None,
140142
) -> nodes.FunctionDef:
141143
"""create and initialize an astroid FunctionDef node"""
142144
# first argument is now a list of decorators
143-
func = nodes.FunctionDef(name, doc)
144-
func.args = argsnode = nodes.Arguments(parent=func)
145+
func = nodes.FunctionDef(name)
146+
argsnode = nodes.Arguments(parent=func)
145147
argsnode.postinit(
146148
args=[nodes.AssignName(name=arg, parent=argsnode) for arg in args or ()],
147149
defaults=[],
@@ -154,6 +156,11 @@ def build_function(
154156
nodes.AssignName(name=arg, parent=argsnode) for arg in posonlyargs or ()
155157
],
156158
)
159+
func.postinit(
160+
args=argsnode,
161+
body=[],
162+
doc_node=nodes.Const(value=doc) if doc else None,
163+
)
157164
for default in defaults or ():
158165
argsnode.defaults.append(nodes.const_factory(default))
159166
argsnode.defaults[-1].parent = argsnode

astroid/rebuilder.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,8 @@ def __init__(
116116
self._parser_module = parser_module
117117
self._module = self._parser_module.module
118118

119-
def _get_doc(
120-
self, node: T_Doc
121-
) -> Tuple[T_Doc, Optional["ast.Constant | ast.Str"], Optional[str]]:
122-
"""Return the doc ast node and the actual docstring."""
119+
def _get_doc(self, node: T_Doc) -> Tuple[T_Doc, Optional["ast.Constant | ast.Str"]]:
120+
"""Return the doc ast node."""
123121
try:
124122
if node.body and isinstance(node.body[0], self._module.Expr):
125123
first_value = node.body[0].value
@@ -129,16 +127,15 @@ def _get_doc(
129127
and isinstance(first_value.value, str)
130128
):
131129
doc_ast_node = first_value
132-
doc = first_value.value if PY38_PLUS else first_value.s
133130
node.body = node.body[1:]
134131
# The ast parser of python < 3.8 sets col_offset of multi-line strings to -1
135132
# as it is unable to determine the value correctly. We reset this to None.
136133
if doc_ast_node.col_offset == -1:
137134
doc_ast_node.col_offset = None
138-
return node, doc_ast_node, doc
135+
return node, doc_ast_node
139136
except IndexError:
140137
pass # ast built from scratch
141-
return node, None, None
138+
return node, None
142139

143140
def _get_context(
144141
self,
@@ -298,10 +295,9 @@ def visit_module(
298295
299296
Note: Method not called by 'visit'
300297
"""
301-
node, doc_ast_node, doc = self._get_doc(node)
298+
node, doc_ast_node = self._get_doc(node)
302299
newnode = nodes.Module(
303300
name=modname,
304-
doc=doc,
305301
file=modpath,
306302
path=[modpath],
307303
package=package,
@@ -1296,10 +1292,9 @@ def visit_classdef(
12961292
self, node: "ast.ClassDef", parent: NodeNG, newstyle: bool = True
12971293
) -> nodes.ClassDef:
12981294
"""visit a ClassDef node to become astroid"""
1299-
node, doc_ast_node, doc = self._get_doc(node)
1295+
node, doc_ast_node = self._get_doc(node)
13001296
newnode = nodes.ClassDef(
13011297
name=node.name,
1302-
doc=doc,
13031298
lineno=node.lineno,
13041299
col_offset=node.col_offset,
13051300
# end_lineno and end_col_offset added in 3.8
@@ -1604,7 +1599,7 @@ def _visit_functiondef(
16041599
) -> T_Function:
16051600
"""visit an FunctionDef node to become astroid"""
16061601
self._global_names.append({})
1607-
node, doc_ast_node, doc = self._get_doc(node)
1602+
node, doc_ast_node = self._get_doc(node)
16081603

16091604
lineno = node.lineno
16101605
if PY38_PLUS and node.decorator_list:
@@ -1619,7 +1614,6 @@ def _visit_functiondef(
16191614

16201615
newnode = cls(
16211616
name=node.name,
1622-
doc=doc,
16231617
lineno=lineno,
16241618
col_offset=node.col_offset,
16251619
# end_lineno and end_col_offset added in 3.8

doc/extending.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ Instantiating a new node might look as in::
9595

9696
new_node = FunctionDef(
9797
name='my_new_function',
98-
doc='the docstring of this function',
9998
lineno=3,
10099
col_offset=0,
101100
parent=the_parent_of_this_function,
@@ -104,6 +103,7 @@ Instantiating a new node might look as in::
104103
args=args,
105104
body=body,
106105
returns=returns,
106+
doc_node=nodes.Const(value='the docstring of this function'),
107107
)
108108

109109

0 commit comments

Comments
 (0)