@@ -12,6 +12,7 @@ class BuiltinsChecker:
12
12
argument_msg = 'A002 argument "{0}" is shadowing a Python builtin'
13
13
class_attribute_msg = 'A003 class attribute "{0}" is shadowing a Python builtin'
14
14
import_msg = 'A004 import statement "{0}" is shadowing a Python builtin'
15
+ lambda_argument_msg = 'A005 lambda argument "{0}" is shadowing a Python builtin'
15
16
16
17
names = []
17
18
ignore_list = {
@@ -58,7 +59,7 @@ def run(self):
58
59
for child in ast .iter_child_nodes (statement ):
59
60
child .__flake8_builtins_parent = statement
60
61
61
- function_nodes = [ast .FunctionDef , ast . Lambda ]
62
+ function_nodes = [ast .FunctionDef ]
62
63
if getattr (ast , 'AsyncFunctionDef' , None ):
63
64
function_nodes .append (ast .AsyncFunctionDef )
64
65
function_nodes = tuple (function_nodes )
@@ -88,6 +89,9 @@ def run(self):
88
89
elif isinstance (statement , function_nodes ):
89
90
value = self .check_function_definition (statement )
90
91
92
+ elif isinstance (statement , ast .Lambda ):
93
+ value = self .check_lambda_definition (statement )
94
+
91
95
elif isinstance (statement , for_nodes ):
92
96
value = self .check_for_loop (statement )
93
97
@@ -136,7 +140,7 @@ def check_assignment(self, statement):
136
140
stack .extend (list (item .value .elts ))
137
141
138
142
def check_function_definition (self , statement ):
139
- if not isinstance ( statement , ast . Lambda ) and statement .name in self .names :
143
+ if statement .name in self .names :
140
144
msg = self .assign_msg
141
145
if type (statement .__flake8_builtins_parent ) is ast .ClassDef :
142
146
msg = self .class_attribute_msg
@@ -156,6 +160,20 @@ def check_function_definition(self, statement):
156
160
variable = arg .arg ,
157
161
)
158
162
163
+ def check_lambda_definition (self , statement ):
164
+ all_arguments = []
165
+ all_arguments .extend (statement .args .args )
166
+ all_arguments .extend (getattr (statement .args , 'kwonlyargs' , []))
167
+ all_arguments .extend (getattr (statement .args , 'posonlyargs' , []))
168
+
169
+ for arg in all_arguments :
170
+ if isinstance (arg , ast .arg ) and arg .arg in self .names :
171
+ yield self .error (
172
+ arg ,
173
+ message = self .lambda_argument_msg ,
174
+ variable = arg .arg ,
175
+ )
176
+
159
177
def check_for_loop (self , statement ):
160
178
stack = [statement .target ]
161
179
while stack :
0 commit comments