Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Unpickling child class fails if base class __init_subclass__ expects an argument

未关闭
#527 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
42/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python
领域
tooling

调研方向

从 issue 中的复现程序开始,检查 cloudpickle.py 中的 _make_skeleton_class,traceback 显示子类 Child 在此处被重新创建。跟踪序列化过程中类定义关键字参数的处理方式,并通过重新运行示例来验证修复:cloudpickle 应能成功对 Child 进行 unpickle,同时保留其 arg 值。

由索引模型根据 Issue 内容生成。

描述

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 and the original PEP 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
<class '__main__.Base'>
<class '__main__.Child'>
<class '__main__.Base'>
Traceback (most recent call last):
  File "/home/tsanders/test_cloudpickle_bug.py", line 20, in <module>
    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'
主要语言
Python
星标
1.9k
派生
212
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

cloudpipe/cloudpickle 的其他 Issue

查看 cloudpipe/cloudpickle 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。