Issue 33385: setdefault() with a single argument doesn't work for dbm.gnu and dmb.dumb objects (original) (raw)
This issue has been migrated to GitHub: https://github.com/python/cpython/issues/77566
classification
Title: | setdefault() with a single argument doesn't work for dbm.gnu and dmb.dumb objects | ||
---|---|---|---|
Type: | enhancement | Stage: | |
Components: | Library (Lib) | Versions: | Python 3.8 |
process
Status: | open | Resolution: | |
---|---|---|---|
Dependencies: | Superseder: | ||
Assigned To: | Nosy List: | ned.deily, rhettinger, serhiy.storchaka | |
Priority: | normal | Keywords: |
Created on 2018-04-29 14:00 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin.
Messages (4) | ||
---|---|---|
msg315898 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * ![]() |
Date: 2018-04-29 14:00 |
setdefault() is not implemented directly in dbm.gdbm and dmb.ndbm database classes. It is inherited from MutableMapping: def setdefault(self, key, default=None): try: return self[key] except KeyError: self[key] = default return default But since assigning is supported only for bytes and str, setdefault(key) fails if the key was not set before. It works only if the key was set or with the second argument. d.setdefault(key) is equivalent to d[key] except that it raises a weird TypeError instead of KeyError. There are two ways of solving this problem: 1. Reimplement setdefault() for dbm.gdbm and dmb.ndbm database classes with default=b'' by default. 2. Make the second argument mandatory. In both cases this violates the MutableMapping interface. | ||
msg315925 - (view) | Author: Raymond Hettinger (rhettinger) * ![]() |
Date: 2018-04-30 06:12 |
Option 2 seems perfectly reasonable here as a way of preventing bugs (working, correct code wouldn't be using the existing default of None). Option 1 is problematic because of the variance from Mutable Mapping, because breaking symmetry with get(), and because the change in behavior is implicit. | ||
msg315931 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * ![]() |
Date: 2018-04-30 07:21 |
Thank you Raymond. I hesitated to make a choose. Ned, is it good to add a deprecation warning in setdefault() methods of the dbm classes when they called with a single argument in 3.7? Their current behavior (no-op or error) is not useful in any case. | ||
msg315943 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * ![]() |
Date: 2018-04-30 09:09 |
Actually dbm.ndbm already implements option 1. Only dbm.gnu and dbm.dumb have non-working with a single argument setdefault(). |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:58:59 | admin | set | github: 77566 |
2018-04-30 09:11:07 | serhiy.storchaka | set | title: setdefault() with a single argument doesn't work for dbm.gdbm and dmb.ndbm objects -> setdefault() with a single argument doesn't work for dbm.gnu and dmb.dumb objects |
2018-04-30 09:09:52 | serhiy.storchaka | set | messages: + |
2018-04-30 07:21:47 | serhiy.storchaka | set | nosy: + ned.deilymessages: + |
2018-04-30 06:12:24 | rhettinger | set | nosy: + rhettingermessages: + |
2018-04-29 14:00:31 | serhiy.storchaka | create |