python / python/mypy

Indexing tuple with `slice` object results in wrong type

Open
#9,200 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug priority-1-normal
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Example:

x = (1, 2, 3)
y = x[1:3]
z = x[slice(1, 3)]
reveal_locals()

Output from mypy:

note: Revealed local types are:
note:     x: Tuple[builtins.int, builtins.int, builtins.int]
note:     y: Tuple[builtins.int, builtins.int]
note:     z: builtins.int

I believe z should also have type Tuple[builtins.int, builtins.int], or at a minimum Tuple[builtins.int, ...].

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 by running the provided tuple-indexing example with mypy and compare the inferred types for slice syntax and an explicit slice object. Trace the type-checking path for tuple indexing, then add coverage showing that z is inferred as a tuple and confirm the revealed type matches the intended result.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.