Manuel Semeco
Manuel Semeco

Reputation: 53

mypy error regarding complex type List[Tuple[...]?

I have the fallowing code parsed by mypy :

 PointsList = List[Tuple[str, int, int]]

 def Input(S: str, X: List[int], Y: List[int]) -> PoinstList:

        inp = list()
        for tag, x, y in zip(S, X, Y):
            inp.append(tuple([tag, x, y]))

        return inp

After parsing it ,returns the message down bellow.

a.py:28: error: Incompatible return value type (got "List[Tuple[object, ...]]", expected "List[Tuple[str, int, int]]")
Found 1 error in 1 file (checked 1 source file)

So what's wrong with the definition?, why mypy saw the returning object's type like List[Tuple[object, ...] instead of List[Tuple[str, int, int]] as shoulded be?. Thank you in advance.

Upvotes: 0

Views: 1522

Answers (1)

user2357112
user2357112

Reputation: 280182

The problem is [tag, x, y]. Mypy does not recognize a type for "3-element list of string, int, and int". The type it computes for [tag, x, y] is List[object], and calling tuple on that produces Tuple[object, ...].

Instead of tuple([tag, x, y]), just use a tuple literal: (tag, x, y).

Or skip the loop entirely: return list(zip(S, X, Y))

Upvotes: 2

Related Questions