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.
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.
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.
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__ >>>
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