Improve error messages for invalid `shelve` datum
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Feature or enhancement
Proposal:
In https://github.com/python/cpython/issues/137829, I got confused because of the following:
import shelve
import dbm.gnu
db = dbm.gnu.open("myshelf", "c")
db.close()
def serializer(obj, protocol=None):
pass
def deserializer(data):
return data.decode("utf-8")
with shelve.open("myshelf", serializer=serializer, deserializer=deserializer) as s:
s["foo"] = "bar"
The traceback is
Traceback (most recent call last):
File "/home/arf/Desktop/source-codes/cpython/repro.py", line 17, in <module>
s["foo"] = "bar"
~^^^^^^^
File "/home/arf/Desktop/source-codes/cpython/Lib/shelve.py", line 138, in __setitem__
self.dict[key.encode(self.keyencoding)] = serialized_value
~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: gdbm mappings have bytes or string indices only
I got confused because I thought that the issue stemmed from the key (because of the word "indices") but the error is raised due to the serialized_value type as serializer returns None. IOW, s["foo"] = "bar" above is equivalent to s["foo"] = None. I suggest:
- changing the error message when the value type is incorrect (namely, don't talk about "indices"). Ideally, we should use "key" and "value" to distinguish the messages.
- add some "must be [...], not [...]" with the input type.
cc @furkanonder
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
- gh-138768
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.
Direzione di ricerca
Inizia da Lib/shelve.py alla riga 138 e riproduci l’esempio fornito di shelve/dbm.gnu per capire perché un valore serializzato non valido produca l’errore fuorviante. Aggiorna il testo riportato per distinguere le chiavi dai valori e includere il tipo ricevuto; il lavoro è completato quando l’esempio identifica chiaramente il valore non valido.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- databases
- Tipo di issue
- Funzionalità
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 25/100