python / python/mypy

Wrong type inference for `<tuple>[s]` where `s: slice`

Open
#21,708 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

a = (1, 2, 3)
s = slice(None, 1)
reveal_type(a[s])  # Revealed type is `builtins.int`
reveal_type(a.__getitem__(s))  # Revealed type is `builtins.tuple[builtins.int, ...]` (OK)

The runtime value of a[s] is (1,).

The type inference works for lists though.

Your Environment

  • Mypy version used: 1.0.0-2.1, master
  • Python version used: 3.12, 3.13

Contributor guide

Open the contributing guide

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.

Research direction

Start with the supplied tuple and slice reproducer, then trace mypy's tuple subscription type inference and compare it with the existing list-slice handling. Done means both a[s] and a.__getitem__(s) reveal a tuple type consistent with the runtime value, without regressing ordinary tuple indexing.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.