python / python/mypy

Literal slicing of a variable-length tuple does not work

Open
#13,058 1 comment 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

Slicing a variable-length tuple with a literal int does not reveal as a corresponding fixed-length tuple. This causes both false negative, where no error reported with incorrect unpacking, and false positive of incompatible type with say return values.

To Reproduce

def foo(x: tuple[str, ...]) -> tuple[str, str]:
    a, b = x[:3]

    return x[:2]

Expected Behavior

a, b = x[:3] should give an error of too many values to unpack, return x[:2] should pass with no error.

Actual Behavior

a, b = x[:3] gives no error, return x[:2] says incompatible return value type.

Your Environment

  • Mypy version used: 0.961
  • Python version used: 3.10
  • Operating system and version: macOS 12.4

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 reproducing the provided tuple-slicing example with mypy 0.961 on Python 3.10 and inspect the type-checking path for literal slices of variable-length tuples. Done means reporting an error for unpacking x[:3] into two values while accepting x[:2] as tuple[str, str].

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.