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

Allow ast.NodeVisitor's generic_visit to return list of all returned values of calls to visit_ #128127

Open
ahmedhus22 opened this issue Dec 20, 2024 · 1 comment
Labels
pending The issue will be closed if no feedback is provided stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@ahmedhus22
Copy link

ahmedhus22 commented Dec 20, 2024

Feature or enhancement

Proposal:

class FindNames(ast.NodeVisitor):
    def visit_Name(self, node):
        return node.id

tree = ast.parse(src_code)
names = FindNames().visit(tree) # will return None for the tree, should return list of all names.

Currently ast.NodeVisitor iterates through all the nodes in tree calls the defined visit_ methods but returns None.

Proposal:

NodeVisitor's generic_visit should not return None when it has visited all the nodes, It will be more appropriate for it to return List of all return values of visit_ calls.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

@picnixz
Copy link
Contributor

picnixz commented Dec 20, 2024

I've replied on the PR but I'll reply here instead to continue the discussion:

This will blow-up memory footprint a lot, especially for static analysis tool so I'm not very sure it should be done. A visitor pattern is essentially used for traversing nodes, dynamically dispatching to the correct method efficiently, but it's not meant to remember what has been visited by default. If such behaviour is desired, a dedicated visitor class must be implemented instead.

I'm considering closing this one as not planned. cc @JelleZijlstra @Eclips4

@picnixz picnixz added the pending The issue will be closed if no feedback is provided label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending The issue will be closed if no feedback is provided stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants