I have a small piece of code where Pycharm gives me a warning I don't understand how to get rid of:
movement_dict = {'R': (0, 1), 'L': (0, -1), 'U': (-1, 0), 'D': (1, 0), 'RU': (-1, 1), 'LU': (-1, -1), 'LD': (1, -1),
'RD': (1, 1)}
def move_in_dir(position, dir):
return tuple(i + j for i, j in zip(position, movement_dict[dir]))
if __name__ == '__main__':
rope = [(0, 0) for _ in range(10)]
rope[0] = move_in_dir(rope[0], 'LD')
print(rope[0])
The warning is:
Unexpected type(s): (int, tuple[Any, ...]) Possible type(s): (SupportsIndex, tuple[int, int]) (slice, Iterable[tuple[int, int]])
and it is located at the first 0 in rope[0] = move_in_dir(rope[0], 'LD')
.
The code works perfectly fine and as expected, it just bugs me that i don't understand the warning. Somehow the tuples are of a different type, but is there a way to change that?
My python version is 3.10.6
The warning complains about a difference between tuple[Any, ...]
and tuple[int, int]
.
Python understands from the following line of code that you will have a list of tuple[int, int]
elements.
rope = [(0, 0) for _ in range(10)]
However inside move_in_dir()
you return a tuple in which you do a comprehension. The elements over which you loop do not have a type (Any
) because it is not specified in the function declaration. Moreover it is not known beforehand how many elements will be looped over in the comprehension.
If you combine these two things you end up having a return type of tuple[Any, ...]