Unpickling child class fails if base class __init_subclass__ expects an argument
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
cloudpipe/cloudpickle 的其他 Issue
-
Bug: unguarded dict.pop() in _function_setstate causes KeyError crash on crafted pickle (regression from c6f8cd4)可能已有人在做 @soodoku 于 88 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
cloudpipe/cloudpickle#593 · 1 条评论 ·
维护者通常 1 天内回复
-
Globals read only inside a class body defined in a by-value function are not pickled (NameError after loading)可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 4/5 3-5 天 新手友好度 55/100
cloudpipe/cloudpickle#609 · 1 条评论 ·
维护者通常 1 天内回复
-
A class pickled by value before its annotations are read loses them.可能已有人在做 @pralav-25 于 15 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 67/100
cloudpipe/cloudpickle#608 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 42/100
cloudpipe/cloudpickle#595 ·
维护者通常 1 天内回复
-
cloudpickle is not compatible with PyPy 7.3.22可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 3/5 1-2 天 新手友好度 65/100
cloudpipe/cloudpickle#592 · 2 条评论 ·
维护者通常 1 天内回复
查看 cloudpipe/cloudpickle 的全部 Issue
相似的 Issue
-
style: article/section 📝 system: cloud.rc ☁️ system: security 🔐
难度 2/5 1-3 小时 新手友好度 65/100
uabrc/uabrc.github.io#1262 ·
-
documentation
难度 2/5 1-3 小时 新手友好度 70/100
NCAR/miles-credit#526 ·
-
documentation
难度 1/5 1-3 小时 新手友好度 76/100
conda/conda-lock#957 ·
维护者通常 1 天内回复
-
Type: Bug
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
aws-samples/sample-per-model-bedrock#464 ·
维护者通常 1 天内回复