Preserve node syntax when printing data class edits - #162
Merged
Merged
Conversation
`DataClassEdit` had no `print()` override, so an edited `DataClassNode` fell back to `AbstractCompoundEdit.print`, which prints each slot edit back to back. That drops whatever syntax the node's formatter writes between the slots: an edited `Assignment` lost its ` = `, an edited `PyObjAttribute` lost its dots, and `x = foo(1, 2)` against `x = foo(1, 2, 3)` rendered as `[x]foo[1,2,++3++]`. `SequenceEdit`, the other container edit type, already hands control back to the node's formatter, which writes the syntax and reaches the sub-edits as it prints each child. `DataClassEdit` now does the same. A `print_DataClassEdit` formatter method would be the wrong place for this, because the syntax and the per-child edits both live in the node formatter. `get_formatter` always resolves, because `DataClassNode` is a `ContainerNode` and formatters find `print_ContainerNode` through the MRO. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GypKU5KdLfs2Cf8kS2TzJa
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.
Closes #150
DataClassEdithad noprint()override, so an editedDataClassNodewas rendered byAbstractCompoundEdit.print, which prints each slot edit back to back. That drops whatever syntax thenode's formatter writes between the slots.
SequenceEdit, the other container edit type, already hands control back to the node's formatter, whichwrites the syntax and reaches the sub-edits as it prints each child.
DataClassEditnow does the same.This affects any
DataClassNodewhose rendering needs punctuation between its slots, so it is aprerequisite for correct diff rendering in #151 and #154.
A
print_DataClassEditformatter method would be the wrong place for this:print_StringEditis the onlyprint_<Edit>formatter in the package, and it exists because a character-level string diff has nonode-level rendering.
get_formatteralways resolves here, becauseDataClassNodeis aContainerNodeand formatters find
print_ContainerNodethrough the MRO.x = foo(1, 2)->x = foo(1, 2, 3)[x]foo[1,2,++3++]x = foo(1, 2, ++3++)x = foo(1, 2)->y = foo(1, 2)[x -> y]foo[1,2]x -> y = foo(1, 2)x = foo(1, 2)->x = bar(1, 2)[x]x = ~~foo~~++bar++(1, 2)x = ~~foo~~++bar++(1, 2)x = a.b.c->x = a.b.d[x]abc -> dx = a.b.c -> dValidation
test/test_pydiff.pycoverAssignment,Call, andPyObjAttribute. Both failagainst the unfixed code with the "Before" strings above, and pass with the fix.
ruff check graphtage test docs bindist: passes.pytest: 142 passed.make -C docs html SPHINXOPTS="-W --keep-going": build succeeded.🤖 Generated with Claude Code
https://claude.ai/code/session_01GypKU5KdLfs2Cf8kS2TzJa