Skip to content

Item assignment and deletion raise IndexError instead of TypeError on types without them, for a huge index #162466

Not planned

Description

@Robertboy18

Bug report

Bug description:

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:

for stmt in ["s = {1}; s[0] = 0", "s = {1}; s[2**63] = 0", "t = (1, 2); del t[0]", "t = (1, 2); del t[2**63]"]:
    try:
        exec(stmt)
    except Exception as e:
        print(f"{stmt:26} {type(e).__name__}: {e}")
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.

CPython versions tested on:

3.12, 3.15, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    interpreter-core
    type-bug
    on Nov 23, 2026
  2. picnixz commented on Nov 23, 2026

    @picnixz
    Member

    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.

  3. added
    pending
    on Nov 23, 2026
  4. Robertboy18 commented on Nov 24, 2026

    @Robertboy18
    Author

    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:

    >>> {1}[2**63]
    TypeError: 'set' object is not subscriptable
    >>> {1}[2**63] = 0
    IndexError: cannot fit 'int' into an index-sized integer
    >>> del {1}[2**63]
    IndexError: cannot fit 'int' into an index-sized integer

    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.

  5. picnixz commented on Nov 24, 2026

    @picnixz
    Member

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

  6. serhiy-storchaka commented on Nov 24, 2026

    @serhiy-storchaka
    Member

    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.

  7. Robertboy18 commented on Nov 27, 2026

    @Robertboy18
    Author

    Thanks Serhiy, that makes sense. The current exception isn't really wrong, so I'm happy to leave it and close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-corependingtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions