Skip to content

Fix Type Generics for VisionDataset - #9381

Merged
NicolasHug merged 5 commits into
pytorch:mainfrom
gabrielfruet:fix/type-generics
Feb 19, 2026
Merged

NicolasHug merged 5 commits into
pytorch:mainfrom
gabrielfruet:fix/type-generics

Conversation

@gabrielfruet

Copy link
Copy Markdown
Contributor

Fixes #9282

Only modified VisionDataset for simplicity.

@pytorch-bot

pytorch-bot Bot commented Feb 9, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9381

Note: Links to docs will display an error until the docs builds have been completed.

⏳ 1 Pending, 2 Unrelated Failures

As of commit cd1de4f with merge base 0f6d91d (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

BROKEN TRUNK - The following job failed but was present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Feb 9, 2026

Copy link
Copy Markdown

Hi @gabrielfruet!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@meta-cla

meta-cla Bot commented Feb 9, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the cla signed label Feb 9, 2026
@NicolasHug

Copy link
Copy Markdown
Member

Thanks for the PR @gabrielfruet . I'm a bit skeptical about the added value. Can you share what kind of errors / typing issues this is preventing?

@gabrielfruet

Copy link
Copy Markdown
Contributor Author

Without the PR:

from torch.utils.data import DataLoader
from torchvision.datasets import VisionDataset

class MyDataset(VisionDataset):
    def __getitem__(self, index: int) -> tuple[Image, int]:
        # Returns image and label
        ...

loader = DataLoader(MyDataset(...))
for batch in loader:
    # Type checker thinks 'batch' is Any, not tuple[Image, int]
    # No autocomplete, no type checking

With the PR:

class MyDataset(VisionDataset[tuple[Image, int]]):
    def __getitem__(self, index: int) -> tuple[Image, int]:
        ...

loader = DataLoader(MyDataset(...))
for batch in loader:
    # Type checker now knows batch is tuple[Image, int]
    # Full IDE autocomplete and type checking support

@NicolasHug NicolasHug left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks for the details @gabrielfruet . Since this seems officially supported by the DataLoader, and since mypy is happy, this sounds reasonably.

@NicolasHug
NicolasHug merged commit 9996814 into pytorch:main Feb 19, 2026
3 checks passed
@github-actions

Copy link
Copy Markdown

Hey @NicolasHug!

You merged this PR, but no labels were added.
The list of valid labels is available at https://github.com/pytorch/vision/blob/main/.github/process_commit.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make VisionDataset generic following Dataset

3 participants