[issue46980] ast.FunctionDef cannot find functions under if statement

Jelle Zijlstra report at bugs.python.org
Thu Mar 10 23:09:18 EST 2022


Jelle Zijlstra <jelle.zijlstra at gmail.com> added the comment:

This is the right place to file an issue.

Your code is incorrect; it will find only top-level functions. Functions within an `if` statement will be nested inside an `ast.If` node. To find all functions in a file, you'll need to recurse into nested nodes. For example, you could use `ast.walk`, an `ast.NodeVisitor`, or manually check for nodes like `ast.If`. Which one is best depends on your needs. For example, the first two will also find methods in classes. The ast module documentation has more information.

----------
nosy: +Jelle Zijlstra
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46980>
_______________________________________


More information about the Python-bugs-list mailing list