Skip to content

Too broad type inferred for literal list of heterogeneous tuples #12720

Open
@LEdoian

Description

@LEdoian

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-join-v-unionUsing join vs. using unions

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions