Skip to content

Preserve explicit subclass overrides when pickling dynamic classes - #600

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

Preserve explicit subclass overrides when pickling dynamic classes#600
soodoku wants to merge 1 commit 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 removes 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.

Preserve the class's own dictionary entries. Python already excludes merely inherited attributes from this dictionary; sharing an object with a base attribute does not make an explicit assignment redundant. The subprocess tests cover constants and methods, single and multiple inheritance, and protocols 2 and 5. They start a worker before defining the classes so class tracking cannot mask the failure.

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. Both cases have regression tests.

Reproducer (run as a script):

import subprocess
import sys
import cloudpickle

class Parent:
    version = 1

class Child(Parent):
    version = 1

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.

Validation on macOS:

  • Python 3.12.11: baseline 266 passed, 10 skipped, 2 xfailed; candidate 274 passed, 10 skipped, 2 xfailed.
  • Python 3.14.7: baseline 264 passed, 12 skipped, 2 xfailed; candidate 272 passed, 12 skipped, 2 xfailed.
  • Reverting only the override-preservation change makes its four regression cases fail; removing only the module-assignment guard makes both metaclass cases fail. The module-restoration tests also reject an unconditional omission of __module__. All eight new cases pass with the final fix.
  • Independent review by a different model in an isolated checkout found no remaining correctness issues after checking the metaclass and class-tracker cases. It independently ran the full suites and confirmed the regression failures with the fixes removed.
  • All configured pre-commit hooks pass on the changed files. Ruff 0.14.3 passes across cloudpickle and tests.
  • python -m build builds the source distribution and a wheel from it; twine check --strict dist/* passes. A fresh environment using the installed wheel, outside the source checkout, passes 274 tests, with 10 skipped and 2 xfailed. The README's functional examples and the reproducer pass against that wheel.

Full suites use PYTHONPATH='.:tests' python -m pytest -q with the development dependencies plus NumPy and SciPy. The wheel suite uses a separate copy of the tests because the existing source distribution omits them (#555).

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