Skip to content

Instantly share code, notes, and snippets.

@hofrob
Created November 13, 2022 20:00
Show Gist options
  • Save hofrob/9dec2f89ef41cfea18536eadf06f185c to your computer and use it in GitHub Desktop.
Save hofrob/9dec2f89ef41cfea18536eadf06f185c to your computer and use it in GitHub Desktop.
Examples on how to use `match` when walking through an AST using astroid
def _walk_through(node: astroid.NodeNG):
for symbol in node.get_children():
match symbol:
# check all classes
case astroid.ClassDef():
_check_classes(symbol)
# check all functions with name "some_function_name"
case astroid.FunctionDef(name="some_function_name"):
_check_some_function_name(symbol)
case astroid.Expr(
value=astroid.Call(func=func, args=args, keywords=keywords)
):
_check_function_call(func, args, keywords)
case astroid.Assign(value=astroid.Subscript() as name):
_check_assignment_value(name)
_walk_through(symbol)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment