cloudpipe / cloudpipe/cloudpickle

Unpickling child class fails if base class __init_subclass__ expects an argument

Open
#527 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1.9k
Forks
195
Avg merge
1d 10h
Merged PRs (30d)
1

Description

If you have a base class that defines `__init_subclass__` with an argument, unpickling a child class will fail because `cloudpickle` does not pass along the original kwarg when re-creating the class.

This case seems to work fine in the built-in `pickle` package.

This usage mirrors examples shown in the [docs](https://docs.python.org/3/reference/datamodel.html#object.__init_subclass__) and the original [PEP](https://peps.python.org/pep-0487/) that introduced the `__init_subclass__` method.

Python 3.11.4
cloudpickle 2.2.1
**EDIT:** This also appears to be broken with cloudpickle 3.0.0.

Example:
```
#!/usr/bin/env python3
import pickle
import cloudpickle
print(cloudpickle.__version__)

class Base:
def __init_subclass__(cls, /, arg, **kwargs):
super().__init_subclass__(**kwargs)
cls.arg = arg

class Child(Base, arg='hello'):
pass

print(pickle.loads(pickle.dumps(Base)))
print(pickle.loads(pickle.dumps(Child)))
print(cloudpickle.loads(cloudpickle.dumps(Base)))
print(cloudpickle.loads(cloudpickle.dumps(Child)))
```
```
2.2.1

Traceback (most recent call last):
File "/home/tsanders/test_cloudpickle_bug.py", line 20, in
print(cloudpickle.loads(cloudpickle.dumps(Child)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/envs/production-3.11/lib/python3.11/site-packages/cloudpickle/cloudpickle.py", line 827, in _make_skeleton_class
skeleton_class = types.new_class(
^^^^^^^^^^^^^^^^
File "/opt/conda/envs/production-3.11/lib/python3.11/types.py", line 75, in new_class
return meta(name, resolved_bases, ns, **kwds)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Base.__init_subclass__() missing 1 required positional argument: 'arg'
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.