python / python/mypy

Too broad type inferred for literal list of heterogeneous tuples

Open
#12,720 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-join-v-union
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

When defining a list literal consisting of heterogeneous tuples with different lengths, mypy infers list[tuple[object, ...]], even when some fields of all the tuples have same type (BAD_LIST bellow). This then propagates and may raise false warnings of types not being compatible (see bellow).

This is somewhat similar to #6968, but since here all the container types are inferred correctly, I think it is a separate issue.

To Reproduce

x.py:

NICE_LIST =  [
		(1, 2),
		(4, 5),
	]
BAD_LIST =  [
		(1, 2, 'I am bad'),
		(4, 5),
	]

PROCESS_NICE = [(x[1], range(x[0]), ['something' for _ in range(x[1])])
	for x in NICE_LIST]
PROCESS_BAD = [(x[1], range(x[0]), ['something' for _ in range(x[1])])
	for x in BAD_LIST]

reveal_locals()

Expected Behavior

Mypy infers that first two indices of all the tuples are integers, so the call range(x[0]) is correct even for PROCESS_BAD so mypy would not raise an error.

Alternatively, mypy could give up on BAD_LIST as "too hard to infer" and require explicit type hint for it. (Explicitly declaring BAD_LIST: list[tuple[int, int, object] | tuple[int,int]] = [...] fixes the issue, but feels tedious.)

For completeness, in order to be consistent, mypy should have crashed on both ranges, not just on the first one.

Actual Behavior

$ mypy --pretty --show-error-codes x.py 
x.py:12: error: No overload variant of "range" matches argument type "object"  [call-overload]
    PROCESS_BAD = [(x[1], range(x[0]), ['something' for _ in range(x[1])])
                          ^
x.py:12: note: Possible overload variants:
x.py:12: note:     def range(self, SupportsIndex) -> range
x.py:12: note:     def range(self, SupportsIndex, SupportsIndex, SupportsIndex = ...) -> range
x.py:15: note: Revealed local types are:
x.py:15: note:     BAD_LIST: builtins.list[builtins.tuple[builtins.object, ...]]
x.py:15: note:     NICE_LIST: builtins.list[Tuple[builtins.int, builtins.int]]
x.py:15: note:     PROCESS_BAD: builtins.list[Tuple[builtins.object, Any, builtins.list[builtins.str]]]
x.py:15: note:     PROCESS_NICE: builtins.list[Tuple[builtins.int, builtins.range, builtins.list[builtins.str]]]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 0.950 (compiled: yes)
  • Python version used: 3.10.4
  • Operating system and version: Arch Linux

Verbose output of mypy: verb.txt

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 mypy on the x.py reproduction and compare the inferred types for NICE_LIST, BAD_LIST, PROCESS_NICE, and PROCESS_BAD with the expected behavior. Trace list and heterogeneous-tuple literal inference in the type checker, then add regression coverage showing that shared tuple fields retain their compatible types and that both range calls are accepted.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.