9
9
import collections
10
10
import itertools
11
11
import sys
12
+ from collections .abc import Iterator
12
13
from typing import TYPE_CHECKING , cast
13
14
14
15
import astroid
21
22
from pylint .utils import LinterStats
22
23
23
24
if TYPE_CHECKING :
24
- pass
25
+ from pylint . lint . pylinter import PyLinter
25
26
26
27
if sys .version_info >= (3 , 8 ):
27
28
from typing import Literal
@@ -63,10 +64,10 @@ class _BasicChecker(BaseChecker):
63
64
64
65
65
66
def report_by_type_stats (
66
- sect ,
67
+ sect : reporter_nodes . Section ,
67
68
stats : LinterStats ,
68
69
old_stats : LinterStats | None ,
69
- ):
70
+ ) -> None :
70
71
"""Make a report of.
71
72
72
73
* percentage of different types documented
@@ -254,11 +255,11 @@ class BasicChecker(_BasicChecker):
254
255
255
256
reports = (("RP0101" , "Statistics by type" , report_by_type_stats ),)
256
257
257
- def __init__ (self , linter ) :
258
+ def __init__ (self , linter : PyLinter ) -> None :
258
259
super ().__init__ (linter )
259
- self ._tryfinallys = None
260
+ self ._tryfinallys : list [ nodes . TryFinally ] | None = None
260
261
261
- def open (self ):
262
+ def open (self ) -> None :
262
263
"""Initialize visit variables and statistics."""
263
264
py_version = self .linter .config .py_version
264
265
self ._py38_plus = py_version >= (3 , 8 )
@@ -285,7 +286,11 @@ def visit_comprehension(self, node: nodes.Comprehension) -> None:
285
286
for if_test in node .ifs :
286
287
self ._check_using_constant_test (node , if_test )
287
288
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 :
289
294
const_nodes = (
290
295
nodes .Module ,
291
296
nodes .GeneratorExp ,
@@ -395,7 +400,9 @@ def visit_expr(self, node: nodes.Expr) -> None:
395
400
self .add_message ("pointless-statement" , node = node )
396
401
397
402
@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 ]:
399
406
# Return the arguments for the given call which are
400
407
# not passed as vararg.
401
408
for arg in call_args :
@@ -409,7 +416,9 @@ def _filter_vararg(node, call_args):
409
416
yield arg
410
417
411
418
@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 :
413
422
if not args :
414
423
return True
415
424
for arg in args :
@@ -493,10 +502,10 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
493
502
494
503
visit_asyncfunctiondef = visit_functiondef
495
504
496
- def _check_dangerous_default (self , node ) :
505
+ def _check_dangerous_default (self , node : nodes . FunctionDef ) -> None :
497
506
"""Check for dangerous default values as arguments."""
498
507
499
- def is_iterable (internal_node ) :
508
+ def is_iterable (internal_node : nodes . NodeNG ) -> bool :
500
509
return isinstance (internal_node , (nodes .List , nodes .Set , nodes .Dict ))
501
510
502
511
defaults = node .args .defaults or [] + node .args .kw_defaults or []
@@ -574,7 +583,7 @@ def visit_raise(self, node: nodes.Raise) -> None:
574
583
"""
575
584
self ._check_unreachable (node )
576
585
577
- def _check_misplaced_format_function (self , call_node ) :
586
+ def _check_misplaced_format_function (self , call_node : nodes . Call ) -> None :
578
587
if not isinstance (call_node .func , nodes .Attribute ):
579
588
return
580
589
if call_node .func .attrname != "format" :
@@ -664,13 +673,17 @@ def visit_set(self, node: nodes.Set) -> None:
664
673
665
674
def visit_tryfinally (self , node : nodes .TryFinally ) -> None :
666
675
"""Update try...finally flag."""
676
+ assert self ._tryfinallys is not None
667
677
self ._tryfinallys .append (node )
668
678
669
679
def leave_tryfinally (self , _ : nodes .TryFinally ) -> None :
670
680
"""Update try...finally flag."""
681
+ assert self ._tryfinallys is not None
671
682
self ._tryfinallys .pop ()
672
683
673
- def _check_unreachable (self , node ):
684
+ def _check_unreachable (
685
+ self , node : nodes .Return | nodes .Continue | nodes .Break | nodes .Raise
686
+ ) -> None :
674
687
"""Check unreachable code."""
675
688
unreach_stmt = node .next_sibling ()
676
689
if unreach_stmt is not None :
@@ -686,7 +699,12 @@ def _check_unreachable(self, node):
686
699
return
687
700
self .add_message ("unreachable" , node = unreach_stmt )
688
701
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 :
690
708
"""Check that a node is not inside a 'finally' clause of a
691
709
'try...finally' statement.
692
710
@@ -706,7 +724,7 @@ def _check_not_in_finally(self, node, node_name, breaker_classes=()):
706
724
_node = _parent
707
725
_parent = _node .parent
708
726
709
- def _check_reversed (self , node ) :
727
+ def _check_reversed (self , node : nodes . Call ) -> None :
710
728
"""Check that the argument to `reversed` is a sequence."""
711
729
try :
712
730
argument = utils .safe_infer (utils .get_argument_from_call (node , position = 0 ))
@@ -779,7 +797,7 @@ def visit_with(self, node: nodes.With) -> None:
779
797
# we assume it's a nested "with".
780
798
self .add_message ("confusing-with-statement" , node = node )
781
799
782
- def _check_self_assigning_variable (self , node ) :
800
+ def _check_self_assigning_variable (self , node : nodes . Assign ) -> None :
783
801
# Detect assigning to the same variable.
784
802
785
803
scope = node .scope ()
@@ -820,7 +838,7 @@ def _check_self_assigning_variable(self, node):
820
838
"self-assigning-variable" , args = (target .name ,), node = target
821
839
)
822
840
823
- def _check_redeclared_assign_name (self , targets ) :
841
+ def _check_redeclared_assign_name (self , targets : list [ nodes . NodeNG | None ]) -> None :
824
842
dummy_variables_rgx = self .linter .config .dummy_variables_rgx
825
843
826
844
for target in targets :
0 commit comments