@@ -17,6 +17,8 @@ class Plugin:
1717 ignore_abstract = False
1818 ignore_stubs = False
1919 ignore_variadic_names = False
20+ ignore_lambdas = False
21+ ignore_nested_functions = False
2022
2123 def __init__ (self , tree : ast .Module ):
2224 self .tree = tree
@@ -50,14 +52,37 @@ def add_options(cls, option_manager: flake8.options.manager.OptionManager) -> No
5052 help = "If provided, then unused *args and **kwargs won't produce warnings." ,
5153 )
5254
55+ option_manager .add_option (
56+ "--unused-arguments-ignore-lambdas" ,
57+ action = "store_true" ,
58+ parse_from_config = True ,
59+ default = cls .ignore_lambdas ,
60+ dest = "unused_arguments_ignore_lambdas" ,
61+ help = "If provided, all lambdas are ignored." ,
62+ )
63+
64+ option_manager .add_option (
65+ "--unused-arguments-ignore-nested-functions" ,
66+ action = "store_true" ,
67+ parse_from_config = True ,
68+ default = cls .ignore_nested_functions ,
69+ dest = "unused_arguments_ignore_nested_functions" ,
70+ help = (
71+ "If provided, only functions at the top level of a module or "
72+ "methods of a class in the top level of a module are checked." ,
73+ ),
74+ )
75+
5376 @classmethod
5477 def parse_options (cls , options : optparse .Values ) -> None :
5578 cls .ignore_abstract = options .unused_arguments_ignore_abstract_functions
5679 cls .ignore_stubs = options .unused_arguments_ignore_stub_functions
5780 cls .ignore_variadic_names = options .unused_arguments_ignore_variadic_names
81+ cls .ignore_lambdas = options .unused_arguments_ignore_lambdas
82+ cls .ignore_nested_functions = options .unused_arguments_ignore_nested_functions
5883
5984 def run (self ) -> Iterable [LintResult ]:
60- finder = FunctionFinder ()
85+ finder = FunctionFinder (self . ignore_nested_functions )
6186 finder .visit (self .tree )
6287
6388 for function in finder .functions :
@@ -70,6 +95,10 @@ def run(self) -> Iterable[LintResult]:
7095 if self .ignore_stubs and is_stub_function (function ):
7196 continue
7297
98+ # ignore lambdas
99+ if self .ignore_lambdas and isinstance (function , ast .Lambda ):
100+ continue
101+
73102 for i , argument in get_unused_arguments (function ):
74103 name = argument .arg
75104 if self .ignore_variadic_names :
@@ -198,12 +227,15 @@ def is_stub_function(function: FunctionTypes) -> bool:
198227class FunctionFinder (NodeVisitor ):
199228 functions : List [FunctionTypes ]
200229
201- def __init__ (self ) -> None :
230+ def __init__ (self , only_top_level = False ) -> None :
202231 super ().__init__ ()
203232 self .functions = []
233+ self .only_top_level = only_top_level
204234
205235 def visit_function_types (self , function : FunctionTypes ) -> None :
206236 self .functions .append (function )
237+ if self .only_top_level :
238+ return
207239 if isinstance (function , ast .Lambda ):
208240 self .visit (function .body )
209241 else :
0 commit comments