|
23 | 23 | # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE |
24 | 24 |
|
25 | 25 | """This module renders Astroid nodes as string""" |
26 | | -from typing import TYPE_CHECKING, List |
| 26 | +from typing import TYPE_CHECKING, List, Optional |
27 | 27 |
|
28 | 28 | if TYPE_CHECKING: |
| 29 | + from astroid.nodes import Const |
29 | 30 | from astroid.nodes.node_classes import ( |
30 | 31 | Match, |
31 | 32 | MatchAs, |
@@ -57,9 +58,14 @@ def __call__(self, node): |
57 | 58 | """Makes this visitor behave as a simple function""" |
58 | 59 | return node.accept(self).replace(DOC_NEWLINE, "\n") |
59 | 60 |
|
60 | | - def _docs_dedent(self, doc): |
| 61 | + def _docs_dedent(self, doc_node: Optional["Const"]) -> str: |
61 | 62 | """Stop newlines in docs being indented by self._stmt_list""" |
62 | | - return '\n{}"""{}"""'.format(self.indent, doc.replace("\n", DOC_NEWLINE)) |
| 63 | + if not doc_node: |
| 64 | + return "" |
| 65 | + |
| 66 | + return '\n{}"""{}"""'.format( |
| 67 | + self.indent, doc_node.value.replace("\n", DOC_NEWLINE) |
| 68 | + ) |
63 | 69 |
|
64 | 70 | def _stmt_list(self, stmts, indent=True): |
65 | 71 | """return a list of nodes to string""" |
@@ -183,7 +189,7 @@ def visit_classdef(self, node): |
183 | 189 | args.append("metaclass=" + node._metaclass.accept(self)) |
184 | 190 | args += [n.accept(self) for n in node.keywords] |
185 | 191 | args = f"({', '.join(args)})" if args else "" |
186 | | - docs = self._docs_dedent(node.doc) if node.doc else "" |
| 192 | + docs = self._docs_dedent(node.doc_node) |
187 | 193 | return "\n\n{}class {}{}:{}\n{}\n".format( |
188 | 194 | decorate, node.name, args, docs, self._stmt_list(node.body) |
189 | 195 | ) |
@@ -328,7 +334,7 @@ def visit_formattedvalue(self, node): |
328 | 334 | def handle_functiondef(self, node, keyword): |
329 | 335 | """return a (possibly async) function definition node as string""" |
330 | 336 | decorate = node.decorators.accept(self) if node.decorators else "" |
331 | | - docs = self._docs_dedent(node.doc) if node.doc else "" |
| 337 | + docs = self._docs_dedent(node.doc_node) |
332 | 338 | trailer = ":" |
333 | 339 | if node.returns: |
334 | 340 | return_annotation = " -> " + node.returns.as_string() |
@@ -417,7 +423,7 @@ def visit_listcomp(self, node): |
417 | 423 |
|
418 | 424 | def visit_module(self, node): |
419 | 425 | """return an astroid.Module node as string""" |
420 | | - docs = f'"""{node.doc}"""\n\n' if node.doc else "" |
| 426 | + docs = f'"""{node.doc_node.value}"""\n\n' if node.doc_node else "" |
421 | 427 | return docs + "\n".join(n.accept(self) for n in node.body) + "\n\n" |
422 | 428 |
|
423 | 429 | def visit_name(self, node): |
|
0 commit comments