This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Should shelve support dict union?
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Dominik V., brandtbucher, lukasz.langa, miss-islington, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2021-03-09 08:46 by Dominik V., last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 27004 merged serhiy.storchaka, 2021-07-03 14:36
PR 27369 merged miss-islington, 2021-07-26 16:11
PR 27370 merged miss-islington, 2021-07-26 16:11
Messages (10)
msg388335 - (view) Author: Dominik Vilsmeier (Dominik V.) * Date: 2021-03-09 08:46
The docs of shelve mention that

> Shelf objects support all methods supported by dictionaries. This eases the transition from dictionary based scripts to those requiring persistent storage.

However the `|=` operator is not implemented, preventing a seamless transition from `dict` to `shelve`. So should this be implemented for `Shelf` as well? `|` on the other hand doesn't make much sense.

Otherwise the docs could be updated.
msg388338 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-03-09 09:49
The comment is outdated. Shelf objects also do not support methods copy and fromkey. Creating a new Shelve object without specifying a new underlying database object does not make much sense.

Maybe say that they implement the MutableMapping interface?

>>> sorted(set(dir(dict)) - set(dir(shelve.Shelf)))
['__ior__', '__or__', '__ror__', 'copy', 'fromkeys']
>>> sorted(set(dir(collections.abc.MutableMapping)) - set(dir(shelve.Shelf)))
[]
msg388342 - (view) Author: Dominik Vilsmeier (Dominik V.) * Date: 2021-03-09 10:21
Right, that seems like a good idea. But `__ior__` could be implemented nevertheless?
msg388388 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-03-09 21:39
> Maybe say that they implement the MutableMapping interface?

Yes, that's a good idea (also add link to the MutableMapping docs).

> Right, that seems like a good idea. But `__ior__` could
> be implemented nevertheless?

Off-hand, I don't see why not.

Adding Brandt to the nosy list to see what he thinks.
msg388389 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-03-09 21:43
One other thought.  While __ior__() could be used as a short-cut for update(), the related __or__() method is more problematic because it would need to create a new underlying DB.  So maybe this is a can worms that we should not open. It would be weird to have __ior__() but not __or__().
msg388394 - (view) Author: Brandt Bucher (brandtbucher) * (Python committer) Date: 2021-03-09 23:09
+1 for the MutableMapping comment.

We purposely omitted shelve when determining what classes should grow the new operators. Guido's thoughts:

> I definitely think we should leave Shelf alone, it's a toy class from a different era.

(https://bugs.python.org/msg364196)

I personally have no experience with shelve, so I'd rather defer to the judgement of others here.
msg388395 - (view) Author: Dominik Vilsmeier (Dominik V.) * Date: 2021-03-09 23:35
It's true, having `__ior__` but not `__or__` would probably be weird. In the end it's just "nice to have", but I'm not even sure that this applies. Calling `db.update(...)` is still more explicit than `db |= ...`.  The docs mention that

> This eases the transition from dictionary based scripts to those requiring persistent storage.

For my use cases, however, I always knew right from the beginning that I want object persistence between different runs of a script (e.g. for data analysis, caching the expensive results), so it was always clear that I'm working with a Shelf object and not a dict (i.e. no expectations on the availability of `|=`).

Primarily, this issue was meant to point out the mismatch of docs/implementation and not to get `|=` implemented for `Shelf`. In the end, I think updating the docs is all that is needed.
msg398242 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-26 16:11
New changeset c97c2a050cf753003012ae3f08e035326b8f6167 by Serhiy Storchaka in branch 'main':
bpo-43443: Clarify difference between shelve objects and dicts (GH-27004)
https://github.com/python/cpython/commit/c97c2a050cf753003012ae3f08e035326b8f6167
msg398252 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-26 19:32
New changeset 563e05743c8f31366db7ef35a00e396c9f442b17 by Miss Islington (bot) in branch '3.10':
bpo-43443: Clarify difference between shelve objects and dicts (GH-27004) (GH-27369)
https://github.com/python/cpython/commit/563e05743c8f31366db7ef35a00e396c9f442b17
msg398253 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-26 19:32
New changeset 7482fff297727bb5faf64cc92c73561ccf48feda by Miss Islington (bot) in branch '3.9':
bpo-43443: Clarify difference between shelve objects and dicts (GH-27004) (GH-27370)
https://github.com/python/cpython/commit/7482fff297727bb5faf64cc92c73561ccf48feda
History
Date User Action Args
2022-04-11 14:59:42adminsetgithub: 87609
2021-07-26 19:33:20lukasz.langasetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.11
2021-07-26 19:32:52lukasz.langasetmessages: + msg398253
2021-07-26 19:32:39lukasz.langasetmessages: + msg398252
2021-07-26 16:11:41miss-islingtonsetpull_requests: + pull_request25908
2021-07-26 16:11:36miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request25907
2021-07-26 16:11:35lukasz.langasetnosy: + lukasz.langa
messages: + msg398242
2021-07-03 14:36:05serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request25564
2021-03-09 23:35:33Dominik V.setmessages: + msg388395
2021-03-09 23:09:20brandtbuchersetmessages: + msg388394
2021-03-09 21:43:12rhettingersetmessages: + msg388389
2021-03-09 21:39:43rhettingersetnosy: + brandtbucher
messages: + msg388388
2021-03-09 10:21:34Dominik V.setmessages: + msg388342
2021-03-09 09:49:39serhiy.storchakasetnosy: + rhettinger, serhiy.storchaka
messages: + msg388338
2021-03-09 08:46:35Dominik V.create