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
from typing import Any, Dict, Tuple, Union
VarTuple = Union[Tuple[int, int], Tuple[int, int, int]]
def make_tuple():
# type: () -> VarTuple
return None
x = make_tuple()
a = b = c = 0
if len(x) == 3:
a, b, c = x
else:
a, b = x
c = 0
mypy complains about the following error:
sandbox.py:12: error: 'Union[Tuple[builtins.int, builtins.int], Tuple[builtins.int, builtins.int, builtins.int]]' object is not iterable
sandbox.py:14: error: 'Union[Tuple[builtins.int, builtins.int], Tuple[builtins.int, builtins.int, builtins.int]]' object is not iterable
The error message is incorrect. However, mypy may not be able to type check this properly since it doesn't recognize the len(x) == 3 check. So there seem to be two related issues:
Union types aren't recognized as iterables when type checking assignment statements.
len(x) == n checks can't be used to narrow down tuple types or unions. These could work similarly to isinstance checks.
The recommended way to work around the issue is to use casts:
if len(x) == 3:
a, b, c = cast(Tuple[int, int, int], x)
else:
a, b = cast(Tuple[int, int], x)
c = 0
Reacted by Christopher Peisert, Simon K and Lucas Basquerotto
If your list to unpack is never greater than your left side and you know how many variables are on the left side, then you can do this (assuming there are 5 variables on the left side):
Save the code in tmp/mypy_test/main.py and run mypy tmp/mypy_test/main.py, I get the following error message: tmp/mypy_test/main.py:13: error: Incompatible return value type (got "Tuple[int, ...]", expected "Tuple[int, int]") [return-value]
Activity
JukkaL commentedon Jan 28, 2016
The error message is incorrect. However, mypy may not be able to type check this properly since it doesn't recognize the
len(x) == 3check. So there seem to be two related issues:len(x) == nchecks can't be used to narrow down tuple types or unions. These could work similarly toisinstancechecks.The recommended way to work around the issue is to use casts:
JukkaL commentedon Nov 30, 2016
Also, mypy could recognize
len(x)whenxis a variable-length tuple. Example:neiljp commentedon Aug 21, 2017
I just ran into this issue and was going to post the following test case, but didn't realise that len(x) wasn't yet a way to distinguish among unions:
Remove wrong TODO note
Implement StartResponse using a protocol (#2392)
Implement StartResponse using a protocol (python#2392)
pgunn commentedon Apr 4, 2019
I'm getting a different error, but I'm guessing this is the same issue:
(I have a mypy.ini specifying check_untyped_defs)
10 remaining items
rodo-hf commentedon Apr 15, 2021
If your list to unpack is never greater than your left side and you know how many variables are on the left side, then you can do this (assuming there are 5 variables on the left side):
if you also want to catch the cases where the list is bigger than the variables, then you can do as follows:
Fix and narrow repr.py typing
wxgeo commentedon Mar 20, 2023
I ran into a similar issue today.
Minimal example:
An error is raised by mypy for last line:
error: Tuple index out of range [misc]arvindsree commentedon May 2, 2023
fofoni commentedon May 2, 2023
arvindsree commentedon May 4, 2023
fofoni commentedon May 5, 2023
arvindsree commentedon May 5, 2023
JelleZijlstra commentedon May 5, 2023
This is getting very off-topic. Let's leave this issue focused on narrowing tuple types with
len().w238liu commentedon Aug 25, 2023
Just wondering if this issue has been resolved? I still get a mypy error for the following code snippet:
Save the code in
tmp/mypy_test/main.pyand runmypy tmp/mypy_test/main.py, I get the following error message:tmp/mypy_test/main.py:13: error: Incompatible return value type (got "Tuple[int, ...]", expected "Tuple[int, int]") [return-value]JelleZijlstra commentedon Aug 25, 2023
It's not resolved, that's why this issue is still open.
w238liu commentedon Aug 25, 2023
Narrow tuple types using len() (#16237)