Improve error messages for invalid `shelve` datum
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 77.2k
- Forks
- 35.9k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con Lib/shelve.py en la línea 138 y reproduce el ejemplo proporcionado de shelve/dbm.gnu para rastrear por qué un valor serializado no válido produce el error engañoso. Actualiza el texto del mensaje para distinguir las claves de los valores e incluir el tipo recibido; se considera terminado cuando el ejemplo identifica claramente el valor no válido.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- databases
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 25/100