Skip to content

tuple index out of range error is inconsistent #20475

@randolf-scholz

Description

@randolf-scholz

Either both of these should error, or neither of them.

def test1(items: tuple[int, ...]) -> None:
    reveal_type(items[1])  # ✅️
    reveal_type(items[2])  # ✅️
    reveal_type(items[3])  # ✅️

def test2(items: tuple[int, *tuple[int, ...]]) -> None:
    reveal_type(items[1])  # ❌️ tuple index out of range
    reveal_type(items[2])  # ❌️ tuple index out of range
    reveal_type(items[3])  # ❌️ tuple index out of range

https://mypy-play.net/?mypy=master&python=3.12&gist=dc9de67cb18ae540cdbb6bc32632eae7

FWIW, only mypy errors here. pyright (strict): no errors, pyrefly no errors, ty: no errors.

Such tuple types can occur naturally, which makes this check prone to false positives, for instance tup=(id, *values), when values is list-like.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions