Skip to content

Support pickling cached_property descriptors on Python 3.8-3.11 - #605

Open
charan-rathore wants to merge 1 commit into
cloudpipe:masterfrom
charan-rathore:fix-cached-property-pickling
Open

charan-rathore wants to merge 1 commit into
cloudpipe:masterfrom
charan-rathore:fix-cached-property-pickling

Conversation

@charan-rathore

Copy link
Copy Markdown

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_property is handled, not subclasses.

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.

Classes with cached_property can't be cloudpickled

1 participant