Skip to content

Remove uses of NodeNG.doc in favour of doc_node #5905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pylint/checkers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion pylint/checkers/ellipsis_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
):
Expand Down
5 changes: 2 additions & 3 deletions pylint/checkers/spelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
12 changes: 7 additions & 5 deletions pylint/extensions/_check_docs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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}'''>"
Expand Down
12 changes: 6 additions & 6 deletions pylint/extensions/docparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)

Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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)

Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion pylint/extensions/docstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down