Expanding on the default unpickling behavior of objects that do not implement __setstate__

Đang mở
#96,224 0 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
38/100
Loại issue
Tài liệu
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
documentation

Hướng nghiên cứu

Bắt đầu từ phần tài liệu giải thích getstate, setstate và hành vi khi unpickling. Làm rõ rằng các state dạng tuple được chấp nhận, phân biệt việc cập nhật trực tiếp dict cho state động với các lần gọi setattr cho state sử dụng slots, và giải thích hành vi liên quan mà không dựa vào ghi chú mơ hồ hiện có.

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

Mô tả

docs

Documentation

I'm about to open a PR for it myself but I wanted to also open a related issue (just in case there are any objections)
Ever since Python 3.11, __getstate__'s default implementation has been added to object. With that change, the section explaining __getstate__ was expanded to also explain the default implementation in regards to slotted objects and the fact that their default state is a tuple of dicts (1 for the slotted dict and 1 optional dynamic dict) .

With this change, the user is now aware of the new concept of the "slotted state" and therefore should be aware of how it is handled when __setstate__ was not implemented. The current documentation states that the user may choose to implement __getstate__ and not __setstate__ so long as the returned state was a dict.

This needs to be updated to explain it also accepts tuples of the former format. Moreover, it should be updated to explain how do these 2 dicts differ in their setting behavior:

  1. The dynamic dict MUST be set using direct edit of the instance's __dict__ (in order to not activate __setattr__ which is very important, especially on immutable instances that will throw an exception on such calls). This is behavior is similar to pure dict states (and should probably be documented as well)
  2. The slotted dict MUST be set using calls to setattr (since they by definition do not exist on the instance __dict__ and therefore cannot be directly edited. Also we want to avoid accidentally a duplicate value on that dict)

Explaining this behavior will be more detailed and helpful than the current note that vaguely states:

At unpickling time, some methods like __getattr__(), __getattribute__(), or __setattr__() may be called upon the instance. 
In case those methods rely on some internal invariant being true, the type should implement __new__() to establish such an invariant, 
as __init__() is not called when unpickling an instance.

Explaining it will also prevent users from mistakenly relying on __setattr__ when they shouldn't. This will also prevents libraries like jsonpickle from incorrectly implementing their unpickling behavior to always use setattr (which causes bugs that arise from discrepancies with the default pickle library.

Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Merge trung bình
1 ngày 9 giờ
Pull request đã merge (30 ngày)
558

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

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 python/cpython

Tất cả issue của python/cpython

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.