Skip to content

fix(BoxList): copy() returns a BoxList instead of a plain list - #362

Open
waseem1302-x wants to merge 5 commits into
cdgriffith:developfrom
waseem1302-x:fix/boxlist-copy
Open

waseem1302-x wants to merge 5 commits into
cdgriffith:developfrom
waseem1302-x:fix/boxlist-copy

Conversation

@waseem1302-x

Copy link
Copy Markdown

Hi, I'm Waseem Mushtaq, a final-year Computer Science undergraduate at Albukhary International University (AIU) in Malaysia on a full scholarship. I'm working on Python backend engineering, and I found this while using Box.

Bug

BoxList.copy() returns a plain list instead of a BoxList. Box.copy() returns a Box, and copy.copy() on a BoxList returns a BoxList, so only the .copy() method drops the type. The copy loses its box_options (box_dots, frozen_box, box_class) and its item comments. Worst case, a frozen BoxList's .copy() returns a mutable plain list, so frozen.copy().append(3) succeeds.

from box import BoxList
bl = BoxList([{"a": 1}, 2], box_dots=True)
type(bl.copy())  # list, expected BoxList
bl.copy()["[0].a"]  # TypeError: list indices must be integers or slices, not str
BoxList([1, 2], frozen_box=True).copy().append(3)  # succeeds, expected BoxError

This is the same class of bug fixed in 8.0.1 for BoxList + and * returning a plain list.

Fix

Added BoxList.copy() with the logic previously in __copy__ (build self.__class__ with box_options, copy item comments), and __copy__ now delegates to it, mirroring Box. Also added the copy() declaration to box_list.pyi.

Tests

  • test_copy_returns_a_box_list: type is BoxList, box_dots preserved, dotted lookup works
  • test_copy_keeps_item_comments
  • test_copy_of_frozen_box_list_stays_frozen: type, options, mutation refused, hash equal

The new tests fail without the fix and pass with it. Full suite: 1227 passed, 1 skipped. ruff check, ruff format --check, and mypy box are clean.

Thanks!

This branch has not been deployed

No deployments
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