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

Open Beginner friendly
#162,466 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@TINGyu123644 is already working on this.

Since Nov 23, 2026.

  • #162470 by @TINGyu123644 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
62/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, python
Domain
backend

Research direction

The bug is in PyObject_SetItem and PyObject_DelItem in Objects/abstract.c, where an index key is converted with PyNumber_AsSsize_t(key, PyExc_IndexError) before checking that sq_ass_item exists. Start by reading those two functions and the non-index branch, which already checks sq_ass_item. Done when tuple, set, str and range item assignment or deletion with a huge int raises TypeError, and a regression test in Lib/test covering the four cases from the report passes.

Written by the indexing model from the issue text.

Description

interpreter-core type-bug

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

Dominant language
Python
Stars
78.4k
Forks
36.6k
Avg merge
23h 34m
Merged PRs (30d)
610

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/cpython

All issues in python/cpython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.