Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading