This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Ruishi
Recipients Ruishi, lys.nikolaou, pablogsal
Date 2022-03-11.03:08:56
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1646968136.79.0.973896459669.issue46980@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2022-03-11 03:08:56Ruishisetrecipients: + Ruishi, lys.nikolaou, pablogsal
2022-03-11 03:08:56Ruishisetmessageid: <1646968136.79.0.973896459669.issue46980@roundup.psfhosted.org>
2022-03-11 03:08:56Ruishilinkissue46980 messages
2022-03-11 03:08:56Ruishicreate