|
18 | 18 |
|
19 | 19 | from astroid import nodes
|
20 | 20 | from astroid._ast import ParserModule, get_parser_module, parse_function_type_comment
|
21 |
| -from astroid.const import IS_PYPY, PY38, PY38_PLUS, PY39_PLUS, Context |
| 21 | +from astroid.const import IS_PYPY, PY38, PY39_PLUS, Context |
22 | 22 | from astroid.manager import AstroidManager
|
23 | 23 | from astroid.nodes import NodeNG
|
24 | 24 | from astroid.nodes.utils import Position
|
@@ -74,10 +74,8 @@ def _get_doc(self, node: T_Doc) -> tuple[T_Doc, ast.Constant | ast.Str | None]:
|
74 | 74 | try:
|
75 | 75 | if node.body and isinstance(node.body[0], ast.Expr):
|
76 | 76 | first_value = node.body[0].value
|
77 |
| - if isinstance(first_value, ast.Str) or ( |
78 |
| - PY38_PLUS |
79 |
| - and isinstance(first_value, ast.Constant) |
80 |
| - and isinstance(first_value.value, str) |
| 77 | + if isinstance(first_value, ast.Constant) and isinstance( |
| 78 | + first_value.value, str |
81 | 79 | ):
|
82 | 80 | doc_ast_node = first_value
|
83 | 81 | node.body = node.body[1:]
|
@@ -158,56 +156,6 @@ def _get_position_info(
|
158 | 156 | end_col_offset=t.end[1],
|
159 | 157 | )
|
160 | 158 |
|
161 |
| - def _fix_doc_node_position(self, node: NodesWithDocsType) -> None: |
162 |
| - """Fix start and end position of doc nodes for Python < 3.8.""" |
163 |
| - if not self._data or not node.doc_node or node.lineno is None: |
164 |
| - return |
165 |
| - if PY38_PLUS: |
166 |
| - return |
167 |
| - |
168 |
| - lineno = node.lineno or 1 # lineno of modules is 0 |
169 |
| - end_range: int | None = node.doc_node.lineno |
170 |
| - if IS_PYPY and not PY39_PLUS: |
171 |
| - end_range = None |
172 |
| - # pylint: disable-next=unsubscriptable-object |
173 |
| - data = "\n".join(self._data[lineno - 1 : end_range]) |
174 |
| - |
175 |
| - found_start, found_end = False, False |
176 |
| - open_brackets = 0 |
177 |
| - skip_token: set[int] = {token.NEWLINE, token.INDENT, token.NL, token.COMMENT} |
178 |
| - |
179 |
| - if isinstance(node, nodes.Module): |
180 |
| - found_end = True |
181 |
| - |
182 |
| - for t in generate_tokens(StringIO(data).readline): |
183 |
| - if found_end is False: |
184 |
| - if ( |
185 |
| - found_start is False |
186 |
| - and t.type == token.NAME |
187 |
| - and t.string in {"def", "class"} |
188 |
| - ): |
189 |
| - found_start = True |
190 |
| - elif found_start is True and t.type == token.OP: |
191 |
| - if t.exact_type == token.COLON and open_brackets == 0: |
192 |
| - found_end = True |
193 |
| - elif t.exact_type == token.LPAR: |
194 |
| - open_brackets += 1 |
195 |
| - elif t.exact_type == token.RPAR: |
196 |
| - open_brackets -= 1 |
197 |
| - continue |
198 |
| - if t.type in skip_token: |
199 |
| - continue |
200 |
| - if t.type == token.STRING: |
201 |
| - break |
202 |
| - return |
203 |
| - else: |
204 |
| - return |
205 |
| - |
206 |
| - node.doc_node.lineno = lineno + t.start[0] - 1 |
207 |
| - node.doc_node.col_offset = t.start[1] |
208 |
| - node.doc_node.end_lineno = lineno + t.end[0] - 1 |
209 |
| - node.doc_node.end_col_offset = t.end[1] |
210 |
| - |
211 | 159 | def _reset_end_lineno(self, newnode: nodes.NodeNG) -> None:
|
212 | 160 | """Reset end_lineno and end_col_offset attributes for PyPy 3.8.
|
213 | 161 |
|
@@ -246,7 +194,6 @@ def visit_module(
|
246 | 194 | [self.visit(child, newnode) for child in node.body],
|
247 | 195 | doc_node=self.visit(doc_ast_node, newnode),
|
248 | 196 | )
|
249 |
| - self._fix_doc_node_position(newnode) |
250 | 197 | if IS_PYPY and PY38:
|
251 | 198 | self._reset_end_lineno(newnode)
|
252 | 199 | return newnode
|
@@ -953,7 +900,6 @@ def visit_classdef(
|
953 | 900 | position=self._get_position_info(node, newnode),
|
954 | 901 | doc_node=self.visit(doc_ast_node, newnode),
|
955 | 902 | )
|
956 |
| - self._fix_doc_node_position(newnode) |
957 | 903 | return newnode
|
958 | 904 |
|
959 | 905 | def visit_continue(self, node: ast.Continue, parent: NodeNG) -> nodes.Continue:
|
@@ -1225,7 +1171,7 @@ def _visit_functiondef(
|
1225 | 1171 | node, doc_ast_node = self._get_doc(node)
|
1226 | 1172 |
|
1227 | 1173 | lineno = node.lineno
|
1228 |
| - if PY38_PLUS and node.decorator_list: |
| 1174 | + if node.decorator_list: |
1229 | 1175 | # Python 3.8 sets the line number of a decorated function
|
1230 | 1176 | # to be the actual line number of the function, but the
|
1231 | 1177 | # previous versions expected the decorator's line number instead.
|
@@ -1265,7 +1211,6 @@ def _visit_functiondef(
|
1265 | 1211 | position=self._get_position_info(node, newnode),
|
1266 | 1212 | doc_node=self.visit(doc_ast_node, newnode),
|
1267 | 1213 | )
|
1268 |
| - self._fix_doc_node_position(newnode) |
1269 | 1214 | self._global_names.pop()
|
1270 | 1215 | return newnode
|
1271 | 1216 |
|
|
0 commit comments