Skip to content

Typing Error in torch.utils.data.DataLoader and Dataset #38913

@jeongukjae

Description

@jeongukjae

🐛 Bug

In dataloader.py#60, DataLoader does not inherit typing.Generic, but in dataloader.pyi#15 type inferace of DataLoader inherits typing.Generic.

Dataset is also in same situation. See also dataset.py#L8, dataset.pyi#L6.

To Reproduce

>>> import torch
>>> from typing import List
>>> from torch.utils.data import Dataset, DataLoader
>>> class SomeDatasetClass(Dataset[List[torch.Tensor]]):
...     ...
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
>>> import torch
>>> from typing import List
>>> from torch.utils.data import Dataset, DataLoader
>>> class SomeDatasetClass(Dataset):
...     ...
... 
>>> def _create_dataloader(is_train: bool) -> DataLoader[List[torch.Tensor]]:
...     ...
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable

Expected behavior

DataLoader and Dataset should inherit typing.Generic, so above code should run without any exceptions.

>>> from typing import Generic, TypeVar
>>> T = TypeVar('T')
>>> class SomeGenericClass(Generic[T]):
...     ...
... 
>>> def create_some_generic_class() -> SomeGenericClass[bool]:
...     ...
...
>>>

Like above code snippet.

Environment

  • PyTorch Version (e.g., 1.0): 1.5.0
  • Python version: Python 3.7.5

Additional context

I think this issue is related to #31765

cc @ezyang @gchanan @zou3519

Metadata

Metadata

Assignees

Labels

high prioritymodule: typingRelated to mypy type annotationstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions