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: del expects __delitem__ if __setitem__ is defined
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: hongweipeng, iritkatriel, r.david.murray, raumzeitkeks, rhettinger, xtreak
Priority: normal Keywords:

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 "<stdin>", line 1, in <module>
TypeError: 'WithoutSetItem' object doesn't support item deletion
>>> w = WithSetItem()
>>> del w[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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:50adminsetgithub: 75401
2021-07-08 19:38:45rhettingersetstatus: open -> closed
resolution: wont fix
messages: + msg397167

stage: resolved
2021-07-06 13:36:21iritkatrielsetversions: + Python 3.11, - Python 3.6
nosy: + iritkatriel

messages: + msg397037

components: + Interpreter Core
2018-10-17 04:38:24rhettingersetnosy: + rhettinger
messages: + msg327857
2018-10-16 09:36:49hongweipengsetnosy: + hongweipeng
2018-09-22 17:36:39xtreaksetnosy: + xtreak
2017-08-16 12:47:05r.david.murraysetnosy: + r.david.murray
messages: + msg300354
2017-08-16 10:48:18raumzeitkekscreate