Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`Iterable[(int, int)]` tuple is not allowed in type hints

I have this very simple code:

from typing import List, Iterable

Position = (int, int)
IntegerMatrix = List[List[int]]

def locate_zeros(matrix: IntegerMatrix) -> Iterable[Position]:
    """Given an NxM matrix find the positions that contain a zero."""
    for row_num, row in enumerate(matrix):
        for col_num, element in enumerate(row):
            if element == 0:
                yield (col_num, row_num)

This is the error:

Traceback (most recent call last):
  File "type_m.py", line 6, in <module>
    def locate_zeros(matrix: IntegerMatrix) -> Iterable[Position]:
  File "/usr/lib/python3.5/typing.py", line 970, in __getitem__
    (len(self.__parameters__), len(params)))
TypeError: Cannot change parameter count from 1 to 2

Why can't I have an iterable of Int pairs as the return type?

Both -> Position and Iterable[Any] work, just not Iterable and Position together don't.

like image 896
Caridorc Avatar asked Sep 18 '16 21:09

Caridorc


1 Answers

You should use typing.Tuple[int, int] to declare the tuple type Position, not (int, int).

like image 124
Sven Marnach Avatar answered Sep 23 '22 03:09

Sven Marnach