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

gh-128127: add feature for ast.NodeVisitor generic_visit to return list of all return values of calls to visit_ #128128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ahmedhus22
Copy link

@ahmedhus22 ahmedhus22 commented Dec 20, 2024

This implementation tries to ensure that previous behaviour of visit is not disturbed. For visit's new behaviour to work, derived classes need to call supers constructor, otherwise it behaves same as before.

Copy link

cpython-cla-bot bot commented Dec 20, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Dec 20, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app
Copy link

bedevere-app bot commented Dec 20, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@ahmedhus22 ahmedhus22 force-pushed the fix-NodeVisitor-return branch from 271dd8f to e98f3fb Compare December 20, 2024 18:28
@ahmedhus22 ahmedhus22 force-pushed the fix-NodeVisitor-return branch from e98f3fb to ab2537e Compare December 20, 2024 18:45
@picnixz
Copy link
Contributor

picnixz commented Dec 20, 2024

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.

@JelleZijlstra
Copy link
Member

There's also some usability issues. For example, currently it's OK to reuse the same visitor instance for multiple concurrent or consecutive visits, but with this change, those runs will accumulate into the same list.

I'm not sure that collecting the return values of all visit_* calls in a single list is a very useful thing to do in the first place, since you don't preserve the structure of the AST.

@ahmedhus22
Copy link
Author

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 also thought about maybe this takes too much memory, and wasn't completely sure about this. Do you think I should make a new class for this feature or make tracking nodes optional(assuming this feature is still desirable)?

@ahmedhus22
Copy link
Author

There's also some usability issues. For example, currently it's OK to reuse the same visitor instance for multiple concurrent or consecutive visits, but with this change, those runs will accumulate into the same list.

I'm not sure that collecting the return values of all visit_* calls in a single list is a very useful thing to do in the first place, since you don't preserve the structure of the AST.

  • Oh, I guess the your first point means it should be optional (if still desirable)
  • As for the usability, it was at least useful for me for getting some source code data about certain specific parts. So, it might be useful for gathering data about code.

@picnixz
Copy link
Contributor

picnixz commented Dec 20, 2024

. Do you think I should make a new class for this feature or make tracking nodes optional(assuming this feature is still desirable)?

I don't think we'll accept this in the standard library, however you can create a third-party library and publish it on pypi.

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

Successfully merging this pull request may close these issues.

3 participants