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

オープン
#96,224 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
38/100
issue の種類
ドキュメント
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python
領域
documentation

調査の方向性

getstatesetstate、およびアンピクル化の動作を説明しているドキュメントのセクションから始めます。タプル形式の状態が受け入れられることを明確にし、動的な状態に対する dict の直接更新と、スロット化された状態に対する setattr 呼び出しを区別し、既存の曖昧な注記に頼らずに関連する動作を説明します。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
77.2k
フォーク
36k
平均マージ
1日 9時間
マージ済み PR(30日)
558

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。