fix[parser]: preserve for-loop annotation spans#703
Draft
michaelos443 wants to merge 1 commit into
Draft
Conversation
The parser previously handled annotated for-loop targets by parsing a\ndummy AnnAssign and then swapping its target in place. That left\nsource-range metadata tied to the temporary node shape, which made\nsource splicing for loop annotations harder to reason about and more\nfragile.\n\nParse the removed annotation tokens in a dedicated helper and return\nonly the annotation expression. visit_For now builds the final\nAnnAssign directly from the real loop target, so the resulting node is\nconstructed explicitly around the source span we actually want to\npreserve.\n\nAdd a unit test that checks the synthesized target keeps\nnode_source_code aligned with the original source for\nfor-loop annotations.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What I did
_visit_for_loop_annotationhelper inAnnotatingVisitorfake_nodetarget swapping pathvisit_Forbuild the syntheticAnnAssigndirectly from the realloop target and parsed annotation span
AnnAssign.node_source_codevalue on annotated for-loops
How I did it
The pre-parser still supplies the removed annotation tokens, but the parser
now handles them through a dedicated helper that parses the temporary
annotated assignment only far enough to extract the annotation expression.
visit_Forthen constructs the finalpython_ast.AnnAssignexplicitly fromnode.targetplus that parsed annotation, instead of mutating a temporaryparsed node in place. This keeps the source range tied to the real target
span while preserving the existing invalid-annotation checks.
How to verify it
python -m py_compile vyper/ast/parse.py tests/unit/ast/test_annotate_and_optimize_ast.pygit diff --checknode_source_code == "i: uint256"python -m pytest tests/unit/ast/test_annotate_and_optimize_ast.py -qis currently blocked in this environment because
pytestis not installedCommit message
The parser previously handled annotated for-loop targets by parsing a dummy
AnnAssignand then swapping its target in place. That left source-rangemetadata tied to the temporary node shape, which made source splicing for
loop annotations harder to reason about and more fragile.
Parse the removed annotation tokens in a dedicated helper and return only
the annotation expression.
visit_Fornow builds the finalAnnAssigndirectly from the real loop target, so the resulting node is constructed
explicitly around the source span we actually want to preserve.
Add a unit test that checks the synthesized target keeps
node_source_codealigned with the original source for for-loopannotations.
Description for the changelog
Improve parser handling for annotated for-loop targets and preserve their
source spans.
Cute Animal Picture
Pull Request opened by Augment Code | View session