Preserve end_line/end_column/end_pos when copying or pickling a Token#1611
Open
gaoflow wants to merge 1 commit into
Open
Preserve end_line/end_column/end_pos when copying or pickling a Token#1611gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
Token.__reduce__ and Token.__deepcopy__ passed only the first five positional attributes (type, value, start_pos, line, column) to the constructor, so copy.copy, copy.deepcopy and pickle round-trips silently reset end_line, end_column and end_pos to None. new_borrow_pos already carries all eight attributes; these two methods predate the end_* fields and were never updated to match.
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.
Summary
Token.__reduce__(used bycopy.copyandpickle) andToken.__deepcopy__only forward the first five positional attributes to the constructor:The constructor signature is
(type, value, start_pos, line, column, end_line, end_column, end_pos), soend_line,end_columnandend_posare silently reset toNoneon everycopy.copy,copy.deepcopy, andpickleround-trip:This is inconsistent with
Token.new_borrow_pos(and thereforeupdate), which already carries all eight attributes. These two methods predate theend_*attributes and were never updated to match.Fix
Forward the three trailing position attributes in both methods, matching
new_borrow_posand the constructor's positional order. Four characters of behavior change, two lines.Verification
master(c169b26):deepcopy/copy/pickleresetend_*toNone; with the fix all eight attributes survive.test_token_copy_preserves_end_positiontotests/test_lexer.py; it fails before the fix and passes after.mypy lark/lexer.pyis identical before and after (no new errors).This pull request was prepared with the assistance of AI, under my direction and review.