[Python] Is there any way to specify a typehinted Pytorch DataLoader?

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    Say I have the following:

    class SomeClass:
    def some_function(dataloader: DataLoader):
    for idx, batch in enumerate(dataloader):
    ... do something with batch ...


    I would like to type the dataloader such that I can show through function parameter typehinting what format I expect batch to have. For example, I would like to have batch be of type Tuple[Tensor, Tensor] or I would like to type it Tuple[Tensor, Tensor, CustomObject]. Is there any way in which I can specify this?

    I thought that maybe it would be possible through an AbstractClass inheriting DataLoader and then somehow specifying a type, but I'm not sure how that would look.

    Continue reading...

Compartilhe esta Página