Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Unpickling child class fails if base class __init_subclass__ expects an argument

Đang mở
#527 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
42/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu với reproducer trong issue và kiểm tra cloudpickle.py tại _make_skeleton_class, nơi traceback cho thấy lớp con Child được tạo lại. Theo dõi cách các đối số từ khóa của định nghĩa lớp được xử lý trong quá trình serialization và xác minh bản sửa bằng cách chạy lại ví dụ: cloudpickle phải unpickle Child thành công trong khi vẫn giữ nguyên giá trị arg của nó.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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'
Ngôn ngữ chính
Python
Star
1.9k
Fork
198
Merge trung bình
1 ngày 10 giờ
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của cloudpipe/cloudpickle

Tất cả issue của cloudpipe/cloudpickle

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.