@@ -19,6 +19,7 @@ class Plugin:
1919 ignore_variadic_names = False
2020 ignore_lambdas = False
2121 ignore_nested_functions = False
22+ ignore_dunder_methods = False
2223
2324 def __init__ (self , tree : ast .Module ):
2425 self .tree = tree
@@ -73,13 +74,26 @@ def add_options(cls, option_manager: flake8.options.manager.OptionManager) -> No
7374 ),
7475 )
7576
77+ option_manager .add_option (
78+ "--unused-arguments-ignore-dunder" ,
79+ action = "store_true" ,
80+ parse_from_config = True ,
81+ default = cls .ignore_dunder_methods ,
82+ dest = "unused_arguments_ignore_dunder_methods" ,
83+ help = (
84+ "If provided, all double-underscore methods are ignored, e.g., __new__, _init__, "
85+ "__enter__, __exit__, __reduce_ex__, etc." ,
86+ ),
87+ )
88+
7689 @classmethod
7790 def parse_options (cls , options : optparse .Values ) -> None :
7891 cls .ignore_abstract = options .unused_arguments_ignore_abstract_functions
7992 cls .ignore_stubs = options .unused_arguments_ignore_stub_functions
8093 cls .ignore_variadic_names = options .unused_arguments_ignore_variadic_names
8194 cls .ignore_lambdas = options .unused_arguments_ignore_lambdas
8295 cls .ignore_nested_functions = options .unused_arguments_ignore_nested_functions
96+ cls .ignore_dunder_methods = options .unused_arguments_ignore_dunder_methods
8397
8498 def run (self ) -> Iterable [LintResult ]:
8599 finder = FunctionFinder (self .ignore_nested_functions )
@@ -99,6 +113,10 @@ def run(self) -> Iterable[LintResult]:
99113 if self .ignore_lambdas and isinstance (function , ast .Lambda ):
100114 continue
101115
116+ # ignore __double_underscore_methods__()
117+ if self .ignore_dunder_methods and is_dunder_method (function ):
118+ continue
119+
102120 for i , argument in get_unused_arguments (function ):
103121 name = argument .arg
104122 if self .ignore_variadic_names :
@@ -224,6 +242,13 @@ def is_stub_function(function: FunctionTypes) -> bool:
224242 return False
225243
226244
245+ def is_dunder_method (function : FunctionTypes ) -> bool :
246+ if isinstance (function , ast .Lambda ):
247+ return False
248+ name = function .name
249+ return name and len (name ) > 4 and name .startswith ("__" ) and name .endswith ("__" )
250+
251+
227252class FunctionFinder (NodeVisitor ):
228253 functions : List [FunctionTypes ]
229254
0 commit comments