Preserve Box subclass for nested boxes after unpickling#309
Open
koriyoshi2041 wants to merge 1 commit into
Open
Preserve Box subclass for nested boxes after unpickling#309koriyoshi2041 wants to merge 1 commit into
koriyoshi2041 wants to merge 1 commit into
Conversation
Box.__init__ derives box_class from self.__class__, but Box.__new__ hardcoded the base Box. Unpickling calls __new__ + __setstate__ (not __init__), so a subclass instance ended up with box_class=Box, and nested boxes created on access after unpickling became base Box instead of the subclass. Use cls in __new__ to match __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.
Fixes #308.
A
Boxsubclass is lost on nested attribute access after unpickling: nested boxes come back as the basebox.box.Boxinstead of the subclass.Box.__init__setsbox_classfromself.__class__:but
Box.__new__hardcodes the baseBox:Unpickling calls
__new__+__setstate__, not__init__, so a subclass instance is restored withbox_class=Box. Nested dicts converted to boxes on access (conversion box) then usebox_class, producing baseBoxobjects. Usingclsin__new__(which is the actual subclass) matches__init__.Added
test_pickle_preserves_subclass_on_nested_access: pickles aBoxsubclass with a nested dict and asserts the nested box is still the subclass after a round-trip. It fails onmaster(nested box is baseBox) and passes with the fix; existing pickle tests still pass. (The subclass is defined at module level so its instances are picklable.)