Why does the 'mypy' module report a type incompatibility error for a variable which is a tuple having container type elements?

Question:

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?

Asked By: thomas_chang

||

Answers:

Tuple[tuple] means a tuple containing exactly one tuple, not a tuple of tuples. A tuple of tuples is Tuple[tuple, ...].

Answered By: user2357112
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.