Skip to content

stdlib/3/ast: add visit_* methods to NodeVisitor #3796

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

Merged
merged 1 commit into from
Mar 6, 2020

Conversation

bluetech
Copy link
Contributor

@bluetech bluetech commented Mar 1, 2020

NodeVisitor recurses over an AST tree. When encountering a node, it
checks if a method called visit_{node.__class__.__name__} exists, and
calls it if so, otherwise calls the generic visitor.

Add the possible methods to NodeVisitor. This is not exactly correct,
since the methods don't actually exist on NodeVisitor, e.g.
NodeVisitor().visit_Module(...) doesn't work. But it's nice for
subclasses to know which methods they can override and which type they
should have.

@bluetech
Copy link
Contributor Author

bluetech commented Mar 1, 2020

This is re. #3767 (cc @hauntsaninja). I think it's either this (have all) or remove the visit_Constant (have none).

@bluetech
Copy link
Contributor Author

bluetech commented Mar 1, 2020

Hmm stubtest complains "ast.NodeVisitor.visit_withitem is not present at runtime" (etc.), which is true, as explained. I suppose once backward compat for Constant is removed, it will also fail for visit_Constant.

@srittau
Copy link
Collaborator

srittau commented Mar 2, 2020

I think it makes sense to have these defined for the reason you stated. We probably need stubtest overrides.

NodeVisitor recurses over an AST tree. When encountering a node, it
checks if a method called `visit_{node.__class__.__name__}` exists, and
calls it if so, otherwise calls the generic visitor.

Add the possible methods to NodeVisitor. This is not exactly correct,
since the methods don't *actually* exist on NodeVisitor, e.g.
`NodeVisitor().visit_Module(...)` doesn't work. But it's nice for
subclasses to know which methods they can override and which type they
should have.
@bluetech bluetech force-pushed the ast-nodevisitor-visit branch from bc3b64b to 1b1292a Compare March 5, 2020 19:47
@bluetech
Copy link
Contributor Author

bluetech commented Mar 5, 2020

I added the methods to the stubtest whitelist, and it passes now.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, although I did not verify it in depth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants