You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Item assignment and deletion raise IndexError instead of TypeError on types without them, for a huge index #162466
interpreter-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or error
Assigning to or deleting an item of a tuple, str, bytes, range, set or frozenset raises TypeError, unless the index is an int that doesn't fit in a Py_ssize_t. Then it raises IndexError, although the type supports no item assignment or deletion at all:
s = {1}; s[0] = 0 TypeError: 'set' object does not support item assignment
s = {1}; s[2**63] = 0 IndexError: cannot fit 'int' into an index-sized integer
t = (1, 2); del t[0] TypeError: 'tuple' object doesn't support item deletion
t = (1, 2); del t[2**63] IndexError: cannot fit 'int' into an index-sized integer
A set isn't even subscriptable, and s[2**63] (a read) does give TypeError: 'set' object is not subscriptable. The cause is in PyObject_SetItem and PyObject_DelItem (Objects/abstract.c): when the type has tp_as_sequence, an index key is converted with PyNumber_AsSsize_t(key, PyExc_IndexError) before anyone checks that sq_ass_item exists. The branch for a non-index key already checks sq_ass_item; checking it first in both branches would give the TypeError. PyPy 3.12 raises TypeError in all four cases. I found it while writing a Lean spec of Python.
I think the current behavior is still better than the other one. Independently of whether the susbcription is supported or not, the conversion to an index-like object should be done first IMO. Insee the operation as:
idx = to_index(i)
assign(s, idx, v)
I know that @serhiy-storchaka touched the code for index conversions so this might also have been a deliberate change.
The read path does it the other way round, though: PyObject_GetItem only converts the key when the type has sq_item, so a set gives TypeError for a read and IndexError for a write or a delete with the same key:
Checking sq_ass_item before the conversion in PyObject_SetItem and PyObject_DelItem would make the three agree. If converting first is the intended order for writes, I'm happy to leave it.
Ah I missed this inconsistency. Mmh, I would prefer that Serhiy tells me what he wanted to do first. We might also change the read path exception as well...
This is simply an implementation artifact, the consequence of the code structure. To make better exception we need either inline PySequence_DelItem() and PySequence_SetItem() or add additional checks which will slow down everybody. It would be nice to have better exception, but I am not sure if it is worth the cost. It is not that the current exception is absolutely inappropriate.
interpreter-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or error
Activity
picnixz commentedon Nov 23, 2026
I think the current behavior is still better than the other one. Independently of whether the susbcription is supported or not, the conversion to an index-like object should be done first IMO. Insee the operation as:
I know that @serhiy-storchaka touched the code for index conversions so this might also have been a deliberate change.
Robertboy18 commentedon Nov 24, 2026
The read path does it the other way round, though:
PyObject_GetItemonly converts the key when the type hassq_item, so a set givesTypeErrorfor a read andIndexErrorfor a write or a delete with the same key:Checking
sq_ass_itembefore the conversion inPyObject_SetItemandPyObject_DelItemwould make the three agree. If converting first is the intended order for writes, I'm happy to leave it.picnixz commentedon Nov 24, 2026
Ah I missed this inconsistency. Mmh, I would prefer that Serhiy tells me what he wanted to do first. We might also change the read path exception as well...
serhiy-storchaka commentedon Nov 24, 2026
This is simply an implementation artifact, the consequence of the code structure. To make better exception we need either inline
PySequence_DelItem()andPySequence_SetItem()or add additional checks which will slow down everybody. It would be nice to have better exception, but I am not sure if it is worth the cost. It is not that the current exception is absolutely inappropriate.Robertboy18 commentedon Nov 27, 2026
Thanks Serhiy, that makes sense. The current exception isn't really wrong, so I'm happy to leave it and close this.