11import argparse
22import ast
33import sys
4- from collections .abc import Generator , Iterable , Sequence
4+ import tokenize
5+ from collections .abc import Iterable , Iterator
56from logging import Logger
6- from typing import Any , ClassVar , Final , Literal , NamedTuple , Protocol , overload
7+ from typing import Any , ClassVar , Final , Literal , NamedTuple , Protocol
78
89__version__ : Final [str ]
910LOG : Logger
@@ -27,12 +28,14 @@ class BugBearChecker:
2728 version : ClassVar [str ]
2829 tree : ast .AST | None
2930 filename : str
30- lines : Sequence [str ] | None
31+ lines : list [str ] | None
3132 max_line_length : int
32- visitor : ast . NodeVisitor
33+ visitor : type [ BugBearVisitor ]
3334 options : argparse .Namespace | None
35+ file_tokens : list [tokenize .TokenInfo ] | None
3436 def run (self ) -> Iterable [tuple [int , int , str , type [BugBearChecker ]]]: ...
35- def gen_line_based_checks (self ) -> Generator [error ]: ...
37+ def gen_line_based_checks (self ) -> Iterator [error ]: ...
38+ def gen_token_based_checks (self ) -> Iterator [error ]: ...
3639 @classmethod
3740 def adapt_error (cls , e : error ) -> tuple [int , int , str , type [BugBearChecker ]]: ...
3841 def load_file (self ) -> None : ...
@@ -42,14 +45,15 @@ class BugBearChecker:
4245 self ,
4346 tree : ast .AST | None = ...,
4447 filename : str = ...,
45- lines : Sequence [str ] | None = ...,
48+ lines : list [str ] | None = ...,
4649 max_line_length : int = ...,
4750 options : argparse .Namespace | None = ...,
51+ file_tokens : list [tokenize .TokenInfo ] | None = ...,
4852 ) -> None : ...
4953 def should_warn (self , code : str ) -> bool : ...
5054
51- def names_from_assignments (assign_target : ast .expr ) -> Generator [str ]: ...
52- def children_in_scope (node : ast .AST ) -> Generator [ast .AST ]: ...
55+ def names_from_assignments (assign_target : ast .expr ) -> Iterator [str ]: ...
56+ def children_in_scope (node : ast .AST ) -> Iterator [ast .AST ]: ...
5357
5458class ExceptBaseExceptionVisitor (ast .NodeVisitor ):
5559 root : ast .ExceptHandler
@@ -75,7 +79,7 @@ class AstPositionNode(Protocol):
7579
7680class BugBearVisitor (ast .NodeVisitor ):
7781 filename : str
78- lines : Sequence [str ] | None
82+ lines : list [str ]
7983 b008_b039_extend_immutable_calls : set [str ]
8084 b902_classmethod_decorators : set [str ]
8185 node_window : list [ast .AST ]
@@ -87,7 +91,7 @@ class BugBearVisitor(ast.NodeVisitor):
8791 def __init__ (
8892 self ,
8993 filename : str ,
90- lines : Sequence [str ] | None ,
94+ lines : list [str ],
9195 b008_b039_extend_immutable_calls : set [str ] = ...,
9296 b902_classmethod_decorators : set [str ] = ...,
9397 node_window : list [ast .AST ] = ...,
@@ -108,6 +112,7 @@ class BugBearVisitor(ast.NodeVisitor):
108112 def visit_UAdd (self , node : ast .UAdd ) -> None : ...
109113 def visit_Call (self , node : ast .Call ) -> None : ...
110114 def visit_Module (self , node : ast .Module ) -> None : ...
115+ def visit_Name (self , node : ast .Name ) -> None : ...
111116 def visit_Assign (self , node : ast .Assign ) -> None : ...
112117 def visit_For (self , node : ast .For ) -> None : ...
113118 def visit_AsyncFor (self , node : ast .AsyncFor ) -> None : ...
@@ -130,8 +135,13 @@ class BugBearVisitor(ast.NodeVisitor):
130135 def visit_Compare (self , node : ast .Compare ) -> None : ...
131136 def visit_Raise (self , node : ast .Raise ) -> None : ...
132137 def visit_With (self , node : ast .With ) -> None : ...
138+ def visit_MatchAs (self , node : ast .MatchAs ) -> None : ...
139+ def visit_MatchMapping (self , node : ast .MatchMapping ) -> None : ...
140+ def visit_MatchStar (self , node : ast .MatchStar ) -> None : ...
141+ def visit_Global (self , node : ast .Global ) -> None : ...
133142 def visit_JoinedStr (self , node : ast .JoinedStr ) -> None : ...
134143 def visit_AnnAssign (self , node : ast .AnnAssign ) -> None : ...
144+ def visit_NamedExpr (self , node : ast .NamedExpr ) -> None : ...
135145 def visit_Import (self , node : ast .Import ) -> None : ...
136146 def visit_ImportFrom (self , node : ast .ImportFrom ) -> None : ...
137147 def visit_Set (self , node : ast .Set ) -> None : ...
@@ -150,36 +160,28 @@ class BugBearVisitor(ast.NodeVisitor):
150160 def check_for_b013_b014_b029_b030 (self , node : ast .ExceptHandler ) -> list [str ]: ...
151161 def check_for_b015 (self , node : ast .Compare ) -> None : ...
152162 def check_for_b016 (self , node : ast .Raise ) -> None : ...
153- def check_for_b017 (self , node : ast .With | ast . AsyncWith ) -> None : ...
163+ def check_for_b017 (self , node : ast .With ) -> None : ...
154164 def check_for_b019 (self , node : ast .FunctionDef | ast .AsyncFunctionDef ) -> None : ...
155- def check_for_b020 (self , node : ast .For | ast .AsyncFor | ast . comprehension ) -> None : ...
165+ def check_for_b020 (self , node : ast .For | ast .AsyncFor ) -> None : ...
156166 def check_for_b023 (
157167 self , loop_node : ast .For | ast .AsyncFor | ast .While | ast .GeneratorExp | ast .SetComp | ast .ListComp | ast .DictComp
158168 ) -> None : ...
159169 def check_for_b024_and_b027 (self , node : ast .ClassDef ) -> None : ...
160170 def check_for_b026 (self , call : ast .Call ) -> None : ...
161- def check_for_b031 (self , loop_node : ast .For | ast . AsyncFor ) -> None : ...
171+ def check_for_b031 (self , loop_node : ast .For ) -> None : ...
162172 def check_for_b035 (self , node : ast .DictComp ) -> None : ...
163173 def check_for_b040_add_note (self , node : ast .Attribute ) -> bool : ...
164174 def check_for_b040_usage (self , node : ast .expr | list [ast .expr ] | list [ast .keyword ] | None ) -> None : ...
165175 def check_for_b904 (self , node : ast .Raise ) -> None : ...
166- def walk_function_body (
167- self , node : ast .FunctionDef | ast .AsyncFunctionDef
168- ) -> Generator [tuple [ast .FunctionDef | ast .AsyncFunctionDef , ast .stmt ]]: ...
169- def check_for_b901 (self , node : ast .FunctionDef | ast .AsyncFunctionDef ) -> None : ...
170-
171- @overload
172- @classmethod
173- def find_decorator_name (cls , d : ast .Name | ast .Attribute | ast .Call ) -> str : ...
174- @overload
176+ def walk_function_body (self , node : ast .FunctionDef | ast .AsyncFunctionDef ) -> Iterator [tuple [ast .AST , ast .AST ]]: ...
177+ def check_for_b901 (self , node : ast .FunctionDef ) -> None : ...
175178 @classmethod
176- def find_decorator_name (cls , d : ast .AST ) -> str | None : ...
177-
179+ def find_decorator_name (cls , d : ast .expr ) -> str | None : ...
178180 def check_for_b902 (self , node : ast .FunctionDef | ast .AsyncFunctionDef ) -> None : ...
179181 def check_for_b903 (self , node : ast .ClassDef ) -> None : ...
180182 def check_for_b018 (self , node : ast .AST ) -> None : ...
181- def check_for_b021 (self , node : ast .AsyncFunctionDef | ast .FunctionDef | ast .ClassDef | ast . Module ) -> None : ...
182- def check_for_b022 (self , node : ast .With | ast . AsyncWith ) -> None : ...
183+ def check_for_b021 (self , node : ast .AsyncFunctionDef | ast .FunctionDef | ast .ClassDef ) -> None : ...
184+ def check_for_b022 (self , node : ast .With ) -> None : ...
183185 def check_for_b908 (self , node : ast .With ) -> None : ...
184186 if sys .version_info >= (3 , 11 ):
185187 def check_for_b025 (self , node : ast .Try | ast .TryStar ) -> None : ...
@@ -204,26 +206,29 @@ class BugBearVisitor(ast.NodeVisitor):
204206 else :
205207 def check_for_b047 (self , node : ast .Try ) -> None : ...
206208
209+ def check_for_b048 (self , node : ast .Call ) -> None : ...
207210 def check_for_b049 (self , node : ast .FunctionDef | ast .AsyncFunctionDef ) -> None : ...
208211 def check_for_b909 (self , node : ast .For ) -> None : ...
209212 def check_for_b910 (self , node : ast .Call ) -> None : ...
210213 def check_for_b911 (self , node : ast .Call ) -> None : ...
211214
212- def compose_call_path (node : ast .expr ) -> Generator [str ]: ...
215+ def compose_call_path (node : ast .expr ) -> Iterator [str ]: ...
213216def is_name (node : ast .expr , name : str ) -> bool : ...
214217
215218class B909Checker (ast .NodeVisitor ):
216219 MUTATING_FUNCTIONS : ClassVar [tuple [str , ...]]
217220 name : str
218- key : str
219- mutations : dict [int , list [ast .Assign | ast .AugAssign | ast .Delete | ast .Call ]]
220- def __init__ (self , name : str , key : str ) -> None : ...
221+ key : str | None
222+ check_subscript_assignments : bool
223+ mutations : dict [int , list [ast .Assign | ast .AnnAssign | ast .AugAssign | ast .Delete | ast .Call ]]
224+ def __init__ (self , name : str , key : str | None , * , check_subscript_assignments : bool = True ) -> None : ...
221225 def visit_Assign (self , node : ast .Assign ) -> None : ...
226+ def visit_AnnAssign (self , node : ast .AnnAssign ) -> None : ...
222227 def visit_AugAssign (self , node : ast .AugAssign ) -> None : ...
223228 def visit_Delete (self , node : ast .Delete ) -> None : ...
224229 def visit_Call (self , node : ast .Call ) -> None : ...
225230 def visit_If (self , node : ast .If ) -> None : ...
226- def visit (self , node : ast .AST | list [ast .stmt ]) -> Any : ...
231+ def visit (self , node : ast .AST | list [ast .stmt ]) -> ast . AST | list [ ast . stmt ] | None : ...
227232
228233class NameFinder (ast .NodeVisitor ):
229234 names : dict [str , list [ast .Name ]]
@@ -257,10 +262,9 @@ class FunctionDefDefaultsVisitor(ast.NodeVisitor):
257262 b008_b039_extend_immutable_calls : set [str ] | None = None ,
258263 imported_names : dict [str , str | None ] | None = None ,
259264 ) -> None : ...
260- def visit_mutable_literal_or_comprehension (
261- self , node : ast .List | ast .Dict | ast .Set | ast .ListComp | ast .DictComp | ast .SetComp
262- ) -> None : ...
265+ def visit_mutable_literal_or_comprehension (self , node : ast .expr ) -> None : ...
263266 def visit_Call (self , node : ast .Call ) -> None : ...
267+ def visit_NamedExpr (self , node : ast .NamedExpr ) -> None : ...
264268 def visit_Lambda (self , node : ast .Lambda ) -> None : ...
265269 def visit (self , node : ast .AST | list [ast .AST ]) -> None : ...
266270
@@ -282,6 +286,8 @@ B006_MUTABLE_LITERALS: Final[tuple[Literal["Dict"], Literal["List"], Literal["Se
282286B006_MUTABLE_COMPREHENSIONS : Final [tuple [Literal ["ListComp" ], Literal ["DictComp" ], Literal ["SetComp" ]]]
283287B006_MUTABLE_CALLS : Final [set [str ]]
284288B008_IMMUTABLE_CALLS : Final [set [str ]]
289+ B040_HANDLED_EXCEPTION_CALLS : Final [set [tuple [str , str ]]]
290+ B040_HANDLED_EXCEPTION_NAMES : Final [set [str ]]
285291B014_REDUNDANT_EXCEPTIONS : Final [dict [Literal ["OSError" , "ValueError" ], set [str ]]]
286292B019_CACHES : Final [set [str ]]
287293B046_DICT_TYPES : Final [set [str ]]
0 commit comments