python / python/cpython

Improve error messages for invalid `shelve` datum

Open
#137,899 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Lib/shelve.py at line 138 and reproduce the supplied shelve/dbm.gnu example to trace why an invalid serialized value produces the misleading error. Update the reported wording to distinguish keys from values and include the received type; done means the example clearly identifies the invalid value.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
databases
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.