I have following code:
from typing import Union,List,Any
v: Union[list[list[int]],list[int]] = [-1,3,1,6,-5] # Create a list of inst
if not isinstance(v[0],list):
v =[v]
v =[v] # Cast list of ints to list of list of ints
print(v)
mypy
complains about it in the following way:
functions.py:5: error: Incompatible types in assignment
(expression has type "List[Union[List[List[int]], List[int]]]",
variable has type "Union[List[List[int]], List[int]]") [assignment]
Found 1 error in 1 file (checked 1 source file)
I probably do not understand how type hints work but I really can not understand why assigning the new v
variable this way makes mypy complain, because the end object is actually a list[list[int]]
.
Can anybody explain?
PS
I want to add a note: I encountered this issue while adding type hinting to some code that I do not own on github, so I can not really touch the "logic" of it.
Apparently the Issue is solved by changing v = [v]
to v=[[*v]]
.
Not clear why...