python / python/mypy

mypy does not recognize conversion between fixed length list and tuple

Open
#7,509 11 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

false-positive feature needs discussion priority-2-low
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

  • Are you reporting a bug, or opening a feature request?

I believe this is a bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

Minimal example to reproduce:

from typing import List, Tuple


def get_point_as_list() -> List[int]:
    return [1, 2]


def point_to_tuple(pt: List[int]) -> Tuple[int, int]:
    assert len(pt) == 2
    return tuple(pt)


if __name__ == "__main__":
    l = get_point_as_list()
    p = point_to_tuple(l)
    print(repr(p))
  • What is the actual behavior/output?

mypy test.py

test.py:10: error: Incompatible return value type (got "Tuple[int, ...]", expected "Tuple[int, int]")
  • What is the behavior/output you expect?

mypy should evaluate this as correct code

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

$ mypy --version
mypy 0.720

$ python --version
Python 3.7.3

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 reported example in test.py with mypy and confirm the incompatible Tuple[int, ...] versus Tuple[int, int] diagnostic. Trace how the type checker handles tuple() conversion from a fixed-length list and verify that the example is accepted without weakening unrelated type checks.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.