Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast.FunctionDef cannot find functions under if statement #91136

Closed
Zero0one1 mannequin opened this issue Mar 11, 2022 · 4 comments
Closed

ast.FunctionDef cannot find functions under if statement #91136

Zero0one1 mannequin opened this issue Mar 11, 2022 · 4 comments
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@Zero0one1
Copy link
Mannequin

Zero0one1 mannequin commented Mar 11, 2022

BPO 46980
Nosy @JelleZijlstra, @lysnikolaou, @pablogsal, @Zero0one1

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2022-03-11.04:09:18.289>
created_at = <Date 2022-03-11.03:08:56.757>
labels = ['interpreter-core', 'invalid', 'type-feature', '3.7']
title = 'ast.FunctionDef cannot find functions under if statement'
updated_at = <Date 2022-03-11.07:01:38.467>
user = 'https://github.com/Zero0one1'

bugs.python.org fields:

activity = <Date 2022-03-11.07:01:38.467>
actor = 'Ruishi'
assignee = 'none'
closed = True
closed_date = <Date 2022-03-11.04:09:18.289>
closer = 'JelleZijlstra'
components = ['Parser']
creation = <Date 2022-03-11.03:08:56.757>
creator = 'Ruishi'
dependencies = []
files = []
hgrepos = []
issue_num = 46980
keywords = []
message_count = 4.0
messages = ['414886', '414887', '414888', '414891']
nosy_count = 4.0
nosy_names = ['JelleZijlstra', 'lys.nikolaou', 'pablogsal', 'Ruishi']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue46980'
versions = ['Python 3.7']

@Zero0one1
Copy link
Mannequin Author

Zero0one1 mannequin commented Mar 11, 2022

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.

@Zero0one1 Zero0one1 mannequin added 3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Mar 11, 2022
@Zero0one1
Copy link
Mannequin Author

Zero0one1 mannequin commented Mar 11, 2022

I'm not sure whether I should file this issue here or on Github and I'm not an expert on this so I think I cannot contribute to this but only report to you.

@JelleZijlstra
Copy link
Member

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.

@Zero0one1
Copy link
Mannequin Author

Zero0one1 mannequin commented Mar 11, 2022

I see. It's my misunderstanding. Thank you for your help!

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant