Feature request: flag attributes set in `__init__` but not `__setstate__`
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
Direzione di ricerca
Inizia riproducendo l’esempio in x.py con mypy --strict x.py, quindi esamina come mypy modella i metodi di inizializzazione e serializzazione. Il lavoro sarà considerato completato quando l’assegnazione mancante mostrata in __setstate__ produrrà una diagnosi appropriata, continuando al contempo a supportare gli attributi temporanei validi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Merge medio
- 1g 18h
- PR unite (30g)
- 54
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python/mypy
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
bug topic-configuration topic-error-reporting
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
bancolombia/sentinel#23 ·
-
test md ApertaCI
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100