fix: preserve existing keys on dotted list-element writes#310
Open
gaoflow wants to merge 1 commit into
Open
Conversation
BoxList.__setitem__ unconditionally replaced the element at the target index with a fresh empty container before recursing, so a second dotted write into an existing element (a[0].y after a[0].x) discarded the keys already stored there. Only replace the element when it is not already the right container type, matching the guard in Box.__setitem__.
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
With
default_box=Trueandbox_dots=True, writing a second dotted key into an already-existing list element silently discards the keys already written there:The write succeeds but drops previously-stored data — a silent correctness/data-loss bug. (Different indices like
a[0].xthena[1].yare fine; the bug is a second write to the same index.)Cause
BoxList.__setitem__(box/box_list.py), in thedefault_boxbranch, unconditionally replaces the element at the target index with a fresh emptyBox/BoxListbefore recursing into it — even when a populated container already lives there. So the second write toa[0]throws away theBox({'x': 1})already stored and recurses into a brand-new empty one.The sibling
Box.__setitem__already guards this with an "is it already the right container?" check; theBoxListpath was missing it.Fix
Read the current element first and only replace it when it isn't already the right container type. A scalar (or
Noneplaceholder from list extension) is still correctly overwritten; an existingBox/BoxListis preserved and recursed into.Added regression coverage to
test_box_list_default_dots(two keys into one element, nested list-in-list, and the scalar-overwrite case). Full suite: 154 passed; the 5test_toon_*failures are pre-existing (aNotImplementedError/BoxErrorfrom the third-partytoon_formatpackage) and reproduce identically on a cleanmaster.black --config=.black.toml --checkclean.This pull request was prepared with the assistance of AI, under my direction and review.