Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
LEdoian opened this issue May 3, 2022 · 0 comments
Open

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

LEdoian opened this issue May 3, 2022 · 0 comments
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions

Comments

@LEdoian
Copy link

LEdoian commented May 3, 2022

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

@LEdoian LEdoian added the bug mypy got something wrong label May 3, 2022
@JelleZijlstra JelleZijlstra added the topic-join-v-union Using join vs. using unions label May 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions
Projects
None yet
Development

No branches or pull requests

2 participants