Skip to content

Preserve Box subclass as default nested box_class during pickle#316

Draft
Shevanio wants to merge 1 commit into
cdgriffith:masterfrom
Shevanio:agent/preserve-box-subclass-as-default-nested-
Draft

Preserve Box subclass as default nested box_class during pickle#316
Shevanio wants to merge 1 commit into
cdgriffith:masterfrom
Shevanio:agent/preserve-box-subclass-as-default-nested-

Conversation

@Shevanio

@Shevanio Shevanio commented Jul 8, 2026

Copy link
Copy Markdown

Context

The Box class implementation needs to support preserving its subclass during the pickling and unpickling process. This is essential for maintaining the integrity of nested Box subclasses when handling serialized data.

Solution

  1. Updated the Box.__new__ initialization in box/box.py to set the default box_class using cls instead of the concrete Box class, which allows subclasses to wrap nested dictionary values recursively with their own types.
  2. Ensured that explicitly provided box_class values still take precedence over the new default configuration to maintain the behavior for existing clients.
  3. Added a regression test in test/test_box.py with a subclass to verify that pickled and unpickled objects retain their subclass instances, using pickle.HIGHEST_PROTOCOL for testing.

Scope

This change impacts the Box class in box/box.py and its behavior in serialization contexts. Regression tests ensure that the functionality remains intact, and new behavior is corroborated.

Tests Run

160 tests were executed during the validation process.

Results

All tests passed successfully, confirming that the implementation is working as intended and does not introduce any regressions.

Risks

Low risk of breakage given the thorough testing and preserving of existing behaviors in the codebase.

Related Issue

This PR addresses the issue of subclass pickling in the Box class and aligns with enhancement requests on the repository regarding maintaining subclass integrity.

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