I encounter a question when I use the module mypy to check the type of values of variables in my code. It can be demonstrated in the example as below:
from typing import Set, Tuple
x: Set[tuple] = {('a', 100, True),
('b', 200, False)}
y: Tuple[tuple] = (('a', 100, True),
('b', 200, False))
The only difference between variables x and y is that x is a set and y is a tuple. Except that, they contain the same elements. But when I ran mypy to check this code, it reported the following error for line 6:
test.py:6: error: Incompatible types in assignment (expression has type "Tuple[Tuple[str, int, bool], Tuple[str, int, bool]]", variable has type "Tuple[Tuple[Any, ...]]") [ass
ignment]
Found 1 error in 1 file (checked 1 source file)
I got confused. Why did mypy report an error for incompatible types in line 6 of my example? And why was the same error not reported for line 3?
>Solution :
Tuple[tuple] means a tuple containing exactly one tuple, not a tuple of tuples. A tuple of tuples is Tuple[tuple, ...].