Issue 31218: del expects delitem if setitem is defined (original) (raw)

Created on 2017-08-16 10:48 by raumzeitkeks, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
delitem_example.py raumzeitkeks,2017-08-16 10:48 Minimal example demonstrating the differing behaviour
Messages (5)
msg300346 - (view) Author: Calvin (raumzeitkeks) Date: 2017-08-16 10:48
I noticed some odd behaviour on classes defining __setitem__. Using del on a class defining __setitem__ but not __delitem__ results in "AttributeError: __delitem__". On classes definig neiter __setitem__ nor __delitem__ on the other hand this results in "TypeError: 'WithoutSetItem' object doesn't support item deletion". See the appended example script.
msg300354 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-08-16 12:47
I'm not sure we would consider this a bug (the message is accurate), but I wouldn't object to fixing it, since that would indeed seem more consistent with how __delitem__ and del are defined in the language reference.
msg327857 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-10-17 04:38
This is likely an implementation artifact. In the abstract API, both PyObject_DelItem() and PyObject_SetItem() route through the same slot, m->mp_ass_subscript. The set and delete operations are only differentiated in the downstream concrete APIs. When the *value* parameter is NULL, the operation is deemed to be a deletion.
msg397037 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-07-06 13:36
It is still the same in 3.11: >>> class WithoutSetItem: ... def __getitem__(self, key): ... return "foo" ... >>> class WithSetItem: ... def __getitem__(self, key): ... return "foo" ... def __setitem__(self, key, val): ... return ... >>> wo = WithoutSetItem() >>> del wo[0] Traceback (most recent call last): File "", line 1, in TypeError: 'WithoutSetItem' object doesn't support item deletion >>> w = WithSetItem() >>> del w[0] Traceback (most recent call last): File "", line 1, in AttributeError: __delitem__ >>>
msg397167 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-07-08 19:38
I'm closing this as not being worth changing. It is only a minor irritant and arguably not a bug. "Fixing it" would be disruptive and likely not help anyone.
History
Date User Action Args
2022-04-11 14:58:50 admin set github: 75401
2021-07-08 19:38:45 rhettinger set status: open -> closedresolution: wont fixmessages: + stage: resolved
2021-07-06 13:36:21 iritkatriel set versions: + Python 3.11, - Python 3.6nosy: + iritkatrielmessages: + components: + Interpreter Core
2018-10-17 04:38:24 rhettinger set nosy: + rhettingermessages: +
2018-10-16 09:36:49 hongweipeng set nosy: + hongweipeng
2018-09-22 17:36:39 xtreak set nosy: + xtreak
2017-08-16 12:47:05 r.david.murray set nosy: + r.david.murraymessages: +
2017-08-16 10:48:18 raumzeitkeks create