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
Fix AST visitor traversal order #5221
Merged
charliermarsh
merged 4 commits into
astral-sh:main
from
jgberry:fix-ast-visitor-argument-order
Jun 21, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9338be2
Traverse argument default before argument annotation
jgberry 7d5caba
Visit decorators before other function and class children
jgberry 78a8eca
Fix variable annotations after expression
jgberry ad3b630
Move annotated assignment target traversal after annotation traversal
jgberry File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we instead need to change how we walk over these such that, in
walk_arguments
, we walk over the defaults, then the arguments... We may want to removevisit_arg_with_default
from the interface entirely. 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's the way I originally implemented it, but after resolving the conflicts from #5192, this seemed to be the simplest way to do things. I'm open to restructuring, but it would make this PR more involved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't necessarily visit in the described order though. Given:
This would visit
1
, thenint
, then2.0
, thenfloat
. I thought you were looking to enforce1
, then2.0
, thenint
, thenfloat
. Is this change sufficient? I'm fine with either but the latter seems "more correct".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right. This doesn't completely solve the issue. But, given as given as function arguments are now walked one by one, it wouldn't be possible to traverse them in the correct order without removing or avoiding calling
visit_arg_with_default
. I think the simplest solution would be to havewalk_arguments
handle all argument traversal and never callvisit_arg_with_default
, but this isn't the cleanest in my opinion. I'll throw this change in though and you can let me know what you think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this became unbearably messy for my tastes. The problem is we need to call
visit_arg
with aArgWithDefault
, so we have to convert anArgWithDefault
to just a plainArg
... but then there are lifetime issues to resolve and the whole thing blows up. I'm inclined to leave this as is for now. Yes, it's not completely correct, but it's so subtle that I don't anticipate it causing issues.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hah, ok -- that's fine, lets see how it goes, this is clearly an improvement.