Commit e7137ec 1 parent 4435548 commit e7137ec Copy full SHA for e7137ec
File tree 3 files changed +56
-4
lines changed
3 files changed +56
-4
lines changed Original file line number Diff line number Diff line change 308
308
Change Log
309
309
----------
310
310
311
+ Future
312
+ ~~~~~~~~~
313
+
314
+ * B906: Ignore ``visit_ `` functions with a ``_fields `` attribute that can't contain ast.AST subnodes. (#330)
315
+
311
316
23.1.17
312
317
~~~~~~~~~
313
318
Original file line number Diff line number Diff line change @@ -997,12 +997,29 @@ def check_for_b906(self, node: ast.FunctionDef):
997
997
if not node .name .startswith ("visit_" ):
998
998
return
999
999
1000
- # extract what's visited, only error if it's a valid ast subclass
1001
- # with a non-empty _fields attribute - which is what's iterated over in
1002
- # ast.NodeVisitor.generic_visit
1000
+ # extract what's visited
1003
1001
class_name = node .name [len ("visit_" ) :]
1004
1002
class_type = getattr (ast , class_name , None )
1005
- if class_type is None or not getattr (class_type , "_fields" , None ):
1003
+
1004
+ if (
1005
+ # not a valid ast subclass
1006
+ class_type is None
1007
+ # doesn't have a non-empty '_fields' attribute - which is what's
1008
+ # iterated over in ast.NodeVisitor.generic_visit
1009
+ or not getattr (class_type , "_fields" , None )
1010
+ # or can't contain any ast subnodes that could be visited
1011
+ # See https://docs.python.org/3/library/ast.html#abstract-grammar
1012
+ or class_type .__name__
1013
+ in (
1014
+ "alias" ,
1015
+ "Constant" ,
1016
+ "Global" ,
1017
+ "MatchSingleton" ,
1018
+ "MatchStar" ,
1019
+ "Nonlocal" ,
1020
+ "TypeIgnore" ,
1021
+ )
1022
+ ):
1006
1023
return
1007
1024
1008
1025
for n in itertools .chain .from_iterable (ast .walk (nn ) for nn in node .body ):
Original file line number Diff line number Diff line change @@ -54,3 +54,33 @@ def a():
54
54
55
55
def visit_ ():
56
56
...
57
+
58
+
59
+ # Check exceptions for ast types that only contain ADSL builtin types
60
+ # i.e. don't contain any ast.AST subnodes and therefore don't need a generic_visit
61
+ def visit_alias ():
62
+ ...
63
+
64
+
65
+ def visit_Constant ():
66
+ ...
67
+
68
+
69
+ def visit_Global ():
70
+ ...
71
+
72
+
73
+ def visit_MatchSingleton ():
74
+ ...
75
+
76
+
77
+ def visit_MatchStar ():
78
+ ...
79
+
80
+
81
+ def visit_Nonlocal ():
82
+ ...
83
+
84
+
85
+ def visit_TypeIgnore ():
86
+ ...
You can’t perform that action at this time.
0 commit comments