Feature request: flag attributes set in `__init__` but not `__setstate__`
まだ誰も着手していません。
評価
調査の方向性
まず mypy --strict x.py を使って x.py の例を再現し、次に mypy が初期化メソッドとシリアライズメソッドをどのようにモデル化しているかを調べます。__setstate__ に示されている不足している代入によって適切な診断が生成され、かつ有効な一時属性が引き続きサポートされれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Feature
If an instance attribute is defined in __init__ but its definition is missing from __setstate__, this could cause a crash when an object is deserialized and the attribute is accessed.
Pitch
import pickle
class A:
"""
Attribute `a` is persistent and serialized.
Attribute `b` is temporary and not serialized.
"""
def __init__(self) -> None:
self.a: int = 1
self.b: int = 2
def sum(self) -> int:
return self.a + self.b
def __getstate__(self) -> tuple[int]:
return (self.a,)
def __setstate__(self, state: tuple[int]) -> None:
self.a = state[0]
# BUG: self.b is not set
if __name__ == '__main__':
a = A()
print(a.sum())
b = pickle.loads(pickle.dumps(a))
print(b.sum()) # AttributeError
$ mypy --version
mypy 1.4.1 (compiled: yes)
$ mypy --strict x.py
Success: no issues found in 1 source file
This bug pattern has bitten me multiple times. Especially in cases where serialization is rare and the test cases that stress class interfaces are not performing deserialization, this bug can make it into releases undetected. This seems like a great candidate for a bug to catch with static analysis. It seems appropriate for mypy because it is in some sense a type bug - if we're using the type model where __init__ is supposed to fully initialize a typed interface, then __setstate__ should do the same.
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- 平均マージ
- 1日 18時間
- マージ済み PR(30日)
- 54
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/mypy のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
bug topic-configuration topic-error-reporting
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
似ている issue
-
area/auth bug comp/agent P3 platform/discord type/security
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
NousResearch/hermes-agent#117848 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
bancolombia/sentinel#23 ·
-
test md オープンCI
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
langchain-ai/deepagents#6450 ·
-
bug client
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100