You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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)
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
:Expected Behavior
Mypy infers that first two indices of all the tuples are integers, so the call
range(x[0])
is correct even forPROCESS_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 declaringBAD_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
Your Environment
Verbose output of mypy: verb.txt
The text was updated successfully, but these errors were encountered: