Skip to content

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

@ahmedhus22

Description

@ahmedhus22

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions