Support pickling cached_property descriptors on Python 3.8-3.11 - #605
Open
charan-rathore wants to merge 1 commit into
Open
charan-rathore wants to merge 1 commit into
charan-rathore wants to merge 1 commit into
Conversation
This branch has not been deployed
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 #438.
On Python before 3.12, functools.cached_property stores an RLock. Pickling a dynamically defined class or instance with one of these properties fails because the lock cannot be pickled.
Add a reducer that reconstructs the descriptor from its function and restores its attributes, leaving the constructor to create a new lock. This follows pierreglaser's suggestion in the issue to add a minimal reducer. Cached results belong to the instance dictionary and continue to travel with the instance.
The reducer keeps the assigned attribute name, documentation and custom attributes. On Python 3.12 and newer, cached_property no longer creates a lock, so a custom attribute named lock is preserved rather than removed.
Tests cover classes in a fresh process, instances before and after computing a cached result, standalone descriptors, repeated references and descriptors not yet assigned to a class. Both default and protocol-2 test classes run them.
Verification: Python 3.10 and 3.11 - the 8 new tests fail on master and the patched full suite passes (3.10: 281 passed, 3.11: 276 passed). On Python 3.12 to 3.14 the new tests already pass on master and the patched suites pass. Python 3.8 and 3.9 were not tested. These are same-version roundtrips; cloudpickle expects the communicating processes to run the same Python version. Only the exact
functools.cached_propertyis handled, not subclasses.