I currently have this code
T = TypeVar("T")
Grid = Sequence[Sequence[T]]
def columns(grid: Grid) -> Iterable[list[T]]:
return ([row[i] for row in grid] for i in range(len(grid[0])))
But I think the T in the alias Grid is bound to a different T in the return type of the function.
How do I define Grid such that I can write
def columns(grid: Grid[T]) -> Iterable[list[T]]:
...
I've looked at typing.GenericAlias, but can't see how it helps me.
(I'm aware that Sequence[Sequence[T]] has no guarantee that the grid is actually rectangular, but that's not the problem I want to focus on here.)
When using type variable as a generic parameter, it can be replaced by other type variables, which is mentioned in the Generic Alias Type (but I only found this one):
So there is no problem with your current implementation. In the interactive interpreter, you will see:
Mypy will also correctly analyze them: