Mypy has troubles determining types when using generics with constraints and list deconstruction

Viewed 196

I hope somone can give me an explanation cause this problem seems so weird. This is the minimum code to reproduce the problem as far as I was able to conclude. It has something to do with the constraints of 'T' and the construction of an array with deconstructing another one. This happens in classes with constrained generics as well. Even for static methods.

from typing import TypeVar

T = TypeVar("T", int, str)
def foo(c: T) -> None:
    a = [1,2,3]
    print([a])   # works
    print(*a)    # works
    print([*a])  # error: Cannot determine type of "a"

if __name__ == "__main__":
    foo(3)
0 Answers
Related