Preserve box options in BoxList.__deepcopy__#319
Open
chuenchen309 wants to merge 1 commit into
Open
Conversation
`BoxList.__deepcopy__` built the copy as `self.__class__()` with no `box_options`, so `copy.deepcopy()` silently dropped every option (`frozen_box`, `box_dots`, `default_box`, `box_intact_types`, …). A frozen BoxList became mutable (and unhashable) after a deepcopy. `BoxList.__copy__` and `Box.__deepcopy__` both preserve options; `BoxList.__deepcopy__` was the outlier. Mirror `Box.__deepcopy__`: build unfrozen, register in memo before recursing (keeps cycle-safety), populate, then re-freeze via `__init__`.
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.
BoxList.__deepcopy__builds the copy asself.__class__()with nobox_options, socopy.deepcopy()silently drops every option:It's a three-way inconsistency:
BoxList.__copy__preserves options,Box.__deepcopy__preserves options (and re-appliesfrozen), butBoxList.__deepcopy__does neither. It's also reachable indirectly —copy.deepcopyof aBoxthat contains a nestedBoxListloses that BoxList's options.Fix (
box/box_list.py): mirrorBox.__deepcopy__— build the copy unfrozen (soappendworks while populating), register it inmemobefore recursing (preserves the existing self-reference/cycle safety), populate, then re-freeze via__init__.Verification (re-runnable from the diff): added
test_deepcopy_preserves_box_options— it checkscopy.deepcopypreservesfrozen_box/box_dots/default_box/box_intact_types(parity withcopy.copy), and that a deepcopied frozen BoxList still raises onappendand hashes equal. It fails on the current tree and passes with the fix.test/test_box_list.pygoes from 18 to 19 passing with no new failures (the 2 pre-existing failures aretest_toon_*, an optionaltoondep not installed — identical before and after).Disclosure: This PR was authored by an AI coding agent (Claude Code) running on this account: the AI found the bug, ran the repro, wrote the test, and wrote this description. The human account holder reviews every change and is accountable for it. The verification above is real and re-runnable from the diff. If this isn't the kind of contribution you want, say so and I'll close it.