Skip to content

fix[parser]: preserve for-loop annotation spans#703

Draft
michaelos443 wants to merge 1 commit into
mainfrom
michaelos443/fix-vyper-for-loop-annotations
Draft

fix[parser]: preserve for-loop annotation spans#703
michaelos443 wants to merge 1 commit into
mainfrom
michaelos443/fix-vyper-for-loop-annotations

Conversation

@michaelos443
Copy link
Copy Markdown
Collaborator

What I did

  • added a dedicated _visit_for_loop_annotation helper in AnnotatingVisitor
  • removed the old for-loop annotation fake_node target swapping path
  • made visit_For build the synthetic AnnAssign directly from the real
    loop target and parsed annotation span
  • added unit coverage for the synthesized AnnAssign.node_source_code
    value 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_For then constructs the final python_ast.AnnAssign explicitly from
node.target plus that parsed annotation, instead of mutating a temporary
parsed 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.py
  • git diff --check
  • isolated smoke check confirmed the synthesized for-loop target reports
    node_source_code == "i: uint256"
  • python -m pytest tests/unit/ast/test_annotate_and_optimize_ast.py -q
    is currently blocked in this environment because pytest is not installed

Commit message

The parser previously handled annotated for-loop targets by parsing a dummy
AnnAssign and then swapping its target in place. That left source-range
metadata 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_For now builds the final AnnAssign
directly 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_code aligned with the original source for for-loop
annotations.

Description for the changelog

Improve parser handling for annotated for-loop targets and preserve their
source spans.

Cute Animal Picture

Cute animal picture


Pull Request opened by Augment Code | View session

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.
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.

1 participant