1- # -*- coding: utf-8 -*-
21import ast
2+ import builtins
33import inspect
44import sys
55
6-
7- try :
8- from flake8 .engine import pep8 as stdin_utils
9- except ImportError :
10- from flake8 import utils as stdin_utils
11-
6+ from flake8 import utils as stdin_utils
127
138WHITE_LIST = {
149 '__name__' ,
1813}
1914
2015
21- if sys .version_info >= (3 , 0 ):
22- import builtins
23-
24- BUILTINS = [a [0 ] for a in inspect .getmembers (builtins ) if a [0 ] not in WHITE_LIST ]
25- PY3 = True
26- else :
27- import __builtin__
28-
29- BUILTINS = [a [0 ] for a in inspect .getmembers (__builtin__ ) if a [0 ] not in WHITE_LIST ]
30- PY3 = False
31-
32- if sys .version_info >= (3 , 6 ):
33- AnnAssign = ast .AnnAssign
34- else : # There was no `AnnAssign` before python3.6
35- AnnAssign = type ('AnnAssign' , (ast .AST ,), {})
16+ BUILTINS = [a [0 ] for a in inspect .getmembers (builtins ) if a [0 ] not in WHITE_LIST ]
3617
3718if sys .version_info >= (3 , 8 ):
3819 NamedExpr = ast .NamedExpr
3920else : # There was no walrus operator before python3.8
4021 NamedExpr = type ('NamedExpr' , (ast .AST ,), {})
4122
4223
43- class BuiltinsChecker ( object ) :
24+ class BuiltinsChecker :
4425 name = 'flake8_builtins'
4526 version = '1.5.2'
4627 assign_msg = 'A001 variable "{0}" is shadowing a python builtin'
@@ -86,7 +67,7 @@ def run(self):
8667
8768 value = None
8869 for statement in ast .walk (tree ):
89- if isinstance (statement , (ast .Assign , AnnAssign , NamedExpr )):
70+ if isinstance (statement , (ast .Assign , ast . AnnAssign , NamedExpr )):
9071 value = self .check_assignment (statement )
9172
9273 elif isinstance (statement , function_nodes ):
@@ -111,8 +92,7 @@ def run(self):
11192 value = self .check_class (statement )
11293
11394 if value :
114- for line , offset , msg , rtype in value :
115- yield line , offset , msg , rtype
95+ yield from value
11696
11797 def check_assignment (self , statement ):
11898 msg = self .assign_msg
@@ -130,7 +110,7 @@ def check_assignment(self, statement):
130110 stack .extend (list (item .elts ))
131111 elif isinstance (item , ast .Name ) and item .id in BUILTINS :
132112 yield self .error (item , message = msg , variable = item .id )
133- elif PY3 and isinstance (item , ast .Starred ):
113+ elif isinstance (item , ast .Starred ):
134114 if hasattr (item .value , 'id' ) and item .value .id in BUILTINS :
135115 yield self .error (
136116 statement ,
@@ -148,23 +128,18 @@ def check_function_definition(self, statement):
148128
149129 yield self .error (statement , message = msg , variable = statement .name )
150130
151- if PY3 :
152- all_arguments = []
153- all_arguments .extend (statement .args .args )
154- all_arguments .extend (getattr (statement .args , 'kwonlyargs' , []))
155- all_arguments .extend (getattr (statement .args , 'posonlyargs' , []))
131+ all_arguments = []
132+ all_arguments .extend (statement .args .args )
133+ all_arguments .extend (getattr (statement .args , 'kwonlyargs' , []))
134+ all_arguments .extend (getattr (statement .args , 'posonlyargs' , []))
156135
157- for arg in all_arguments :
158- if isinstance (arg , ast .arg ) and arg .arg in BUILTINS :
159- yield self .error (
160- arg ,
161- message = self .argument_msg ,
162- variable = arg .arg ,
163- )
164- else :
165- for arg in statement .args .args :
166- if isinstance (arg , ast .Name ) and arg .id in BUILTINS :
167- yield self .error (arg , message = self .argument_msg )
136+ for arg in all_arguments :
137+ if isinstance (arg , ast .arg ) and arg .arg in BUILTINS :
138+ yield self .error (
139+ arg ,
140+ message = self .argument_msg ,
141+ variable = arg .arg ,
142+ )
168143
169144 def check_for_loop (self , statement ):
170145 stack = [statement .target ]
@@ -174,7 +149,7 @@ def check_for_loop(self, statement):
174149 stack .extend (list (item .elts ))
175150 elif isinstance (item , ast .Name ) and item .id in BUILTINS :
176151 yield self .error (statement , variable = item .id )
177- elif PY3 and isinstance (item , ast .Starred ):
152+ elif isinstance (item , ast .Starred ):
178153 if hasattr (item .value , 'id' ) and item .value .id in BUILTINS :
179154 yield self .error (
180155 statement ,
@@ -184,44 +159,31 @@ def check_for_loop(self, statement):
184159 stack .extend (list (item .value .elts ))
185160
186161 def check_with (self , statement ):
187- if not PY3 :
188- var = statement .optional_vars
162+ for item in statement . items :
163+ var = item .optional_vars
189164 if isinstance (var , (ast .Tuple , ast .List )):
190165 for element in var .elts :
191166 if isinstance (element , ast .Name ) and element .id in BUILTINS :
192167 yield self .error (statement , variable = element .id )
168+ elif (
169+ isinstance (element , ast .Starred )
170+ and element .value .id in BUILTINS
171+ ):
172+ yield self .error (
173+ element ,
174+ variable = element .value .id ,
175+ )
193176
194177 elif isinstance (var , ast .Name ) and var .id in BUILTINS :
195178 yield self .error (statement , variable = var .id )
196- else :
197- for item in statement .items :
198- var = item .optional_vars
199- if isinstance (var , (ast .Tuple , ast .List )):
200- for element in var .elts :
201- if isinstance (element , ast .Name ) and element .id in BUILTINS :
202- yield self .error (statement , variable = element .id )
203- elif (
204- isinstance (element , ast .Starred )
205- and element .value .id in BUILTINS
206- ):
207- yield self .error (
208- element ,
209- variable = element .value .id ,
210- )
211-
212- elif isinstance (var , ast .Name ) and var .id in BUILTINS :
213- yield self .error (statement , variable = var .id )
214179
215180 def check_exception (self , statement ):
216181 exception_name = statement .name
217- value = ''
218- if isinstance (exception_name , ast .Name ):
219- value = exception_name .id
220- elif isinstance (exception_name , str ): # Python +3.x
221- value = exception_name
222-
223- if value in BUILTINS :
224- yield self .error (statement , variable = value )
182+ if exception_name is None :
183+ return
184+
185+ if exception_name in BUILTINS :
186+ yield self .error (statement , variable = exception_name )
225187
226188 def check_comprehension (self , statement ):
227189 for generator in statement .generators :
0 commit comments