Skip to content

Preserve explicit subclass overrides when pickling dynamic classes - #600

Open
soodoku wants to merge 2 commits into
cloudpipe:masterfrom
gojiplus:fix/584-preserve-subclass-overrides
Open

soodoku wants to merge 2 commits into
cloudpipe:masterfrom
gojiplus:fix/584-preserve-subclass-overrides

Conversation

@soodoku

@soodoku soodoku commented Sep 11, 2026 •

Copy link
Copy Markdown

When a subclass explicitly assigns the same object as a base attribute, cloudpickle drops that assignment from the serialized class state. In a fresh process, updating the base then changes the subclass too, despite its explicit override. Fixes #584.

The class state now keeps each explicit assignment. When a value is identical to an attribute of a direct base, it records which base supplies that value and restores a child-owned binding on load. This avoids serializing the aliased value as part of the child state and allows aliases to unpicklable base attributes, such as a threading.Lock, to roundtrip. For an importable base whose attribute changes before loading, the restored binding uses the base's current value, consistent with loading that base by reference. If the class already exists in cloudpickle's dynamic class tracker, its existing child-owned binding is retained.

Class construction already sets __module__, so state restoration skips a redundant assignment of that attribute. This avoids triggering custom metaclass setters twice while still restoring a module value that changed on an existing tracked class.

Reproducer (run as a script):

import subprocess
import sys
import cloudpickle

class Parent:
    version = 1

class Child(Parent):
    version = Parent.version

payload = cloudpickle.dumps((Parent, Child))
Parent.version = 2
assert Child.version == 1

output = subprocess.check_output(
    [sys.executable, "-c", """
import sys
import cloudpickle
Parent, Child = cloudpickle.loads(sys.stdin.buffer.read())
Parent.version = 2
print(Child.version)
"""],
    input=payload,
)
assert output == b"1\n", output

On upstream 4af5936, the final assertion fails with b"2\n". It passes with this fix.

Regression tests cover constants and methods, single and multiple inheritance, protocols 2 and 5, an importable base with an unpicklable lock, an importable base changed before loading, and a class already present in the dynamic class tracker. Reverting the new base-attribute reference code makes the lock regression fail for both protocols.

Local validation on Python 3.14.7: 280 passed, 12 skipped, 2 xfailed. Ruff passes across the repository; Black passes on all changed files. The notebook tests used a temporary kernel spec for the local Python installation.

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.

Override attributes are not pickled correctly

1 participant