Skip to content

Commit acd04fe

Browse files
committed
Add typing to basic_error_checker and docstring_checker
1 parent 7aa1f08 commit acd04fe

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

pylint/checkers/base/basic_error_checker.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
TYPING_FORWARD_REF_QNAME = "typing.ForwardRef"
2525

2626

27-
def _get_break_loop_node(break_node):
27+
def _get_break_loop_node(break_node: nodes.Break) -> nodes.For | nodes.While | None:
2828
"""Returns the loop node that holds the break node in arguments.
2929
3030
Args:
@@ -45,7 +45,7 @@ def _get_break_loop_node(break_node):
4545
return parent
4646

4747

48-
def _loop_exits_early(loop):
48+
def _loop_exits_early(loop: nodes.For | nodes.While) -> bool:
4949
"""Returns true if a loop may end with a break statement.
5050
5151
Args:
@@ -56,7 +56,7 @@ def _loop_exits_early(loop):
5656
"""
5757
loop_nodes = (nodes.For, nodes.While)
5858
definition_nodes = (nodes.FunctionDef, nodes.ClassDef)
59-
inner_loop_nodes = [
59+
inner_loop_nodes: list[nodes.For | nodes.While] = [
6060
_node
6161
for _node in loop.nodes_of_class(loop_nodes, skip_klass=definition_nodes)
6262
if _node != loop
@@ -77,7 +77,7 @@ def _has_abstract_methods(node):
7777
return len(utils.unimplemented_abstract_methods(node)) > 0
7878

7979

80-
def redefined_by_decorator(node):
80+
def redefined_by_decorator(node: nodes.FunctionDef) -> bool:
8181
"""Return True if the object is a method redefined via decorator.
8282
8383
For example:
@@ -210,7 +210,7 @@ class BasicErrorChecker(_BasicChecker):
210210
def visit_classdef(self, node: nodes.ClassDef) -> None:
211211
self._check_redefinition("class", node)
212212

213-
def _too_many_starred_for_tuple(self, assign_tuple):
213+
def _too_many_starred_for_tuple(self, assign_tuple: nodes.Tuple) -> bool:
214214
starred_count = 0
215215
for elem in assign_tuple.itered():
216216
if isinstance(elem, nodes.Tuple):
@@ -296,7 +296,7 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
296296

297297
visit_asyncfunctiondef = visit_functiondef
298298

299-
def _check_name_used_prior_global(self, node):
299+
def _check_name_used_prior_global(self, node: nodes.FunctionDef) -> None:
300300

301301
scope_globals = {
302302
name: child
@@ -323,10 +323,10 @@ def _check_name_used_prior_global(self, node):
323323
"used-prior-global-declaration", node=node_name, args=(name,)
324324
)
325325

326-
def _check_nonlocal_and_global(self, node):
326+
def _check_nonlocal_and_global(self, node: nodes.FunctionDef) -> None:
327327
"""Check that a name is both nonlocal and global."""
328328

329-
def same_scope(current):
329+
def same_scope(current: nodes.Global | nodes.Nonlocal) -> bool:
330330
return current.scope() is node
331331

332332
from_iter = itertools.chain.from_iterable
@@ -391,7 +391,7 @@ def visit_unaryop(self, node: nodes.UnaryOp) -> None:
391391
):
392392
self.add_message("nonexistent-operator", node=node, args=node.op * 2)
393393

394-
def _check_nonlocal_without_binding(self, node, name):
394+
def _check_nonlocal_without_binding(self, node: nodes.Nonlocal, name: str) -> None:
395395
current_scope = node.scope()
396396
while True:
397397
if current_scope.parent is None:
@@ -424,7 +424,7 @@ def visit_call(self, node: nodes.Call) -> None:
424424
for inferred in infer_all(node.func):
425425
self._check_inferred_class_is_abstract(inferred, node)
426426

427-
def _check_inferred_class_is_abstract(self, inferred, node):
427+
def _check_inferred_class_is_abstract(self, inferred, node: nodes.Call):
428428
if not isinstance(inferred, nodes.ClassDef):
429429
return
430430

@@ -461,11 +461,11 @@ def _check_inferred_class_is_abstract(self, inferred, node):
461461
"abstract-class-instantiated", args=(inferred.name,), node=node
462462
)
463463

464-
def _check_yield_outside_func(self, node):
464+
def _check_yield_outside_func(self, node: nodes.Yield) -> None:
465465
if not isinstance(node.frame(future=True), (nodes.FunctionDef, nodes.Lambda)):
466466
self.add_message("yield-outside-function", node=node)
467467

468-
def _check_else_on_loop(self, node):
468+
def _check_else_on_loop(self, node: nodes.For | nodes.While) -> None:
469469
"""Check that any loop with an else clause has a break statement."""
470470
if node.orelse and not _loop_exits_early(node):
471471
self.add_message(
@@ -477,7 +477,9 @@ def _check_else_on_loop(self, node):
477477
line=node.orelse[0].lineno - 1,
478478
)
479479

480-
def _check_in_loop(self, node, node_name):
480+
def _check_in_loop(
481+
self, node: nodes.Continue | nodes.Break, node_name: str
482+
) -> None:
481483
"""Check that a node is inside a for or while loop."""
482484
for parent in node.node_ancestors():
483485
if isinstance(parent, (nodes.For, nodes.While)):
@@ -495,7 +497,9 @@ def _check_in_loop(self, node, node_name):
495497

496498
self.add_message("not-in-loop", node=node, args=node_name)
497499

498-
def _check_redefinition(self, redeftype, node):
500+
def _check_redefinition(
501+
self, redeftype: str, node: nodes.Call | nodes.FunctionDef
502+
) -> None:
499503
"""Check for redefinition of a function / method / class name."""
500504
parent_frame = node.parent.frame(future=True)
501505

pylint/checkers/base/docstring_checker.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
"""Docstring checker from the basic checker."""
66

7+
from __future__ import annotations
8+
79
import re
810
import sys
911

@@ -28,7 +30,9 @@
2830
NO_REQUIRED_DOC_RGX = re.compile("^_")
2931

3032

31-
def _infer_dunder_doc_attribute(node):
33+
def _infer_dunder_doc_attribute(
34+
node: nodes.Module | nodes.ClassDef | nodes.FunctionDef,
35+
) -> str | None:
3236
# Try to see if we have a `__doc__` attribute.
3337
try:
3438
docstring = node["__doc__"]
@@ -40,7 +44,7 @@ def _infer_dunder_doc_attribute(node):
4044
return None
4145
if not isinstance(docstring, nodes.Const):
4246
return None
43-
return docstring.value
47+
return str(docstring.value)
4448

4549

4650
class DocStringChecker(_BasicChecker):
@@ -101,7 +105,7 @@ class DocStringChecker(_BasicChecker):
101105
),
102106
)
103107

104-
def open(self):
108+
def open(self) -> None:
105109
self.linter.stats.reset_undocumented()
106110

107111
@utils.only_required_for_messages("missing-docstring", "empty-docstring")
@@ -153,10 +157,10 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
153157
def _check_docstring(
154158
self,
155159
node_type: Literal["class", "function", "method", "module"],
156-
node,
157-
report_missing=True,
158-
confidence=interfaces.HIGH,
159-
):
160+
node: nodes.Module | nodes.ClassDef | nodes.FunctionDef,
161+
report_missing: bool = True,
162+
confidence: interfaces.Confidence = interfaces.HIGH,
163+
) -> None:
160164
"""Check if the node has a non-empty docstring."""
161165
docstring = node.doc_node.value if node.doc_node else None
162166
if docstring is None:

0 commit comments

Comments
 (0)