Created
November 13, 2022 20:00
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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