Skip to content

Commit

Permalink
B906: ignore functions whose _fields attribute can't contain ast.AST …
Browse files Browse the repository at this point in the history
…subnodes (#335)
  • Loading branch information
jakkdl committed Jan 18, 2023
1 parent 4435548 commit e7137ec
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ MIT
Change Log
----------

Future
~~~~~~~~~

* B906: Ignore ``visit_`` functions with a ``_fields`` attribute that can't contain ast.AST subnodes. (#330)

23.1.17
~~~~~~~~~

Expand Down
25 changes: 21 additions & 4 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,12 +997,29 @@ def check_for_b906(self, node: ast.FunctionDef):
if not node.name.startswith("visit_"):
return

# extract what's visited, only error if it's a valid ast subclass
# with a non-empty _fields attribute - which is what's iterated over in
# ast.NodeVisitor.generic_visit
# extract what's visited
class_name = node.name[len("visit_") :]
class_type = getattr(ast, class_name, None)
if class_type is None or not getattr(class_type, "_fields", None):

if (
# not a valid ast subclass
class_type is None
# doesn't have a non-empty '_fields' attribute - which is what's
# iterated over in ast.NodeVisitor.generic_visit
or not getattr(class_type, "_fields", None)
# or can't contain any ast subnodes that could be visited
# See https://docs.python.org/3/library/ast.html#abstract-grammar
or class_type.__name__
in (
"alias",
"Constant",
"Global",
"MatchSingleton",
"MatchStar",
"Nonlocal",
"TypeIgnore",
)
):
return

for n in itertools.chain.from_iterable(ast.walk(nn) for nn in node.body):
Expand Down
30 changes: 30 additions & 0 deletions tests/b906.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,33 @@ def a():

def visit_():
...


# Check exceptions for ast types that only contain ADSL builtin types
# i.e. don't contain any ast.AST subnodes and therefore don't need a generic_visit
def visit_alias():
...


def visit_Constant():
...


def visit_Global():
...


def visit_MatchSingleton():
...


def visit_MatchStar():
...


def visit_Nonlocal():
...


def visit_TypeIgnore():
...

0 comments on commit e7137ec

Please sign in to comment.