New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
del expects __delitem__ if __setitem__ is defined #75401
Comments
I noticed some odd behaviour on classes defining __setitem__. 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 "<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__
>>> |
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. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: