Skip to content

Commit 4295d41

Browse files
committed
Add typing to basic_checker
1 parent 9935d49 commit 4295d41

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

pylint/checkers/base/basic_checker.py

+35-17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import collections
1010
import itertools
1111
import sys
12+
from collections.abc import Iterator
1213
from typing import TYPE_CHECKING, cast
1314

1415
import astroid
@@ -21,7 +22,7 @@
2122
from pylint.utils import LinterStats
2223

2324
if TYPE_CHECKING:
24-
pass
25+
from pylint.lint.pylinter import PyLinter
2526

2627
if sys.version_info >= (3, 8):
2728
from typing import Literal
@@ -63,10 +64,10 @@ class _BasicChecker(BaseChecker):
6364

6465

6566
def report_by_type_stats(
66-
sect,
67+
sect: reporter_nodes.Section,
6768
stats: LinterStats,
6869
old_stats: LinterStats | None,
69-
):
70+
) -> None:
7071
"""Make a report of.
7172
7273
* percentage of different types documented
@@ -254,11 +255,11 @@ class BasicChecker(_BasicChecker):
254255

255256
reports = (("RP0101", "Statistics by type", report_by_type_stats),)
256257

257-
def __init__(self, linter):
258+
def __init__(self, linter: PyLinter) -> None:
258259
super().__init__(linter)
259-
self._tryfinallys = None
260+
self._tryfinallys: list[nodes.TryFinally] | None = None
260261

261-
def open(self):
262+
def open(self) -> None:
262263
"""Initialize visit variables and statistics."""
263264
py_version = self.linter.config.py_version
264265
self._py38_plus = py_version >= (3, 8)
@@ -285,7 +286,11 @@ def visit_comprehension(self, node: nodes.Comprehension) -> None:
285286
for if_test in node.ifs:
286287
self._check_using_constant_test(node, if_test)
287288

288-
def _check_using_constant_test(self, node, test):
289+
def _check_using_constant_test(
290+
self,
291+
node: nodes.If | nodes.IfExp | nodes.Comprehension,
292+
test: nodes.NodeNG | None,
293+
) -> None:
289294
const_nodes = (
290295
nodes.Module,
291296
nodes.GeneratorExp,
@@ -395,7 +400,9 @@ def visit_expr(self, node: nodes.Expr) -> None:
395400
self.add_message("pointless-statement", node=node)
396401

397402
@staticmethod
398-
def _filter_vararg(node, call_args):
403+
def _filter_vararg(
404+
node: nodes.Lambda, call_args: list[nodes.NodeNG]
405+
) -> Iterator[nodes.NodeNG]:
399406
# Return the arguments for the given call which are
400407
# not passed as vararg.
401408
for arg in call_args:
@@ -409,7 +416,9 @@ def _filter_vararg(node, call_args):
409416
yield arg
410417

411418
@staticmethod
412-
def _has_variadic_argument(args, variadic_name):
419+
def _has_variadic_argument(
420+
args: list[nodes.Starred | nodes.Keyword], variadic_name: str
421+
) -> bool:
413422
if not args:
414423
return True
415424
for arg in args:
@@ -493,10 +502,10 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
493502

494503
visit_asyncfunctiondef = visit_functiondef
495504

496-
def _check_dangerous_default(self, node):
505+
def _check_dangerous_default(self, node: nodes.FunctionDef) -> None:
497506
"""Check for dangerous default values as arguments."""
498507

499-
def is_iterable(internal_node):
508+
def is_iterable(internal_node: nodes.NodeNG) -> bool:
500509
return isinstance(internal_node, (nodes.List, nodes.Set, nodes.Dict))
501510

502511
defaults = node.args.defaults or [] + node.args.kw_defaults or []
@@ -574,7 +583,7 @@ def visit_raise(self, node: nodes.Raise) -> None:
574583
"""
575584
self._check_unreachable(node)
576585

577-
def _check_misplaced_format_function(self, call_node):
586+
def _check_misplaced_format_function(self, call_node: nodes.Call) -> None:
578587
if not isinstance(call_node.func, nodes.Attribute):
579588
return
580589
if call_node.func.attrname != "format":
@@ -664,13 +673,17 @@ def visit_set(self, node: nodes.Set) -> None:
664673

665674
def visit_tryfinally(self, node: nodes.TryFinally) -> None:
666675
"""Update try...finally flag."""
676+
assert self._tryfinallys is not None
667677
self._tryfinallys.append(node)
668678

669679
def leave_tryfinally(self, _: nodes.TryFinally) -> None:
670680
"""Update try...finally flag."""
681+
assert self._tryfinallys is not None
671682
self._tryfinallys.pop()
672683

673-
def _check_unreachable(self, node):
684+
def _check_unreachable(
685+
self, node: nodes.Return | nodes.Continue | nodes.Break | nodes.Raise
686+
) -> None:
674687
"""Check unreachable code."""
675688
unreach_stmt = node.next_sibling()
676689
if unreach_stmt is not None:
@@ -686,7 +699,12 @@ def _check_unreachable(self, node):
686699
return
687700
self.add_message("unreachable", node=unreach_stmt)
688701

689-
def _check_not_in_finally(self, node, node_name, breaker_classes=()):
702+
def _check_not_in_finally(
703+
self,
704+
node: nodes.Break | nodes.Return,
705+
node_name: str,
706+
breaker_classes: tuple[nodes.NodeNG, ...] = (),
707+
) -> None:
690708
"""Check that a node is not inside a 'finally' clause of a
691709
'try...finally' statement.
692710
@@ -706,7 +724,7 @@ def _check_not_in_finally(self, node, node_name, breaker_classes=()):
706724
_node = _parent
707725
_parent = _node.parent
708726

709-
def _check_reversed(self, node):
727+
def _check_reversed(self, node: nodes.Call) -> None:
710728
"""Check that the argument to `reversed` is a sequence."""
711729
try:
712730
argument = utils.safe_infer(utils.get_argument_from_call(node, position=0))
@@ -779,7 +797,7 @@ def visit_with(self, node: nodes.With) -> None:
779797
# we assume it's a nested "with".
780798
self.add_message("confusing-with-statement", node=node)
781799

782-
def _check_self_assigning_variable(self, node):
800+
def _check_self_assigning_variable(self, node: nodes.Assign) -> None:
783801
# Detect assigning to the same variable.
784802

785803
scope = node.scope()
@@ -820,7 +838,7 @@ def _check_self_assigning_variable(self, node):
820838
"self-assigning-variable", args=(target.name,), node=target
821839
)
822840

823-
def _check_redeclared_assign_name(self, targets):
841+
def _check_redeclared_assign_name(self, targets: list[nodes.NodeNG | None]) -> None:
824842
dummy_variables_rgx = self.linter.config.dummy_variables_rgx
825843

826844
for target in targets:

0 commit comments

Comments
 (0)