[New-bugs-announce] [issue46980] ast.FunctionDef cannot find functions under if statement

Ruishi report at bugs.python.org
Thu Mar 10 22:08:56 EST 2022


New submission from Ruishi <summerlinasity at gmail.com>:

When I use the Python ast package to get the functions of Python files, I find the functions defined in the body of `if` statement cannot be recognized.

Here is my code:
with open(py_file, 'r') as f:
    data = f.read()
    module = ast.parse(data)
    func_def = [node for node in module.body if isinstance(node, ast.FunctionDef)]

Here is an example of Python file:
if supports_bytes_environ:
    def _check_bytes(value):
        if not isinstance(value, bytes):
            raise TypeError("bytes expected, not %s" % type(value).__name__)
        return value

The function `_check_bytes` is not in `func_def`. I also tested `ast.iter_child_nodes(module)` and it also has this issue.

----------
components: Parser
messages: 414886
nosy: Ruishi, lys.nikolaou, pablogsal
priority: normal
severity: normal
status: open
title: ast.FunctionDef cannot find functions under if statement
type: enhancement
versions: Python 3.7

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


More information about the New-bugs-announce mailing list