From 8d334018be526dd470089e0b01e86f94f36e8c03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Sat, 12 Mar 2022 20:00:12 +0100 Subject: [PATCH] Remove uses of ``NodeNG.doc`` in favour of ``doc_node`` --- pylint/checkers/base.py | 4 ++-- pylint/checkers/ellipsis_checker.py | 2 +- pylint/checkers/spelling.py | 5 ++--- pylint/extensions/_check_docs_utils.py | 12 +++++++----- pylint/extensions/docparams.py | 12 ++++++------ pylint/extensions/docstyle.py | 2 +- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py index 71dabd8efc..89603dd35a 100644 --- a/pylint/checkers/base.py +++ b/pylint/checkers/base.py @@ -2302,7 +2302,7 @@ def _check_docstring( confidence=interfaces.HIGH, ): """Check if the node has a non-empty docstring.""" - docstring = node.doc + docstring = node.doc_node.value if node.doc_node else None if docstring is None: docstring = _infer_dunder_doc_attribute(node) @@ -2368,7 +2368,7 @@ class PassChecker(_BasicChecker): def visit_pass(self, node: nodes.Pass) -> None: if len(node.parent.child_sequence(node)) > 1 or ( isinstance(node.parent, (nodes.ClassDef, nodes.FunctionDef)) - and (node.parent.doc is not None) + and node.parent.doc_node ): self.add_message("unnecessary-pass", node=node) diff --git a/pylint/checkers/ellipsis_checker.py b/pylint/checkers/ellipsis_checker.py index f29ebed042..a6d44df961 100644 --- a/pylint/checkers/ellipsis_checker.py +++ b/pylint/checkers/ellipsis_checker.py @@ -41,7 +41,7 @@ def visit_const(self, node: nodes.Const) -> None: len(node.parent.parent.child_sequence(node.parent)) > 1 or ( isinstance(node.parent.parent, (nodes.ClassDef, nodes.FunctionDef)) - and (node.parent.parent.doc is not None) + and node.parent.parent.doc_node ) ) ): diff --git a/pylint/checkers/spelling.py b/pylint/checkers/spelling.py index 8c004444e6..e63196155a 100644 --- a/pylint/checkers/spelling.py +++ b/pylint/checkers/spelling.py @@ -460,14 +460,13 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None: def _check_docstring(self, node): """Check the node has any spelling errors.""" - docstring = node.doc - if not docstring: + if not node.doc_node: return start_line = node.lineno + 1 # Go through lines of docstring - for idx, line in enumerate(docstring.splitlines()): + for idx, line in enumerate(node.doc_node.value.splitlines()): self._check_spelling("wrong-spelling-in-docstring", line, start_line + idx) diff --git a/pylint/extensions/_check_docs_utils.py b/pylint/extensions/_check_docs_utils.py index 70b312539e..440af688e5 100644 --- a/pylint/extensions/_check_docs_utils.py +++ b/pylint/extensions/_check_docs_utils.py @@ -25,7 +25,7 @@ """Utility methods for docstring checking.""" import re -from typing import List, Set, Tuple +from typing import List, Optional, Set, Tuple import astroid from astroid import nodes @@ -176,7 +176,9 @@ def possible_exc_types(node: nodes.NodeNG) -> Set[nodes.ClassDef]: return set() -def docstringify(docstring: str, default_type: str = "default") -> "Docstring": +def docstringify( + docstring: Optional[nodes.Const], default_type: str = "default" +) -> "Docstring": best_match = (0, DOCSTRING_TYPES.get(default_type, Docstring)(docstring)) for docstring_type in ( SphinxDocstring, @@ -208,9 +210,9 @@ class Docstring: # These methods are designed to be overridden # pylint: disable=no-self-use - def __init__(self, doc): - doc = doc or "" - self.doc = doc.expandtabs() + def __init__(self, doc: Optional[nodes.Const]) -> None: + docstring = doc.value if doc else "" + self.doc = docstring.expandtabs() def __repr__(self) -> str: return f"<{self.__class__.__name__}:'''{self.doc}'''>" diff --git a/pylint/extensions/docparams.py b/pylint/extensions/docparams.py index 35324ac041..dbe3ee8489 100644 --- a/pylint/extensions/docparams.py +++ b/pylint/extensions/docparams.py @@ -219,7 +219,7 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None: :param node: Node for a function or method definition in the AST :type node: :class:`astroid.scoped_nodes.Function` """ - node_doc = utils.docstringify(node.doc, self.config.default_docstring_type) + node_doc = utils.docstringify(node.doc_node, self.config.default_docstring_type) # skip functions that match the 'no-docstring-rgx' config option no_docstring_rgx = get_global_option(self, "no-docstring-rgx") @@ -244,7 +244,7 @@ def check_functiondef_params(self, node, node_doc): class_node = checker_utils.node_frame_class(node) if class_node is not None: class_doc = utils.docstringify( - class_node.doc, self.config.default_docstring_type + class_node.doc_node, self.config.default_docstring_type ) self.check_single_constructor_params(class_doc, node_doc, class_node) @@ -298,14 +298,14 @@ def visit_raise(self, node: nodes.Raise) -> None: if not expected_excs: return - if not func_node.doc: + if not func_node.doc_node: # If this is a property setter, # the property should have the docstring instead. property_ = utils.get_setters_property(func_node) if property_: func_node = property_ - doc = utils.docstringify(func_node.doc, self.config.default_docstring_type) + doc = utils.docstringify(func_node.doc_node, self.config.default_docstring_type) if not doc.matching_sections(): if doc.doc: missing = {exc.name for exc in expected_excs} @@ -340,7 +340,7 @@ def visit_return(self, node: nodes.Return) -> None: if not isinstance(func_node, astroid.FunctionDef): return - doc = utils.docstringify(func_node.doc, self.config.default_docstring_type) + doc = utils.docstringify(func_node.doc_node, self.config.default_docstring_type) is_property = checker_utils.decorated_with_property(func_node) @@ -361,7 +361,7 @@ def visit_yield(self, node: nodes.Yield) -> None: if not isinstance(func_node, astroid.FunctionDef): return - doc = utils.docstringify(func_node.doc, self.config.default_docstring_type) + doc = utils.docstringify(func_node.doc_node, self.config.default_docstring_type) if doc.supports_yields: doc_has_yields = doc.has_yields() diff --git a/pylint/extensions/docstyle.py b/pylint/extensions/docstyle.py index 7dc26db5d4..e520d0650c 100644 --- a/pylint/extensions/docstyle.py +++ b/pylint/extensions/docstyle.py @@ -58,7 +58,7 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None: visit_asyncfunctiondef = visit_functiondef def _check_docstring(self, node_type, node): - docstring = node.doc + docstring = node.doc_node.value if node.doc_node else None if docstring and docstring[0] == "\n": self.add_message( "docstring-first-line-empty",