summaryrefslogtreecommitdiff
path: root/pylint
diff options
context:
space:
mode:
authorDaniël van Noord <13665637+DanielNoord@users.noreply.github.com>2022-05-31 13:35:27 +0200
committerDaniël van Noord <13665637+DanielNoord@users.noreply.github.com>2022-05-31 14:16:41 +0200
commitacd04fea9bf5a2a7335a135d641e22317be4932e (patch)
treea27f62507c9e672c901c4ec88135e49c17eceb27 /pylint
parent7aa1f08fc4e9142c247c8e277c137a0f7a4157d1 (diff)
downloadpylint-git-acd04fea9bf5a2a7335a135d641e22317be4932e.tar.gz
Add typing to ``basic_error_checker`` and ``docstring_checker``
Diffstat (limited to 'pylint')
-rw-r--r--pylint/checkers/base/basic_error_checker.py32
-rw-r--r--pylint/checkers/base/docstring_checker.py18
2 files changed, 29 insertions, 21 deletions
diff --git a/pylint/checkers/base/basic_error_checker.py b/pylint/checkers/base/basic_error_checker.py
index ad70b26f5..97176fec2 100644
--- a/pylint/checkers/base/basic_error_checker.py
+++ b/pylint/checkers/base/basic_error_checker.py
@@ -24,7 +24,7 @@ REDEFINABLE_METHODS = frozenset(("__module__",))
TYPING_FORWARD_REF_QNAME = "typing.ForwardRef"
-def _get_break_loop_node(break_node):
+def _get_break_loop_node(break_node: nodes.Break) -> nodes.For | nodes.While | None:
"""Returns the loop node that holds the break node in arguments.
Args:
@@ -45,7 +45,7 @@ def _get_break_loop_node(break_node):
return parent
-def _loop_exits_early(loop):
+def _loop_exits_early(loop: nodes.For | nodes.While) -> bool:
"""Returns true if a loop may end with a break statement.
Args:
@@ -56,7 +56,7 @@ def _loop_exits_early(loop):
"""
loop_nodes = (nodes.For, nodes.While)
definition_nodes = (nodes.FunctionDef, nodes.ClassDef)
- inner_loop_nodes = [
+ inner_loop_nodes: list[nodes.For | nodes.While] = [
_node
for _node in loop.nodes_of_class(loop_nodes, skip_klass=definition_nodes)
if _node != loop
@@ -77,7 +77,7 @@ def _has_abstract_methods(node):
return len(utils.unimplemented_abstract_methods(node)) > 0
-def redefined_by_decorator(node):
+def redefined_by_decorator(node: nodes.FunctionDef) -> bool:
"""Return True if the object is a method redefined via decorator.
For example:
@@ -210,7 +210,7 @@ class BasicErrorChecker(_BasicChecker):
def visit_classdef(self, node: nodes.ClassDef) -> None:
self._check_redefinition("class", node)
- def _too_many_starred_for_tuple(self, assign_tuple):
+ def _too_many_starred_for_tuple(self, assign_tuple: nodes.Tuple) -> bool:
starred_count = 0
for elem in assign_tuple.itered():
if isinstance(elem, nodes.Tuple):
@@ -296,7 +296,7 @@ class BasicErrorChecker(_BasicChecker):
visit_asyncfunctiondef = visit_functiondef
- def _check_name_used_prior_global(self, node):
+ def _check_name_used_prior_global(self, node: nodes.FunctionDef) -> None:
scope_globals = {
name: child
@@ -323,10 +323,10 @@ class BasicErrorChecker(_BasicChecker):
"used-prior-global-declaration", node=node_name, args=(name,)
)
- def _check_nonlocal_and_global(self, node):
+ def _check_nonlocal_and_global(self, node: nodes.FunctionDef) -> None:
"""Check that a name is both nonlocal and global."""
- def same_scope(current):
+ def same_scope(current: nodes.Global | nodes.Nonlocal) -> bool:
return current.scope() is node
from_iter = itertools.chain.from_iterable
@@ -391,7 +391,7 @@ class BasicErrorChecker(_BasicChecker):
):
self.add_message("nonexistent-operator", node=node, args=node.op * 2)
- def _check_nonlocal_without_binding(self, node, name):
+ def _check_nonlocal_without_binding(self, node: nodes.Nonlocal, name: str) -> None:
current_scope = node.scope()
while True:
if current_scope.parent is None:
@@ -424,7 +424,7 @@ class BasicErrorChecker(_BasicChecker):
for inferred in infer_all(node.func):
self._check_inferred_class_is_abstract(inferred, node)
- def _check_inferred_class_is_abstract(self, inferred, node):
+ def _check_inferred_class_is_abstract(self, inferred, node: nodes.Call):
if not isinstance(inferred, nodes.ClassDef):
return
@@ -461,11 +461,11 @@ class BasicErrorChecker(_BasicChecker):
"abstract-class-instantiated", args=(inferred.name,), node=node
)
- def _check_yield_outside_func(self, node):
+ def _check_yield_outside_func(self, node: nodes.Yield) -> None:
if not isinstance(node.frame(future=True), (nodes.FunctionDef, nodes.Lambda)):
self.add_message("yield-outside-function", node=node)
- def _check_else_on_loop(self, node):
+ def _check_else_on_loop(self, node: nodes.For | nodes.While) -> None:
"""Check that any loop with an else clause has a break statement."""
if node.orelse and not _loop_exits_early(node):
self.add_message(
@@ -477,7 +477,9 @@ class BasicErrorChecker(_BasicChecker):
line=node.orelse[0].lineno - 1,
)
- def _check_in_loop(self, node, node_name):
+ def _check_in_loop(
+ self, node: nodes.Continue | nodes.Break, node_name: str
+ ) -> None:
"""Check that a node is inside a for or while loop."""
for parent in node.node_ancestors():
if isinstance(parent, (nodes.For, nodes.While)):
@@ -495,7 +497,9 @@ class BasicErrorChecker(_BasicChecker):
self.add_message("not-in-loop", node=node, args=node_name)
- def _check_redefinition(self, redeftype, node):
+ def _check_redefinition(
+ self, redeftype: str, node: nodes.Call | nodes.FunctionDef
+ ) -> None:
"""Check for redefinition of a function / method / class name."""
parent_frame = node.parent.frame(future=True)
diff --git a/pylint/checkers/base/docstring_checker.py b/pylint/checkers/base/docstring_checker.py
index 47c9d221f..3b53ff882 100644
--- a/pylint/checkers/base/docstring_checker.py
+++ b/pylint/checkers/base/docstring_checker.py
@@ -4,6 +4,8 @@
"""Docstring checker from the basic checker."""
+from __future__ import annotations
+
import re
import sys
@@ -28,7 +30,9 @@ else:
NO_REQUIRED_DOC_RGX = re.compile("^_")
-def _infer_dunder_doc_attribute(node):
+def _infer_dunder_doc_attribute(
+ node: nodes.Module | nodes.ClassDef | nodes.FunctionDef,
+) -> str | None:
# Try to see if we have a `__doc__` attribute.
try:
docstring = node["__doc__"]
@@ -40,7 +44,7 @@ def _infer_dunder_doc_attribute(node):
return None
if not isinstance(docstring, nodes.Const):
return None
- return docstring.value
+ return str(docstring.value)
class DocStringChecker(_BasicChecker):
@@ -101,7 +105,7 @@ class DocStringChecker(_BasicChecker):
),
)
- def open(self):
+ def open(self) -> None:
self.linter.stats.reset_undocumented()
@utils.only_required_for_messages("missing-docstring", "empty-docstring")
@@ -153,10 +157,10 @@ class DocStringChecker(_BasicChecker):
def _check_docstring(
self,
node_type: Literal["class", "function", "method", "module"],
- node,
- report_missing=True,
- confidence=interfaces.HIGH,
- ):
+ node: nodes.Module | nodes.ClassDef | nodes.FunctionDef,
+ report_missing: bool = True,
+ confidence: interfaces.Confidence = interfaces.HIGH,
+ ) -> None:
"""Check if the node has a non-empty docstring."""
docstring = node.doc_node.value if node.doc_node else None
if docstring is None: